spring boot / cloud (十七) 快速搭建注冊中心和配置中心
本文將使用spring cloud的eureka和config server來搭建.
然后搭建的模式,有很多種,本文主要聊的是將注冊中心和配置中心整合成一個服務的方式.
對於其他方式,如果有同學感興趣,還請自行百度,謝謝.
為什么將注冊中心和配置中心整合在一起?
其實整合在一起和分開,在使用層面上並沒有太大的區別,主要就是節省資源,啟動一個服務就夠了
開始搭建
添加pom依賴
<!-- eureka server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!-- config server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- amqp rabbitmq -->
<dependency>
<groupId>org.itkk.udf</groupId>
<artifactId>udf-starter-amqp-rabbitmq</artifactId>
</dependency>
以上為主要的依賴,引入了eureka server和config server以及amqp
amqp主要是為了支持config server的在runtime刷新各個子服務的配置文件的這個特性
pom關鍵配置
<resources>
<resource>
<directory>${project.basedir}/src/main/resources/</directory>
<filtering>true</filtering>
</resource>
</resources>
以上配置可以使我們在待會的配置文件中引用maven的上下文屬性,通過${xxx}的方式
<!-- package -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${springboot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
添加打包插件,會將這個應用打包成一個可執行的jar包(內置容器,可直接啟動應用)
編寫啟動類
@SpringCloudApplication
@EnableEurekaServer
@EnableConfigServer
public class UdfEurekaConfigServerDemoApplication extends BaseApplication {
/**
* 描述 : spring boot的入口
*
* @param args 參數
*/
public static void main(String[] args) {
SpringApplication.run(UdfEurekaConfigServerDemoApplication.class, args);
}
}
標記@SpringCloudApplication,@EnableEurekaServer,@EnableConfigServer開啟注冊中心和配置中心
同時這里繼承了BaseApplication,這個類是前面博客中udf-starter-core中的一個類,此類做了一些簡單的封裝,詳情大家可查閱
bootstrap.properties主要配置文件詳解
info.build.group=${project.groupId}
info.build.artifact=${project.artifactId}
info.build.version=${project.version}
info.build.name=${project.name}
info.build.time=${project.build.date}
info.build.encoding=${project.build.sourceEncoding}
info.build.java.source=${java.version}
info.build.java.target=${java.version}
以上配置,主要定義了應用的一些基礎信息,並且這些基礎信息都是通過${xxx}的方式直接從maven變量中獲取的
這些信息在應用啟動了之后,可以通過http://localhost:port/info訪問
注意,這里的${project.build.date}是UTC時間,不是北京時間,所以查看的時候,要+8小時
spring.application.name=${project.name}
server.port=${server.port}
management.security.enabled=false
spring.profiles.active=${profiles.activation}
以上配置主要設定了應用名稱(使用項目的名稱),應用端口(從pom中獲取),以及profiles
bootstrap-xxx.properties主要配置文件詳解
security.user.name=admin
security.user.password=123456
設置應用的basic認證用戶名和密碼
eureka.instance.preferIpAddress=true
eureka.client.enabled=true
eureka.client.serviceUrl.defaultZone=http://admin:123456@127.0.0.1:${server.port}/eureka/
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
配置eureka實例和客戶端信息,注意的是,因為這里是講config server整合到注冊中心上了,所以這里必須要將自身也注冊到eureka上,否則,其他應用無法找到config server
spring.cloud.config.server.prefix=config-server
spring.cloud.config.server.git.uri=https://git.oschina.net/wangkang/udf-sample.git
spring.cloud.config.server.git.searchPaths=udf-config-hub/{application}
spring.cloud.config.server.git.defaultLabel=master
spring.cloud.config.server.git.forcePull=true
這里配置了config server的主要信息 ,
首先prefix是為了配置中心的上下文根和注冊中心區分開來
然后在searchPaths中,spring cloud為我們預留了{application(應用名)}/{profile(環境)}/{label(分支)}這三個變量
大家可以根據這3個變量,靈活的組合遠端git倉庫中目錄的結構,如下 :
usf-server-a
-> usf-server-a-dev.properties
-> usf-server-a-qa.properties
usf-server-b
-> usf-server-b-dev.properties
-> usf-server-b-qa.properties
在這里的話,我只使用了{application}
spring.rabbitmq.host=itkk.org
spring.rabbitmq.port=5672
spring.rabbitmq.username=dev_udf-sample
spring.rabbitmq.password=1qazxsw2
spring.rabbitmq.virtual-host=/dev_udf-sample
spring.rabbitmq.template.retry.enabled=true
以上配置了消息中間件rabbitmq的信息
.properties和.yml的區別 ?
本質上沒有什么區別 , yml格式是屬性結構的 , 可能對於某些同學來說 , 感覺更易於閱讀 .
不過我不喜歡(哈哈) , 習慣用properties了 , 所以本文全部使用的是.properties文件進行配置的 , 此格式的文件等價於.yml , 如有偏好yml的同學 , 可自行轉換
application.properties和bootstrap.properties的區別 ?
首先共同點 , 他們都可以用來配置參數 .
但是bootstrap的加載優先級高於application
在spring cloud應用中,官方也推薦使用bootstrap來存放配置
所以本文以及后續的配置項都會存放在bootstrap.properties中
如何區分profiles,來加載不同環境的配置文件 ?
在spring boot的中是通過spring.profiles.active屬性來設置環境的,默認為dev
然后spring boot預先約定配置文件通過"-"分隔,"-"之后,".properties"之前,之間的部分就是profiles,例如 :
bootstrap.properties
bootstrap-dev.properties
bootstrap-qa.properties
bootstrap-xxxx.properties
注意,沒有帶profiles的配置文件,稱為默認配置,不管什么環境下,都是必然會被加載的.
然后在啟動應用的時候,指定profiles即可,如下 :
java -server -jar demo-1.0.jar --spring.profiles.active=qa
如何動態刷新配置 ?
可在任何config client或者config service上執請求如下的HTTP服務 :
POST http://localhost:port/bus/refresh
那么,config server就會通過消息中間件rabbitmq將配置文件更新的事件通知到各個微服務中了.
結束
今天跟大家分享了整個注冊中心和配置中心的樣例和主要的配置,相關完整的項目大家可在udf-sample中的udf-eureka-config-server-demo中查閱
歡迎大家的意見跟建議
代碼倉庫 (博客配套代碼)
想獲得最快更新,請關注公眾號