效果
- 服務中心高可用:其實就是互相注冊
- 服務中心的配置文件由配置中心進行管理
- 配置中心注冊服務到服務中心
統(tǒng)一服務注冊中心
新建項目:enreka-server-center
添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
添加注解
@EnableEurekaServer
配置配置中心
application-peer1
spring.application.name=high-eureka-service
server.port=1111
eureka.instance.hostname=peer1
eureka.client.service-url.defaultZone=http://peer2:1112/eureka/
application-peer2
spring.application.name=high-eureka-service
server.port=1112
eureka.instance.hostname=peer2
eureka.client.service-url.defaultZone=http://peer1:1111/eureka/
統(tǒng)一的配置中心
新建項目: config-center
添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
添加注解
@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
配置配置中心
spring.application.name=config-server
server.port=8888
spring.cloud.config.server.git.uri=http://github.com/liuyiyou/config_center_repository.git
##如果配置中心git倉庫是多文件夾方式
spring.cloud.config.server.git.searchPaths=config_repo,config_repo/*
spring.cloud.config.label=master
spring.cloud.config.server.git.username=xxxx
spring.cloud.config.server.git.password=xxxx
配置中心完成
配置中心客戶端(其他服務)
新建項目: config-client
添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
添加注解
@EnableDiscoveryClient
@SpringBootApplication
配置客戶端
spring.application.name=config-client
#服務注冊中心地址: 這個只能放在項目中奈偏,而不是放在git倉庫中
eureka.client.service-url.defaultZone=http://peer1:1111/eureka
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
spring.cloud.config.profile=dev
需要說明:其他服務注冊服務中心的地址必須放到項目的配置文件夾中,因為啟動注冊中心后,在啟動客戶端的時候丰滑,默認會通過service-url去找到config-server暴露的服務惯吕,通過config-server暴露的服務去git倉庫中讀取其他的配置信息