最近在學習spring-cloud微服務相關(guān)內(nèi)容,首先對服務治理Eureka了解學習,記錄下學習過程中的內(nèi)容。
一,服務注冊中心
1,Maven依賴,這里使用的是Camden.RELEASE版本
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Camden.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2,創(chuàng)建啟動類
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* EUREKA SERVER
* 使用SpringBoot注解快速構(gòu)建
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main( String[] args ) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
3,配置注冊中心信息
eureka:
instance:
hostname: localhost
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
client:
#是否從注冊中心獲取注冊信息,默認為true
fetch-registry: false
#是否將自身注冊到注冊中心,默認為true
register-with-eureka: false
#eureka注冊中心地址(/eureka是固定值)
service-url:
defaultZone: http://localhost:${server.port}/eureka
spring:
application:
name: eureka-server
server:
port: 8000
這里需要注意的是配置文件使用的是YAML語言,需要注意一些使用規(guī)范:
1.大小寫敏感
2.使用縮進表示層級關(guān)系
3.縮進時不允許使用Tab鍵,只允許使用空格
4.縮進的空格數(shù)目不重要,只要相同層級的元素左側(cè)對齊即可
4,啟動注冊中心
可以直接運行main函數(shù),也可以使用maven命令spring-boot:run
輸出如下信息表示啟動成功:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.0.RELEASE)
省略輸出信息......
2017-08-08 21:02:03.002 INFO 1932 --- [ Thread-9] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2017-08-08 21:02:03.391 INFO 1932 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8000 (http)
2017-08-08 21:02:03.394 INFO 1932 --- [ main] c.n.e.EurekaDiscoveryClientConfiguration : Updating port to 8000
2017-08-08 21:02:03.426 INFO 1932 --- [ main] c.s.c.eureka.EurekaServerApplication : Started EurekaServerApplication in 20.1 seconds (JVM running for 39.913)
5,管理頁面

管理頁面.png

管理頁面.png
目前為止,一個Spring-Cloud-Eureka的服務注冊中心就構(gòu)建好了