本篇和大家分享的是springcloud-config配置中心搭建,寫到這里突然想起自己曾今開源過基於Redis發布訂閱編寫的一個配置中心,剛看了git星數有點少哈哈,這里順勢發個連接歡迎大俠們點贊:https://github.com/shenniubuxing3/IConfCenter
- springcloud版本說明
- config-server配置中心
- config-client配置客戶端
- eureka注冊中心實現配置高可用
springcloud版本說明
由於市面上其版本比較多,版本不一可能造成了讀者嘗試時版本問題,所以這里指明當前作者寫文章時使用的cloud版本,springboot版本:
1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>2.0.7.RELEASE</version> 5 <relativePath/> <!-- lookup parent from repository --> 6 </parent>
springcloud版本:
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>
config-server配置中心
config配置中心主要是用來獲取要發布的配置文件信息,並開放接口被其他調用者使用,先上maven配置:
1 <dependency> 2 <groupId>org.springframework.cloud</groupId> 3 <artifactId>spring-cloud-config-server</artifactId> 4 </dependency>
通常在程序入口處添加注解 @EnableConfigServer 然后我們還需要知道開放那些配置文件作為配置信息來源,因此需要在application.yml文件中配置如下信息:
1 spring: 2 application: 3 name: config-server #springcloud-config默認采用application作為name 4 cloud: 5 config: 6 server: 7 native: 8 search-locations: file:///D:/my_study/study_java/springcloud_3/config-server/src/main/resources/config 9 profiles: 10 active: native 11 server: 12 port: 4020
這里我使用本地的配置文件目錄 src/main/resources/config 來提供配置文件,如果在windows上其實不用寫file:///,不過官網還特別標注了windows上file后面要多一個 '/' 這里需要大家注意;這里我config文件夾下有兩個配置文件,如下:
此刻我們最簡單的配置服務就搭建好了,啟動程序並訪問如下地址:http://10.0.75.1:4020/config-server/conf1,conf0;值得注意的時候這里用 ',' 分割了下,在瀏覽器中得到如下兩個配置文件合並后的信息:
可以去掉其中任何一個conf1或者conf0,得到的就是對應配置文件的信息,這里通過瀏覽器訪問的路徑規則是:http://xx.xx.xx/{application}/{profiles}/{label} label默認null
config-client配置客戶端
同樣先來看pom的config-client對應的配置,這里多了個web依賴因為我打算在api接口信息看配置效果
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-web</artifactId> 4 </dependency> 5 <dependency> 6 <groupId>org.springframework.cloud</groupId> 7 <artifactId>spring-cloud-starter-config</artifactId> 8 </dependency>
然后在Application入口處增加注解 @EnableDiscoveryClient 下面就是配置文件中的信息了,要注意的是這個版本config-client的config相關配置要放在名稱為 bootstrap.properties 的文件中(這是默認的配置文件名),如下bootstrap.yml信息:
1 spring: 2 cloud: 3 config: 4 name: config-server #application 5 profile: conf1,conf0 #profile 后來者覆蓋,沒有合並 6 label: #label 7 uri: http://10.0.75.1:4020
需要注意的是uri配置的是剛才上面我們訪問的config-server地址,其他的幾個配置對應剛才說的url規則
application.yml配置:
1 spring: 2 application: 3 name: config-client 4 server: 5 port: 5020
再來定義個api接口:
1 @RestController 2 public class ConfigController { 3 4 @Value("${shenniu.author}") 5 private String author; 6 7 @Value("${shenniu.des}") 8 private String des; 9 10 @GetMapping("/getPort") 11 public String getPort() { 12 return "作者:" + author + 13 "描述:" + des; 14 } 15 }
此時運行config-client,通過開放的api接口返回映射的配置信息如下:
eureka注冊中心實現配置高可用
高可用通俗來講就是部署多個服務,當某個掛掉的時候其他的頂上去,這里使用Eureka注冊中心(后面可能會分享關於zk和consul);先創建個eureka-server項目並運行起來:
1 <dependency> 2 <groupId>org.springframework.cloud</groupId> 3 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 4 </dependency>
在Application入口處添加 @EnableEurekaServer 注解啟動eureka服務,這里我分別啟動1020,1021,1022三個eureka服務端口,使其自身是高可用的,相關配置:
1 spring: 2 application: 3 name: eureka 4 server: 5 port: 1020 6 eureka: 7 instance: 8 appname: ${spring.application.name} 9 client: 10 # register-with-eureka: false #開啟自動注冊到eureka中心,高可用 11 # fetch-registry: false 12 service-url: 13 defaultZone: http://localhost:1020/eureka/,http://localhost:1021/eureka/,http://localhost:1022/eureka/ 14 server: 15 eviction-interval-timer-in-ms: 30000 #檢測失效信息的時間 16 enable-self-preservation: false #關閉自我保護 17 use-read-only-response-cache: false
下面需要分別改造下config-server和config-client的配置,可以遵循如下信息:
config-server
pom增加:
1 <dependency> 2 <groupId>org.springframework.cloud</groupId> 3 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 4 </dependency>
application.yml增加:
1 eureka: 2 client: 3 service-url: 4 defaultZone: http://localhost:1020/eureka/,http://localhost:1021/eureka/,http://localhost:1022/eureka/ 5 instance: 6 appname: ${spring.application.name} 7 prefer-ip-address: true
config-client
pom增加:
1 <dependency> 2 <groupId>org.springframework.cloud</groupId> 3 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 4 </dependency>
bootstrap.yml改造:
1 spring: 2 cloud: 3 config: 4 name: config-server #application 5 profile: conf1,conf0 #profile 后來者覆蓋,沒有合並 6 label: #label 7 # uri: http://10.0.75.1:4020 8 discovery: 9 enabled: true 10 service-id: CONFIG-SERVER 11 eureka: 12 client: 13 service-url: 14 defaultZone: http://localhost:1020/eureka/,http://localhost:1021/eureka/,http://localhost:1022/eureka/ 15 instance: 16 appname: ${spring.application.name} 17 prefer-ip-address: true
如果可以吧config-server多開幾個端口,都注冊到eureka中心,成功后如下信息:
同樣訪問api接口時得到如下獲取配置成功信息