1. sping-cloud config簡介
微服務(wù)的體系中酝静,配置文件的統(tǒng)一管理是非常有必要的罩息,我們需要替代人為手動(dòng)維護(hù)配置文件的責(zé)任,因?yàn)樵诖笮偷奈⒎?wù)體系中我們需要維護(hù)的配置成百上千份幸撕,很容易發(fā)生配置漂移打月。此時(shí),Spring-Cloud Config可以給我們提供配置的統(tǒng)一管理和分發(fā)湿硝,以及自動(dòng)化刷新配置的需求薪前。
2. sping-cloud config 服務(wù)特點(diǎn)
2.1. 分離性
Config配置中心支持將配置文件存入本地或者無縫銜接git管理的特性能夠集中進(jìn)行版本的控制和配置文件的管理;
2.2. 抽象性
我們在獲取配置文件時(shí)关斜,不需要自己進(jìn)行配置文件讀取操作示括,可以通過http請求配配置中心提供的服務(wù)進(jìn)行配置文件讀取,并且集成bus總線可以動(dòng)態(tài)IDE刷新配置痢畜,并且刷新所有的服務(wù)實(shí)例無需重啟機(jī)器垛膝;
2.3. 集中性
所有的配置文件信息集中存儲在配置中心,有效的進(jìn)行數(shù)據(jù)的版本統(tǒng)一管理裁着。
2.4. 穩(wěn)定性
作為spring旗下項(xiàng)目繁涂,與eureka、consul等緊密結(jié)合二驰,可實(shí)現(xiàn)高可用部署扔罪,降低服務(wù)奔潰的風(fēng)險(xiǎn),實(shí)現(xiàn)拉取一份配置文件本地緩存等特性來降低風(fēng)險(xiǎn)桶雀,保證了高可用和冗余矿酵。
3. Config-Server 服務(wù)端搭建
注:本文項(xiàng)目采用idea工具進(jìn)行搭建
- 使用idea自身的spring initializr進(jìn)行項(xiàng)目的初始化,集成git使用的rabbitmq請自行搜索安裝矗积,如果是mac全肮,請直接使用brew install rabbitmq即可。
- 初始化完成項(xiàng)目之后進(jìn)行pom文件導(dǎo)入
<!-- config-server 啟動(dòng)引入 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- security 安全控制 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- eureka 服務(wù)注冊 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 消息總線 配置動(dòng)態(tài)配置中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
- 修改application.yml文件棘捣,添加如下配置:
spring:
application:
name: config-server
# profiles:
# active: native #開啟本地配置 默認(rèn)為git
# cloud:
# config:
# server:
# native:
# search-locations: classpath:config-repo
## git管理配置
cloud:
config:
retry:
initial-interval: 3000
multiplier: 1.5
max-interval: 20000
max-attempts: 6
server:
git:
uri: #個(gè)人git地址
search-paths: '{application}'
username: # 你個(gè)人的git賬戶
password: # git密碼
default-label: master
rabbitmq:
host: localhost
port: 5672
username: guest #默認(rèn)密碼
password: guest #默認(rèn)密碼
# 安全認(rèn)證
security:
user:
name: luhanlin
password: ***
## 暴露所有監(jiān)控端口 主要暴露動(dòng)態(tài)刷新接口
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
- 修改bootstrap.yml文件,鏈接eureka-config辜腺,添加如下配置:
#服務(wù)注冊中心配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #,http://xxxxx:8761/eureka/
# 指定多少秒從注冊中心獲取一次實(shí)例服務(wù)列表 默認(rèn) 30秒
# 減少值可以解決服務(wù)注冊慢問題,但一般不要設(shè)置太小
registry-fetch-interval-seconds: 20
instance:
# 獲取實(shí)例ip地址
prefer-ip-address: true
# 心跳包發(fā)送時(shí)間 默認(rèn) 30秒
lease-renewal-interval-in-seconds: 60
# 最后一次心跳時(shí)間間隔以下值之后清楚實(shí)例列表中的值乍恐,不應(yīng)該小于心跳檢測時(shí)間 默認(rèn)90秒
lease-expiration-duration-in-seconds: 100
# instance-id: config-server
- 最后在Config服務(wù)啟動(dòng)實(shí)例上面添加一下注解:
@EnableDiscoveryClient // 開啟服務(wù)注冊
@EnableConfigServer // 開啟配置中心服務(wù)端
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
最后啟動(dòng)Eureka—server评疗,在啟動(dòng)Config-Server就可以了,注意:rabbitmq服務(wù)需要啟動(dòng)茵烈,不然會報(bào)錯(cuò)百匆。
4. Config-Client 端搭建
此處我以Demo-Service項(xiàng)目為例。
首先我們構(gòu)建一個(gè)maven工程呜投,初始化完成后進(jìn)行pom引入(沒有加入boot監(jiān)控依賴):
<!-- Eureka客戶端啟動(dòng)類 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- Config客戶端啟動(dòng)類 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<!-- 消息總線 配置動(dòng)態(tài)配置中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
- 修改我們的配置文件Bootstrap.yml加匈, 配置如下:
spring:
application:
name: demo
# 配置中心服務(wù)的地址
cloud:
config:
discovery:
# 默認(rèn)false存璃,設(shè)為true表示使用注冊中心中的config-server配置而不自己配置config-server的uri
enabled: true
service_id: config-server
fail-fast: true
name: demo
profile: dev # 要讀取的配置文件profile屬性
# 使用git管理配置文件時(shí)指定
label: master
username: luhanlin
password: ***
# 指定服務(wù)注冊中心的位置。
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
hostname: localhost
preferIpAddress: true
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
- 配置完后需要在Client啟動(dòng)類上加上一下注解:
@RefreshScope # GIT動(dòng)態(tài)刷新注解
@EnableDiscoveryClient # 服務(wù)發(fā)現(xiàn)
5. 動(dòng)態(tài)刷新配置測試
// 此類進(jìn)行配置文件的信息讀取雕拼,**使用對象進(jìn)行測試會更加準(zhǔn)確**
@Data
public class CustomBean {
private String id;
private String name;
private String version;
}
@Configuration // 配置類纵东,如不熟悉可以先行進(jìn)行spring-b
public class BusinessConfig {
@Bean
@ConfigurationProperties(prefix="custom.bean")
public CustomBean initCustomBean(){
log.info(">>>>>>>>>>> CustomBean >>>>>>>>>>>>>>> 配置成功!!!");
return new CustomBean();
}
}
@RestController // 對外api提供,最后返回配置中信息
@RequestMapping(value = "/test")
public class TestController {
@Autowired
private CustomBean customBean;
@GetMapping(value = "/hello")
public ResultInfo hello(){
return ResultUtil.success(customBean.getVersion());
}
}
// 配置文件(demo-dev.yml,位于個(gè)人倉庫中)添加如下配置:
custom:
bean:
id: 100
name: luhanlin
version: 1.0.1
- 以上配置完成后訪問 http://localhost:8080/test/hello 返回:
{
"code": "I0000",
"msg": "請求成功",
"data": "1.0.1"
}
- 修改配置倉庫中的配置文件,并提交到push到遠(yuǎn)程git倉庫
custom:
bean:
id: 100
name: luhanlin
version: 1.0.9
- 這是再次訪問http://localhost:8080/test/hello悲没,發(fā)現(xiàn)返回信息沒篮迎。使用POST請求訪問http://localhost:7001/actuator/bus-refresh(老版使用fresh/refresh)進(jìn)行屬性的刷新男图,發(fā)現(xiàn)403無法訪問請求示姿,因?yàn)槲以谂渲弥刑砑恿藄ecurity配置需要進(jìn)行安全認(rèn)證,而新版的boot和cloud中需要Config-server中進(jìn)行以下配置才行:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
/**
* 高版本的丟棄了
*
* security:
* basic:
* enabled: true
*
* 配置逊笆,應(yīng)該使用以下方式開啟
*
* @param http
* @throws Exception
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
// Configure HttpSecurity as needed (e.g. enable http basic).
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
http.csrf().disable();
//注意:為了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 這種方式登錄,所以必須是httpBasic,
// 如果是form方式,不能使用url格式登錄
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}
- 完成后調(diào)用http://localhost:7001/actuator/bus-refresh栈戳,接口請求完成后可以看到控制臺進(jìn)行了配置刷新,再次訪問http://localhost:8080/test/hello接口返回:
{
"code": "I0000",
"msg": "請求成功",
"data": "1.0.9"
}
- 配置成功
6. config-server配置RSA加密
- 由于我們配置文件時(shí)很多需要加密的信息不宜暴露給外界难裆,需要進(jìn)行各種加密操作子檀,其中spring-cloud提供了對稱加密和RSA非對稱加密的形式,此處我們使用RSA加密的方式乃戈,通過Config暴露的entrypt接口進(jìn)行加密褂痰,我們只需要在配置文件中使用{cipher},如:“{cipher}shdshdswwhxxxxxxxx66x65xsdsdsd”,其中在yml配置文件中一定要使用雙引號包含起來症虑,不然會出錯(cuò)缩歪,詳細(xì)的RSA配置大家可以自行搜索博客進(jìn)行配置。
本文github代碼地址
spring-cloud 基礎(chǔ)模塊搭建 -- 歡迎指正