SpringCloud学习记录(三)分布式配置中心

Spring Cloud config

在这里插入图片描述

配置中心服务端

  1. 配置pom依赖
 <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>

  1. 配置yml
server:
  port: 3344
spring:
  application:
    name: springcloud-config-server
    # 连接远程仓库
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/himitzh0730/springcloud-config.git #远程仓库地址
      label: master # 分支



# 服务注册到eureka
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com

  1. 主启动类,配置完就启动,等待其它微服务通过本配置中心去远程仓库获取配置。
@SpringBootApplication
@EnableConfigServer
public class Config_Server_3344 {
    public static void main(String[] args) {
        SpringApplication.run(Config_Server_3344.class,args);
    }
}
  1. 微服务读取配置方式
    在这里插入图片描述
    推荐:/{label}/{application}-{profile}.yml
  2. 微服务需要添加pom依赖spring-cloud-starter-config
 		<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>


  1. 微服务配置需要用到bootstrap.yml(系统级上下文管理),这样才能在启动前通过配置中心获取远程仓库的配置。
server:
  port: 3355
spring:
  cloud:
    config:
      name: config-eureka # 配置文件的名字
      label: master #分支
      profile: dev # 生产环境
      uri: http://localhost:3344 #配置中心服务的地址
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
#暴露监控端口
management:
  endpoints:
    web:
      exposure:
        include: "*"

  1. 在控制层controller加上注解@RefreshScope 动态刷新配置
@RefreshScope
@RestController
public class ConfigClientController {

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

    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}
  1. 每次修改都得发送一个POST请求 "http://localhost:3355/actuator/refresh"去提醒微服务配置已更新,可以避免每次配置更改,都要重启服务。
# 使用curl发送post请求
curl -X POST "http://localhost:3355/actuator/refresh"
  1. 如果有多个微服务配置都要更新呢,总不能一个一个提醒微服务吧?能不能一键提醒所有微服务呢?有的,springcloud 的另一组件spring cloud bus消息总线配合MQ消息中间件可以做到一键刷新所有微服务配置。
    原理大概: 触发一个服务端ConfigServer的/bus/refresh端点,通过消息总线告知每个微服务,而刷新所有客户端微服务的配置
    在这里插入图片描述
  2. 给配置中心服务和各微服务都加上消息总线的pom依赖
<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

  1. 消息中间件用的是RabbitMQ,在这之前需要先安装RabbitMQ和相关环境,之后配置中心和各微服务都要在yml加上相关配置
rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
  1. 启动配置中心,各微服务,然后在远程仓库修改配置文件,然后依旧发送post请求 "http://localhost:3344/actuator/bus-refresh,通过告知配置中心,配置中心通过消息总线去通知全部微服务。达到一键动态更新所有微服务的配置。
curl -X POST "http://localhost:3344/actuator/bus-refresh

nacos实现配置中心功能

在这里插入图片描述

  1. 啊,启动nacos,这个就不说了,有需要的去看本系列的第一篇学习记录,启动相当于开启了注册与发现,配置中心的功能。(还不止,应该再加上bus,因为nacos实现了一键动态更新所有微服务配置),然后在nacos控制台可视化添加配置文件即可~
    在这里插入图片描述
    点击添加,填写配置,点击发布即可,Data UD必须为“微服务名-生产环境.文件后缀"格式===>${spring.application.name}-${spring.profile.active}.${spring.cloud.naces.config.file-extension}
    在这里插入图片描述
  2. 微服务导入spring cloud alibaba的nacos的配置中心依赖spring-cloud-starter-alibaba-nacos-config。顺便也导入了注册与发现的依赖spring-cloud-starter-alibaba-nacos-discovery
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>
    <!--nacos-discovery-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!--web + actuator-->
    <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>

  1. 配置yml,依旧需要系统级别的上下文管理bootstrap.yml

# nacos配置

server:
  port: 3377

spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: 129.204.177.72:8848 #Nacos 服务注册中心地址
      config:
        server-addr: 129.204.177.72:8848 #Nacos 作为配置中心地址
        file-extension: yml #指定yml格式的配置
        group: DEFAULT_GROUP  # 指定分组
        #namespace:命名空间ID  默认为public

# ${spring.application.name}-${spring.profile.active}.${spring.cloud.naces.config.file-extension}
# nacos-config-client-test.yml

application.yml来配置指定获取的配置环境

spring:
  profiles:
    active: test
  1. 控制层加上@RefreshScope注解,让微服务可以自动检测刷新配置。
@RestController
@RefreshScope
public class ConfigClientController
{
    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/config/info")
    public String getConfigInfo() {
        return configInfo;
    }
}
  1. 主启动类
//启动类
@SpringBootApplication
@EnableDiscoveryClient //服务发现
public class CloudAlibabaConfigClient3377 {
    public static void main(String[] args) {
       SpringApplication.run(CloudAlibabaConfigClient3377.class, args);
    }
}
  1. 完事,使用确实很简单,只要每次在nacos里面修改一下对应的配置文件,获取该配置文件的各微服务都会动态更新相应配置,当然如何让配置持久化呢?那就应该写入mysql等数据库,让nacos集群都从mysql获取相同的配置,让每个微服务获取的配置都能保持一致性。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章