一、配置中心應用(Git)
1、如果在GitHub上建立的倉庫是私有的,那么還要加上spring.cloud.config.server.git.username和spring.cloud.config.server.git.password 這兩個配置
2、springcloud config 的URL與配置文件的映射關系如下:
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
3、如果github上建立的目錄下的文件為application-config-dev.yml,那么當啟動配置中心服務器端時,可以通過http://localhost:9006/config/application-config-dev.yml訪問配置文件,如果訪問成功則表示配置中心搭建成功。這里的config是分支名稱
第一步:配置中心服務端
1、依賴配置
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
2、配置文件
spring:
application:
#應⽤名稱,會在Eureka中作為服務的id標識(serviceId)
name: config-server9006
cloud:
config:
server:
git:
uri: git@gitee.com:niunafei1/springcloud.git
username: niunafei0315@163.com
password:
label: config
eureka:
client:
#eureka server的路徑
serviceUrl:
#注冊單實例只需要寫一台服務器即可
#集群模式下,也需要寫其它 http://Server其他服務地址:其他服務端口/eureka,如果多個服務需要使用逗號分隔
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
instance:
#使⽤ip注冊,否則會使⽤主機名注冊了(此處考慮到對⽼版本的兼容,新版本經過實驗都是ip)
prefer-ip-address: true
#⾃定義實例顯示格式,加上版本號,便於多版本管理,注意是ip-address,早期版本是ipAddress
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
3、在啟動類上添加注解,@EnableDiscoveryClient【@EnableDiscoveryClient注解可替換為@EnableEurekaClient】和@EnableConfigServer
package city.albert; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.config.server.EnableConfigServer; /** * @author niunafei * @function * @email niunafei0315@163.com * @date 2020/9/22 11:44 PM */ @SpringBootApplication @EnableDiscoveryClient @EnableConfigServer public class ConfigServer9006 { public static void main(String[] args) { SpringApplication.run(ConfigServer9006.class, args); } }
訪問:http://localhost:9006/config/application-config-dev.yml
第二步:配置中心客戶端
1、引入依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency>
2、配置文件設置,文件名為bootstrap.yml
在spring boot中系統文件名bootstrap.yml優先被加載使用,需要在系統加載application.yml之前初始化獲取配置文件。
spring:
application:
#應⽤名稱,會在Eureka中作為服務的id標識(serviceId)
name: gateway-server9002
cloud:
config:
name: application-config #配置⽂件名稱
profile: dev #后綴名稱
label: config #分⽀名稱
uri: http://localhost:9006 #ConfigServer配置中⼼地址
3、使用可以用@Value("${spring.port}")注入即可
二、配置中心的手動刷新
實現手動刷新不⽤重啟微服務,只需要⼿動的做⼀些其他的操作(訪問⼀個地址/refresh)刷新,之后再訪問即可此時,客戶端取到了配置中⼼的值,但當我們修改GitHub上⾯的值時,服務端(Confifig Server)能實時獲取最新的值,但客戶端(Confifig Client)讀的是緩存,⽆法實時獲取最新值。Spring Cloud已 經為我們解決了這個問題,那就是客戶端使⽤post去觸發refresh,獲取最新數據。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
management:
endpoints:
web:
exposure:
include: "*"
三、配置中心的自動刷新(Spring Cloud Confifig+Spring Cloud Bus 實現)
MQ消息代理,我們還選擇使⽤RabbitMQ,ConfifigServer和ConfifigClient都添加都消息總線的⽀持以及與RabbitMq的連接信息
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
spring:
rabbitmq:
host: 127.0.0.1
password: guest
port: 5672
username: guest
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
b、暴露服務
management:
endpoints:
web:
exposure:
include: "*"
四、配置中心應用(數據庫)
<!--連接msql數據庫相關jar包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency>
2、改動配置文件
spring:
application:
name: config-server-jdbc
profiles:
active: jdbc
cloud:
config:
server:
default-label: dev
jdbc:
sql: SELECT akey , avalue FROM config_server where APPLICATION=? and APROFILE=? and LABEL=?
# mysql 屬性配置
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test
username: root
password: root
這里主要講下連接配置信息
(1)spring.profiles.active=jdbc,自動實現JdbcEnvironmentRepository。
(2)sql語句自定義,否則會默認為“SELECT KEY, VALUE from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?”,具體可以參考JdbcEnvironmentRepository實現。
(3)本人數據庫建表為config_server,由於key,value和profile是mysql關鍵字,所以我都在最前面加了a。當然表名字段名都可以自定義。
(4) {application} 對應客戶端的"spring.application.name"屬性;
{aprofile} 對應客戶端的 "spring.profiles.active"屬性(逗號分隔的列表); 和
{label} 對應服務端屬性,這個屬性能標示一組配置文件的版本.
(5)只要select出來是兩個字段,框架會自動包裝到environment的map<key,value>。