springcloud学习笔记-config分布式配置中心+bus消息总线

建立配置中心module:
依赖:

      <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>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </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>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

yml:

server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri: https://github.com/surpera/springcloud-config.git  #github路径
          search-paths:
            - springcloud-config
      label: main
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

主启动类:

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

测试可以从github中获取到配置信息,启动eureka7001和配置中心3344:
访问:http://localhost:3344/main/config-dev.yml
在这里插入图片描述
访问测试成功:
在这里插入图片描述

推荐配置规则:
在这里插入图片描述
然后建立3355客户端module来获取配置中心的配置:
依赖:

    <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>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </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>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

bootstap.yml:

server:
  port: 3355
spring:
  application:
    name: config-client
  cloud:
    config:
      label: main #分支名称
      name: config #配置文件名称
      profile: dev #读取后缀名称 name+profile=config-dev.yml的config-dev
      uri: http://localhost:3344 #配置中心地址

controller:

@RestController
@RefreshScope  //动态刷新
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

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

主启动类:

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

启动3355并访问github:http://localhost:3355/configInfo
结果:
在这里插入图片描述
问题:当github上的配置文件修改时,3344刷新可以获得修改后的配置,
但是3355由于yml中没有连接github,只有重启才能获取到修改后的配置。

解决方法:
首先引入依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

yml添加:

management:
  endpoints:
    web:
      exposure:
        include: "*"

来暴露监控端口

controller类加上@RefreshScope注解:

@RefreshScope
@RestController
public class ConfigClientController {}

主启动:

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

之后如果修改了github的配置文件:
只要在cmd中输入:curl -X POST “http://localhost:3355/actuator/refresh”
就可以让3355不用重启就可以得到更新后的配置。

问题:需要手动刷新,无法大范围定点的刷新。

解决方法,加入springcloud BUS消息总线(支持rabbitqm和kafka):
安装Erlang,再安装RabbitMQ
启动rabbitmq,访问http://localhost:15672/测试启动成功。

然后建立和3355客户端一样项目3366客户端:
集体修改配置中心3344,客户端3355,客户端3366的
pom.xml:
加入

<dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

yml中加入:

rabbitmq:
  host: localhost
  port: 5672
  username: guest
  password: guest

之后修改配置中心的配置文件后:
命令行输入:

curl -X POST "http://localhost:3344/actuator/bus-refresh"

发现3355,3366客户端都刷新了。

定点刷新输入:

curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355"

只刷新3355客户端,不刷新3366客户端.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值