本篇和你們分享的是springcloud-config配置中心搭建,寫到這裏忽然想起本身曾今開源過基於Redis發佈訂閱編寫的一個配置中心,剛看了git星數有點少哈哈,這裏順勢發個鏈接歡迎大俠們點贊:https://github.com/shenniubuxing3/IConfCenterjava
因爲市面上其版本比較多,版本不一可能形成了讀者嘗試時版本問題,因此這裏指明當前做者寫文章時使用的cloud版本,springboot版本:git
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版本:github
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>
config配置中心主要是用來獲取要發佈的配置文件信息,並開放接口被其餘調用者使用,先上maven配置:web
1 <dependency> 2 <groupId>org.springframework.cloud</groupId> 3 <artifactId>spring-cloud-config-server</artifactId> 4 </dependency>
一般在程序入口處添加註解 @EnableConfigServer 而後咱們還須要知道開放那些配置文件做爲配置信息來源,所以須要在application.yml文件中配置以下信息:spring
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文件夾下有兩個配置文件,以下:bootstrap
此刻咱們最簡單的配置服務就搭建好了,啓動程序並訪問以下地址:http://10.0.75.1:4020/config-server/conf1,conf0;值得注意的時候這裏用 ',' 分割了下,在瀏覽器中獲得以下兩個配置文件合併後的信息:windows
能夠去掉其中任何一個conf1或者conf0,獲得的就是對應配置文件的信息,這裏經過瀏覽器訪問的路徑規則是:http://xx.xx.xx/{application}/{profiles}/{label} label默認nullapi
一樣先來看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信息:springboot
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註冊中心(後面可能會分享關於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接口時獲得以下獲取配置成功信息