集中配置组件SpringCloudConfig和消息总线组件SpringCloudBus管理配置文件

为什么要使用SpringCloudConfig和SpringCloudBus

在分布式系统中,配置文件很多,不太方便管理;于是就出现了配置中心组件SpringCloudConfig,来帮助我们管理配置文件,实时更新。它支持将配置文件存放在git中;而SpringCloudBus可以让我们的服务不用重启,配置就能修改。

服务器

1. pom依赖

 <!--config-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
         <!-- Spring Cloud Bus配置 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-bus</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
        </dependency>

2. 配置文件

server:
  port: 12000
spring:
  application:
    name: tensquare-config
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/hanmtou/tensquare-config.git
  rabbitmq:
    host: 106.13.191.207
management:   # 暴露触发消息总线的地址
  endpoint:
    web:
      exposure:
        include: bus-refresh

3. 启动类

@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {

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

测试
在gitee创建一个仓库

将配置文件user-dev.yml上传到git上;配置中心对配置文件的名是有格式要求的
文件命名规则:
{ApplicationName}-{profile}.yml或{ApplicationName}-{profile}.properties application为应用名称 profile指的开发环境(用于区分开发环境,测试环境、生产环境 等)

启动服务器;

http://localhost:12000/user-dev.yml

在这里插入图片描述
这就说明服务器搭建成功了。

客户端

1. pom依赖

  <!--config-client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
           <!-- spring cloud bus -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-bus</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

2. bootstarp.yml

spring:
  cloud:
    config:
      name: eureka  
      label: master #git的哪个分支
      profile: dev #profile
      uri: http://127.0.0.1:12000 #配置中心服务器地址和端口

**bootstrap.yml(bootstrap.properties)**用来在程序引导时执行,应用于更加早期配置信息读取,如可以使用来配置application.yml中使用到参数等

application.yml(application.properties) 应用程序特有配置信息,可以用来配置后续各个模块中需使用的公共参数等。
bootstrap.yml 先于 application.yml 加载

3. git上的配置文件user-dev

server:
  port: 9009
spring:
  application:
    name: tensquare-user   # 模块名
  datasource:   # 配置数据源
    druid:
      url: jdbc:mysql://127.0.0.1:3306/tensquare_user?characterEncoding=UTF-8&useUnicode=true&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
      username: 用户名
      password: 密码
      driverClassName: com.mysql.cj.jdbc.Driver
      initialSize: 5  #初始建立连接数量
      minIdle: 5  #最小连接数量
      maxActive: 20 #最大连接数量
      maxWait: 10000  #获取连接最大等待时间,毫秒
      testOnBorrow: true #申请连接时检测连接是否有效
      testOnReturn: false #归还连接时检测连接是否有效
      timeBetweenEvictionRunsMillis: 60000 #配置间隔检测连接是否有效的时间(单位是毫秒)
      minEvictableIdleTimeMillis: 300000  #连接在连接池的最小生存时间(毫秒)
  jpa:      # 配置jpa
    database: mysql
    show-sql: true
  redis:
    host: 106.13.191.207
    port: 6380
  rabbitmq:
    host: 106.13.191.207
eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:6868/eureka/
  instance:
    prefer-ip-address: true     # 跨域访问
 testbus: 6666

在修改完配置文件后:以post 方式 http://127.0.0.1:12000/actuator/bus-refresh 进行更新

@RefreshScope:是spring cloud提供的一种特殊的scope实现,用来实现配置、实例热加载。该注解可更新自定义注解如修改testbus值:

@RestController
@RefreshScope
@RequestMapping("/user")
public class UserController {

	@Value("${testbus}")
	private String testbus;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章