javascript
第七篇: 高可用的分布式配置中心(Spring Cloud Config)(Finchley版本)V2.0_dev
前言:
上一篇文章講述了一個服務如何從配置中心讀取文件,配置中心如何從遠程git讀取配置文件,當服務實例很多時,都從配置中心讀取文件,這時可以考慮將配置中心做成一個微服務,將其集群化,從而達到高可用。
繼續使用上一篇文章的工程,
一、創建一個eureka-server工程,用作服務注冊中心。
第1步:在其pom.xml文件引入Eureka的起步依賴spring-cloud-starter-netflix- eureka-server,代碼如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.gblfy</groupId><artifactId>sc-f-chapter6</artifactId><version>0.0.1-SNAPSHOT</version></parent><groupId>com.gblfy</groupId><artifactId>eureka-server</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>eureka-server</name><description>Demo project for Spring Boot</description><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies> </project>第2步:在配置文件application.yml上,指定服務端口為8761,加上作為服務注冊中心的基本配置,代碼如下:
server:port: 8761eureka:instance:hostname: localhostclient:registerWithEureka: falsefetchRegistry: falseserviceUrl:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/spring:application:name: eurka-server第2步:入口類:
@SpringBootApplication @EnableEurekaServer @EnableDiscoveryClient public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}}二、改造config-server
第1步:在其pom.xml文件加上EurekaClient的起步依賴spring-cloud-starter-netflix-eureka-client,代碼如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.gblfy</groupId><artifactId>sc-f-chapter6</artifactId><version>0.0.1-SNAPSHOT</version></parent><groupId>com.gblfy</groupId><artifactId>config-server</artifactId><version>0.0.1-SNAPSHOT</version><name>config-server</name><description>Demo project for Spring Boot</description><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency></dependencies> </project>第2步:配置文件application.yml,指定服務注冊地址為http://localhost:8761/eureka/
eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/ spring:application:name: config-servercloud:config:server:git:uri: git@github.com:gb-heima/config-rep.gitusername: password: basedir: /Users/Administrator.PC-20180929LWLP/Desktop/springcloud-practical-column/sc-f-chapter6/config/basedir第3步:最后需要在程序的啟動類Application加上@EnableConfigServer
@SpringBootApplication @EnableConfigServer public class ConfigServerApplication {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args);}}三、改造config-client
第1步:將其注冊微到服務注冊中心,作為Eureka客戶端,需要pom文件加上起步依賴spring-cloud-starter-netflix-eureka-client,代碼如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.gblfy</groupId><artifactId>sc-f-chapter6</artifactId><version>0.0.1-SNAPSHOT</version></parent><groupId>com.gblfy</groupId><artifactId>config-client</artifactId><version>0.0.1-SNAPSHOT</version><name>config-client</name><description>Demo project for Spring Boot</description><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency></dependencies> </project>第2步:配置文件bootstrap.yml,注意是bootstrap。
加上服務注冊地址為:http://localhost:8761/eureka/
- spring.cloud.config.discovery.enabled 是從配置中心讀取文件。
- spring.cloud.config.discovery.serviceId 配置中心的servieId,即服務名。
這時發現,在讀取配置文件不再寫ip地址,而是服務名,這時如果配置服務部署多份,通過負載均衡,從而高可用。
依次啟動:
eureka-server,端口號為8761,
config-server的3個實例,端口號為8888,9999,0000
config-client:的2個實例,端口號為8080,8081
訪問網址:http://localhost:8761/,就會出現eureka頁面
注冊的應用有:
1個eureka-server(默認隱藏)
3個config-server
2個config-client
共計6個
- 在config-client啟動之前先把控制臺清除
- 分別啟動config-client的2個實例,觀察控制臺輸出
以上3種場景都有可能出出現,由于client端從server端拉取信息,采用的時輪訓策略
訪問http://localhost:8881/hi?name=gblfy,
瀏覽器顯示:
hi,i am from port 8080訪問http://localhost:8880/hi?name=gblfy,
瀏覽器顯示:
hi,i am from port 8081本文源碼下載:
dev分支(最新企業實戰版本):
與master代碼一樣只是啟動的實例多幾個而已,參看master即可
https://github.com/gb-heima/springcloud-practical-column/tree/master/sc-f-chapter6
master分支(入門版本):
https://github.com/gb-heima/springcloud-practical-column/tree/master/sc-f-chapter6
總結
以上是生活随笔為你收集整理的第七篇: 高可用的分布式配置中心(Spring Cloud Config)(Finchley版本)V2.0_dev的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 不太平凡的2020、平凡的我
- 下一篇: 企业实战02:Oracle数据库的安装和