当前位置: 首页 > 知识库问答 >
问题:

如何使用Spring云总线刷新不受配置服务器控制的数据的应用实例?

濮阳旺
2023-03-14

我正在尝试在我的微服务应用程序中使用带有Kafka的Spring云总线,确实我可以使用它,但只有由Spring云配置服务器控制的数据被刷新了!

我在配置服务器上使用jdbc后端,为了模拟我的需要,我在我的一个服务的属性文件中更改了一些值,在属性表旁边,然后再次调用/monintorendpoint(这里提到了第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

这些是分别用于我的一个MissRoseServices及其属性文件的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"

这是Rest控制器的快照

@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云总线来刷新这些外部数据。

共有1个答案

孟楷
2023-03-14

经过几个小时的调查,我发现有一些推荐的方法。云总线可以发送刷新事件,Spring boot具有该事件的刷新事件监听器;这就是我构建解决方案的基础。

所以当事件由总线发送时;所有实例将对加载的内存配置执行相同的逻辑(刷新数据)。

我用这个片段来应用这个

@Configuration
public class ReloadLookupEvent implements ApplicationListener<RefreshScopeRefreshedEvent> {
    @Autowired
    private CacheService cacheService;

    @Override
    public void onApplicationEvent(RefreshScopeRefreshedEvent event) {
        cacheService.refreshLookUp();     
    }

}

我可以根据需要刷新所有其他配置,也许这是一种解决方法,但可以接受。

 类似资料:
  • 据我所知,当Spring Cloud Config Server配置更改时,可以通过Spring Cloud Bus在微服务之间传播刷新事件。然后,这会触发通知的微服务从Spring Cloud Config Server中提取最新配置。 这种方法的扩展性好吗?我的意思是,我们可以有1000个微服务实例,如果一些全局配置发生变化,那么所有这1000个微服务都会在同一时刻尝试从配置服务器获取配置。这

  • 我试图找到一些Spring Cloud数据流和Azure服务总线设置的示例。. 我找到了https://github.com/microsoft/spring-cloud-azure/tree/master/spring-cloud-azure-stream-binder/spring-cloud-azure-servicebus-topic-stream-binder,但它仍然在RC中,我没有看

  • 我试图在应用程序启动时从配置服务器读取log4j2配置。 bootstrap.yml 应用程序似乎在启动期间得到了正确的配置,因为我看到了适当的日志级别。然而,自动配置似乎不起作用。当我更改日志记录器的日志级别时,它似乎没有在monitorInterval通过后从配置服务器读取更新的配置。我已将monitorInterval设置为10秒。根据文件,最小间隔应为5秒。如果我指向本地驱动器上的文件,而

  • 我已经用config server和github支持的外部配置文件配置了我的应用程序。当我在cloud foundry中有一个应用程序实例时,它工作得很好。 但对于多个实例,据说要实现spring云总线,以将外部配置更改应用于所有实例。为此,我将配置服务器与Pivotal Cloud foundry上可用的rabbit MQ实例绑定。增加了Spring。云起动机。公共汽车我的构建中的amqp ja

  • 我的Spring云配置客户端依赖于Spring。云起动机。公共汽车amqp,但它仍然没有启用总线/刷新endpoint 我在配置客户端应用程序中有这些依赖项,但仍然没有启用总线/刷新、总线/环境。 请让我知道我的客户申请中缺少什么。 注: 我尝试在应用程序中设置这些指标。yml或应用程序。属性,因为这些属性由总线自动配置用来启用endpoint。 在我的Spring Cloud Config Se

  • 我想在我的项目中使用Spring Cloud Bus来动态更新配置。我们的环境中已经有Apache ActiveMQ。有没有可能使用ActiveMQ而不是RabbitMQ作为代理?它们都是一种AMQP服务器。 我在pom.xml中设置依赖项如下: 它看起来像一个标准的AMQP启动器,不限于RabbitMQ。但是,当配置服务器启动时,以下错误日志会提示: