Spring cloud Config 配置中心+ Bus消息总线

一、简介

Spring cloud Config 为分布式系统内部服务提供外部配置,用于集中管理环境中的外部配置。

二、实战

2.1 config server

  • pom.xml
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>
  • application.yml
server:
  port: 3344
spring:
  application:
    name: cloud-config
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/sujinfengee/springcloud-config # github上面仓库名称
          search-paths:
            - springcloud-config
          default-label: main

# eureka
eureka:
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
  client:
    service-url:
      defaultZone: http://localhost:7071/eureka/,http://localhost:7072/eureka/
  • Application
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
    }
}

2.2 config client

  • pom.xml
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>
  • bootstrap.yml
server:
  port: 3355
spring:
  application:
    name: config-client
  cloud:
    config: # 从 main 分支上 config.dev.yml的配置文件读物
      label: main   # 从配置中心 main 分支下
      name: config  # 配置文件名称
      profile: dev  # 配置文件环境,文件后缀名称
      uri: http://localhost:3344

# eureka 注册中心
eureka:
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
  client:
    service-url:
      defaultZone: http://localhost:7071/eureka/,http://localhost:7072/eureka/

# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"
  • application
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
public class ConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}
  • controller
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope  // 动态刷新必须加该注解
public class ConfigController {
    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}

刷新配置方式

动态刷新:向客户端发送 post 请求 http://localhost:3355/actuator/refresh

三、Bus 动态全局广播刷新

方式一:通过消息总线向客户端发送刷新通知,是其他客户端进行主动刷新

方式二:通过消息总线向配置中心发送刷新通知,使客户端进行被动刷新

遵循单一模块,单一功能原则,建议使用方式二,方式二配置如下

3.1 启动RabbitMq

3.2 pom.xml

<!-- config-client 和 server 都要增加该依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

3.3 yml

  • 服务端 application.yml 增加如下配置:
spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: user
    password: password

management:
  endpoints:
    web:
      exposure:
        include: 'bus-refresh' # 暴露bus 刷新配置端点
  • 客户端 bootstrap.yml 增加如下配置:
spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: user
    password: password

# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

3.4 动态刷新

  • 广播式

向 config 服务端发送 post 请求 http://localhost:3344/actuator/bus-refresh

  • 定点式

向 config 服务端发送 post 请求 http://localhost:3344/actuator/bus-refresh/config-client:3355

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值