[英]How to refresh app instances using Spring cloud bus with data which isn't controlled by config server?
我正在嘗試在我的微服務應用程序中使用帶有 Kafka 的 Spring cloud bus,確實我可以使用它,但只有由 Spring cloud config server 控制的數據才會刷新!
我在我的配置服務器上使用 jdbc 后端,為了模擬我的需要,我在我的一個服務中更改了屬性文件中的一些值,在屬性表旁邊,並再次調用/monintor端點(這里提到的第 4.3 節https://www.baeldung.com/spring-cloud-bus ); 因此,只有來自屬性表的數據被更改。
這是我的配置服務器的 yml 文件
spring:
cloud:
config:
server:
jdbc:
sql: SELECT KEY,VALUE from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?
order: 1
stream:
kafka:
binder:
brokers: localhost:9092
datasource:
url: jdbc:mysql://localhost:3306/sweprofile?zeroDateTimeBehavior=convertToNull
username: 123
password: 123ertbnm
hikari:
maximum-pool-size: 10
connection-timeout: 5000
profiles:
active:
- jdbc
application:
name: configServer
這些分別是我的 Miscroservices 之一及其屬性文件的 yml 文件
spring:
datasource:
username: 123
password: 123ertbnm
url: jdbc:mysql://localhost:3306/sweprofile?zeroDateTimeBehavior=convertToNull
jpa:
properties:
hibernate:
format_sql: true
ddl-auto: none
application:
name: auth-service
cloud:
config:
discovery:
enabled: true
service-id: configServer
bus:
refresh:
enabled: true
profiles:
active: jdbc
management:
endpoints:
web:
exposure:
include: ["health","info","refresh", "bus-refresh"]
# This line is dummy data for testing purpose
ali.man = " Ola 12333"
這是來自休息控制器的快照
@RestController
@RequestMapping("/user")
@RefreshScope
public class AuthController {
private UserAuthService userAuthService;
@Value("${name}")
private String name; // changed normally
// Calling the key value mentioned in properties file after changing
@Value("${ali.man}")
private String k; // -> not changed
public AuthController(UserAuthService userAuthService) {
this.userAuthService = userAuthService;
}
@GetMapping("authTest")
public String getAuth() {
return name + k;
}
}
我錯過了什么? 為什么屬性文件中的值沒有改變? 希望我可以使用帶有Kafka的Spring cloud bus來刷新這些外部數據。
經過幾個小時的調查,我發現有一些推薦的方法。 Cloud bus 可以發送 Refresh Event,Spring Boot 有 RefreshEvent Listener 到該事件; 這就是我構建解決方案的基礎。
所以當總線發送事件時; 所有實例都將對加載的內存配置執行相同的邏輯(刷新數據)。
我用這個片段來應用這個
@Configuration
public class ReloadLookupEvent implements ApplicationListener<RefreshScopeRefreshedEvent> {
@Autowired
private CacheService cacheService;
@Override
public void onApplicationEvent(RefreshScopeRefreshedEvent event) {
cacheService.refreshLookUp();
}
}
我可以按需刷新所有其他配置,也許這是一種解決方法,但可以接受。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.