spring cloud實現熱更新功能


三步走:

1. 將配置文件中心化

目標:配置文件統一放在config-server項目中

配置文件更新后,重啟config-server服務及調用服務如core服務獲取最新配置信息

 

其實這樣只是完成了配置文件中心化,由於還要重啟調用服務,未實現熱更新功能

config源碼:https://gitee.com/constfafa/config-server-demo01-config

core源碼:https://gitee.com/constfafa/config-server-demo01-core

 

2. 實現熱更新功能

目標:配置文件更新后,重啟config-server服務,無需重啟調用服務

 

 

 

Config部署完成后調用/bus/refresh

@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {

	private static final Logger LOGGER = LoggerFactory.getLogger(ConfigApplication.class);

	public static void main(String[] args) {

		SpringApplication.run(ConfigApplication.class, args);
		String url = "http://192.168.211.104:8888/bus/refresh";
		try {
			HttpResponse httpResponse = Request.Post(url).execute().returnResponse();
			int statusCode = httpResponse.getStatusLine().getStatusCode();
			if (statusCode == 200) {
				LOGGER.info("配置文件更新推送成功");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

注意:這里是直接通過http發送post請求的方式實現更新,還有一種方法是git hook的方式。

其中url應該配置到配置文件中

 

調用服務core使用@RefreshScope注解

其起作用的機制是@Configuration中顯式聲明的bean在再次使用的時候會去顯式調用其放在config服務器上的core-dev.properties

@Refresh與@RestController以及@Value一同使用

 

@Refresh與@Configuration及@Bean一同使用

 

當更新config中core的配置文件core-dev.properties並重新啟動后,會看到core的日志顯示

 

而調用服務core無需重新啟動

這樣其實就比較好的實現了熱更新

 

rabbitmq顯示為

 

 

 

在這里使用了rabbitmq作為消息隊列,也可以使用kafka起到同樣的作用

config源碼:https://gitee.com/constfafa/config-server-demo02-config

core源碼:https://gitee.com/constfafa/config-server-demo02-core

 

3. 加入eureka,實現服務發現

在第二步中,存在一個不足,就是配置中服務還是直接配置的url,如果加入eureka,實現服務發現,那就非常好了

 

 

 

 

config的配置文件中配置eureka

eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://192.168.211.105:8761/eureka/

core的配置文件中配置eureka,並通過eureka服務發現機制發現config-server服務

spring:
  application:
    name: core
  profiles:
    active: dev
  cloud:
    inetutils:
      ignored-interfaces[0]: eth0
      default-ip-address: 192.168.211.107
    config:
      #uri: http://192.168.211.104:8888
      name: core #對應core-dev.properties文件中core部分
      fail-fast: true
      password: root
      username: user
      discovery:
        enabled: true
        service-id: config-server

eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://192.168.211.105:8761/eureka/

這樣config-server更換地址或者集群化都不會影響core服務

部署兩個服務之后的Eureka

 

注:

 

在進行eureka配置的時候有幾個注意事項:

1. 由於使用的是虛擬機,兩個網卡:eth0采用NAT,保證可以連接互聯網;eth1采用HOST-ONLY,保證虛擬機互通。

所以設置spring.cloud.inetutils.ignored-interfaces[0]=eth0

並設置指定ip spring.cloud.inetutils.default-ip-address:192.168.211.107

參考:

springCloud(6):Eureka的自我保護模式、多網卡下的IP選擇、Eureka的健康檢查

最終源碼:

config:https://gitee.com/constfafa/config-server-demo03-config

core:https://gitee.com/constfafa/config-server-demo03-core

eureka-server:https://gitee.com/constfafa/eureka-server

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM