springcloud 学习笔记(五)Spring Cloud Config分布式配置中心

缘起

在分布式系统中,由于服务数量非常多,配置文件分散在不同的微服务项目中,管理不方便。为了方便配置文件集中管理,需要分布式配置中心组件。在Spring Cloud中,提供了Spring Cloud Config,它支持配置文件放在配置服务的本地,也支持放在远程Git仓库(GitHub、码云)
使用Spring Cloud Config配置中心后的架构如下图:
在这里插入图片描述

创建远程配置仓库

在这里插入图片描述
配置文件的命名方式:{application}-{profile}.yml 或 {application}-{profile}.properties
application为应用名称profile用于区分开发环境,测试环境、生产环境等如userservice-dev.yml,表示用户微服务开发环境下使用的配置文件。
这里将userservice工程的配置文件application.yml文件的内容复制作为user-dev.yml文件的内容。
在这里插入图片描述

创建配置服务

添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloudparents</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>configserver</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!--<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!--<executions>
                    <execution>
                        <phase>none</phase>
                    </execution>
                </executions>-->
            </plugin>
        </plugins>
    </build>
</project>

创建启动类

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

此处要注明的是需要添加@EnableConfigServer配置类

创建配置类

server:
  port: ${port:12000}
spring:
  application:
    name: configserver
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/xueshanfeitian/feitianconfig.git
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka

需要从码云上拉取地址,所以我们需要添加码云配置仓库地址。
测试后配置中心搭建成功
在这里插入图片描述

改造userservice

userservice需要改造yaml配置文件

spring:
  cloud:
    config:
      #要与配置仓库中的配置文件application的名字保持一致
      name: userservice
      # 要与仓库中的profiles
      profile: dev
      # 要与仓库中的配置文件所属的分支号保持一致
      label: master
      discovery:
        enabled: true
        service-id: configserver
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka

在这里插入图片描述
在这里插入图片描述
访问userservice端口:
在这里插入图片描述

springbus

上述案例存在的问题

当修改配置远程仓库中的配置文件时,配置文件更新的实时性出现问题,每次服务都得重启才能获取最新配置文件,如何解决这一问题,我们将采用一种全新的技术架构来解决此问题。

架构的改进

Spring Cloud Bus是用轻量的消息代理将分布式的节点连接起来,可以用于广播配置文件的更改或者服务的监控管理。也就是消息总线可以为微服务做监控,也可以实现应用程序之间相互通信。 Spring Cloud Bus可选的消息代理有RabbitMQ和Kafka。
在这里插入图片描述
Spring Cloud Bus将git仓库的配置文件更新,在不重启系统的情况下实现及时同步到各个微服务。

springcloud bus使用

启动rabbitmq

在这里插入图片描述

修改configserver依赖

在pom文件中加入下面的依赖

       <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>
修改配置文件

需要接入rabbitMQ的地址

server:
  port: ${port:12000}
spring:
  application:
    name: configserver
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/xueshanfeitian/feitianconfig.git
  # 配置rabbit信息
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka
management:
  endpoints:
    web:
      exposure:
        # 暴露触发消息总线地址
        include: bus-refresh

改造userservice

修改pom文件
<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>
修改配置文件
spring:
  cloud:
    config:
      #要与配置仓库中的配置文件application的名字保持一致
      name: userservice
      # 要与仓库中的profiles
      profile: dev
      # 要与仓库中的配置文件所属的分支号保持一致
      label: master
      discovery:
        enabled: true
        service-id: configserver
  # 配置rabbit信息
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka
修改更新作用范围
@RestController
@RequestMapping("/userController")
@RefreshScope
public class UserController {

    @Autowired
    private UserService userService;

    @Value("${test.name}")
    private String name;

    @RequestMapping("/findById/{id}")
    public User findById(@PathVariable Long id){
        System.out.println("------the name is ---------"+name);
        return userService.findById(id);
    }

    @RequestMapping("/queryById/{id}")
    public User queryById(@PathVariable Long id){
        System.out.println("------the name is ---------"+name);
        return userService.queryById(id);
    }
}

在这里插入图片描述

测试

启动顺序
在这里插入图片描述

测试结果

在这里插入图片描述
发送postman请求更新配置文件
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
说明配置文件在线更新成功。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值