springcloud kafka 分布式配置中心管理

  1. 配置管理服务端   更新配置信息需要调用接口地址(post请求)/bus/refresh

1.1配置管理服务端需要的jar

<dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-config-server</artifactId>

</dependency>

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

<!-- 注册中心-->

<dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-eureka</artifactId>

</dependency>

<!-- kafka消息中心 -->

<dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-bus-kafka</artifactId>

</dependency>

<dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-config-monitor</artifactId>

</dependency>

1.2.在bootstrap.yml中添加

spring:

  application:

    name: framework-config-service

#配置文件设置成本地模式

  profiles:

    active: native

#开启消息总线刷新

  cloud:

    bus:

      enabled: true

    config:

      server:

        native:

          search-locations: F:/config/

#设置消息总线的地址

    stream:

      kafka:

        binder:

          brokers: 10.1.110.45:9092,10.1.110.44:9092

          zkNodes: 10.1.110.45:2181,10.1.110.44:2181

server:

  port: 8899

#关闭安全验证

management:

  security:

    enabled: false

#注册中心

eureka:

  client:

    service-url:http://localhost:8761/eureka/

 

1.3.启动项设置

@SpringBootApplication

@EnableDiscoveryClient

@EnableConfigServer

@RefreshScope

public class ConfigBoostrap {

    public static void main(String[] args) {

        ApplicationContext applicationContext = SpringApplication.run(ConfigBoostrap.class, args);

    }

}

 

  1. 配置文件管理客户端配置

2.1.需要的jar

<!-- kafka消息中心 -->

<dependency>

   <groupId>org.springframework.cloud</groupId>

   <artifactId>spring-cloud-starter-bus-kafka</artifactId>

</dependency>

<!-- 配置文件-->

<dependency>

   <groupId>org.springframework.cloud</groupId>

   <artifactId>spring-cloud-config-client</artifactId>

</dependency>

<!--信息采集 -->

<dependency>

   <groupId>org.springframework.boot</groupId>

   <artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

2.2.消息总线的配置需要在bootstrap.yml中设置

#配置消息总线

spring:

  cloud:

    stream:

      kafka:

        binder:

          brokers: 10.1.110.45:9092,10.1.110.44:9092

          zkNodes: 10.1.110.45:2181,10.1.110.44:2181

#启动配置文件中心读取

    config:

      discovery:

        enabled: true

        service-id: framework-config-service

      profile: dev

  application:

    name: framework-common-service

#注册中心

eureka:

  client:

    service-url:

      defaultZone: http://localhost:8761/eureka/

management:

  security:

    enabled: false

server:

  port: 8760

2.3.在启动项添加@EnableDiscoveryClient注解

 

@SpringBootApplication

@EnableDiscoveryClient

@EnableFeignClients

@EnableHystrixDashboard

@EnableCircuitBreaker

//@MapperScan(basePackages = "com.tzbank.common.dao")

public class CommonBootstrap {

   public static void main(String[] args) {

      ApplicationContext applicationContext = SpringApplication.run(CommonBootstrap.class, args);

      SpringContextUtils.setApplicationContext(applicationContext);

   }

}

2.4.需要动态刷新的数据,取值的写法

需要在有刷新的数据的类或者方法上加上

@RefreshScope注解

取值的方法有:

import org.springframework.core.env.Environment;中取值

@Autowired

Environment environment;

或者直接绑定${value}中的value是对应配置文件的值

@Vlue(“${value}”)

Private String value;

 

转载于:https://my.oschina.net/youkun/blog/1834460

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值