文章目錄
-
-
- 1. sping-cloud config簡介
- 2. sping-cloud config 服務特點
- 3. Config-Server 服務端搭建
- 4. Config-Client 端搭建
- 5. 動态重新整理配置測試
- 6. config-server配置RSA加密
-
1. sping-cloud config簡介
微服務的體系中,配置檔案的統一管理是非常有必要的,我們需要替代人為手動維護配置檔案的責任,因為在大型的微服務體系中我們需要維護的配置成百上千份,很容易發生配置漂移。此時,Spring-Cloud Config可以給我們提供配置的統一管理和分發,以及自動化重新整理配置的需求。
2. sping-cloud config 服務特點
2.1. 分離性
Config配置中心支援将配置檔案存入本地或者無縫銜接git管理的特性能夠集中進行版本的控制和配置檔案的管理;
2.2. 抽象性
我們在擷取配置檔案時,不需要自己進行配置檔案讀取操作,可以通過http請求配配置中心提供的服務進行配置檔案讀取,并且內建bus總線可以動态IDE重新整理配置,并且重新整理所有的服務執行個體無需重新開機機器;
2.3. 集中性
所有的配置檔案資訊集中存儲在配置中心,有效的進行資料的版本統一管理。
2.4. 穩定性
作為spring旗下項目,與eureka、consul等緊密結合,可實作高可用部署,降低服務奔潰的風險,實作拉取一份配置檔案本地緩存等特性來降低風險,保證了高可用和備援。
3. Config-Server 服務端搭建
注:本文項目采用idea工具進行搭建
- 使用idea自身的spring initializr進行項目的初始化,內建git使用的rabbitmq請自行搜尋安裝,如果是mac,請直接使用brew install rabbitmq即可。
- 初始化完成項目之後進行pom檔案導入
<!-- config-server 啟動引入 -->
<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 服務注冊 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 消息總線 配置動态配置中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
- 修改application.yml檔案,添加如下配置:
spring:
application:
name: config-server
# profiles:
# active: native #開啟本地配置 預設為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: #個人git位址
search-paths: '{application}'
username: # 你個人的git賬戶
password: # git密碼
default-label: master
rabbitmq:
host: localhost
port: 5672
username: guest #預設密碼
password: guest #預設密碼
# 安全認證
security:
user:
name: luhanlin
password: ***
## 暴露所有監控端口 主要暴露動态重新整理接口
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
- 修改bootstrap.yml檔案,連結eureka-config,添加如下配置:
#服務注冊中心配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #,http://xxxxx:8761/eureka/
# 指定多少秒從注冊中心擷取一次執行個體服務清單 預設 30秒
# 減少值可以解決服務注冊慢問題,但一般不要設定太小
registry-fetch-interval-seconds: 20
instance:
# 擷取執行個體ip位址
prefer-ip-address: true
# 心跳包發送時間 預設 30秒
lease-renewal-interval-in-seconds: 60
# 最後一次心跳時間間隔以下值之後清楚執行個體清單中的值,不應該小于心跳檢測時間 預設90秒
lease-expiration-duration-in-seconds: 100
# instance-id: config-server
- 最後在Config服務啟動執行個體上面添加一下注解:
@EnableDiscoveryClient // 開啟服務注冊
@EnableConfigServer // 開啟配置中心服務端
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
最後啟動Eureka—server,在啟動Config-Server就可以了,注意:rabbitmq服務需要啟動,不然會報錯。
4. Config-Client 端搭建
- 此處我以Demo-Service項目為例。
- 首先我們建構一個maven工程,初始化完成後進行pom引入(沒有加入boot監控依賴):
<!-- Eureka用戶端啟動類 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- Config用戶端啟動類 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<!-- 消息總線 配置動态配置中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
- 修改我們的配置檔案Bootstrap.yml, 配置如下:
spring:
application:
name: demo
# 配置中心服務的位址
cloud:
config:
discovery:
# 預設false,設為true表示使用注冊中心中的config-server配置而不自己配置config-server的uri
enabled: true
service_id: config-server
fail-fast: true
name: demo
profile: dev # 要讀取的配置檔案profile屬性
# 使用git管理配置檔案時指定
label: master
username: luhanlin
password: ***
# 指定服務注冊中心的位置。
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
hostname: localhost
preferIpAddress: true
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
- 配置完後需要在Client啟動類上加上一下注解:
@RefreshScope # GIT動态重新整理注解
@EnableDiscoveryClient # 服務發現
5. 動态重新整理配置測試
// 此類進行配置檔案的資訊讀取,**使用對象進行測試會更加準确**
@Data
public class CustomBean {
private String id;
private String name;
private String version;
}
@Configuration // 配置類,如不熟悉可以先行進行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,位于個人倉庫中)添加如下配置:
custom:
bean:
id: 100
name: luhanlin
version: 1.0.1
- 以上配置完成後通路 http://localhost:8080/test/hello 傳回:
{
"code": "I0000",
"msg": "請求成功",
"data": "1.0.1"
}
- 修改配置倉庫中的配置檔案,并送出到push到遠端git倉庫
custom:
bean:
id: 100
name: luhanlin
version: 1.0.9
- 這是再次通路http://localhost:8080/test/hello,發現傳回資訊沒。使用POST請求通路http://localhost:7001/actuator/bus-refresh(老版使用fresh/refresh)進行屬性的重新整理,發現403無法通路請求,因為我在配置中添加了security配置需要進行安全認證,而新版的boot和cloud中需要Config-server中進行以下配置才行:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
/**
* 高版本的丢棄了
*
* security:
* basic:
* enabled: true
*
* 配置,應該使用以下方式開啟
*
* @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();
}
}
- 完成後調用http://localhost:7001/actuator/bus-refresh,接口請求完成後可以看到控制台進行了配置重新整理,再次通路http://localhost:8080/test/hello接口傳回:
{
"code": "I0000",
"msg": "請求成功",
"data": "1.0.9"
}
- 配置成功
6. config-server配置RSA加密
- 由于我們配置檔案時很多需要加密的資訊不宜暴露給外界,需要進行各種加密操作,其中spring-cloud提供了對稱加密和RSA非對稱加密的形式,此處我們使用RSA加密的方式,通過Config暴露的entrypt接口進行加密,我們隻需要在配置檔案中使用{cipher},如:“{cipher}shdshdswwhxxxxxxxx66x65xsdsdsd”,其中在yml配置檔案中一定要使用雙引号包含起來,不然會出錯,詳細的RSA配置大家可以自行搜尋部落格進行配置。
本文github代碼位址
spring-cloud 基礎子產品搭建 – 歡迎指正