當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringCloud运行时刷新数据源相关配置
生活随笔
收集整理的這篇文章主要介紹了
SpringCloud运行时刷新数据源相关配置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
序
數據庫的相關配置,一般來說是不會頻繁變的,特別是當數據庫連接使用的是域名而不是ip地址的時候,這樣即使ip地址變化了,也不影響業務系統。這里呢,我們講一下如果真的是迫不得已的時候,有沒有不重啟就可以更改配置的方法。
思路
有很多種方案,這里我們講一下基于SpringCloud的RefreshScope的方案。
數據庫配置實例
spring:datasource:platform: postgresurl: jdbc:postgresql://192.168.99.100:5432/postgresdriverClassName: org.postgresql.Driverusername: postgrespassword: 123456validation-query: SELECT 1test-while-idle: truetest-on-borrow: trueJava配置
@Configuration public class DbConfig {@Bean@RefreshScope //refresh@Primary@ConfigurationProperties(prefix = "spring.datasource")public DataSource dataSource() {return DataSourceBuilder.create().build();} }更新
調用一下服務的refresh端點就可以了
curl -i -X POST http://localhost:9001/refresh驗證
@Component public class ScheduleTask {@AutowiredUserRepository userRepository;@Scheduled(fixedDelay = 5*1000 /**ms**/,initialDelay = 5*1000)public void testDataSourceConn() {try{System.out.println("call jdbc");userRepository.findAll();}catch (Exception e){e.printStackTrace();}} }這里跑一個定時任務,不停地調用數據查詢方法,然后中途改下密碼,然后refresh一下,看是否報錯
doc
Refresh Scope
總結
以上是生活随笔為你收集整理的SpringCloud运行时刷新数据源相关配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android trace文件分析ANR
- 下一篇: css中的一些常用选择器