SpringBoot2.5.3集成Spring Cloud Config服务配置中心

新建一个my-config工程,作为配置中心服务端,在此之前我是用的Consul作为注册中心,并已经有一个,服务消费者my-consumer,服务提供者my-producer
前提是我们已经搭建好RabbitMQ,在其启动后并可以正常访问http://localhost:15672

1. 简单集成Spring Cloud Config,连接到Git仓库

配置中心服务端实现

1. 添加必要依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

SpringCloud的依赖:要注意版本要按照官网对应起来

<dependencyManagement>
   <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2020.0.3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

2. 在启动类添加注解@EnableConfigServer

@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class SecretConfigApplication {

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

}

3. 修改配置文件

server:
  port: 8896
spring:
  application:
    name: secret-config
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        serviceName: ${spring.application.name}	# 注册到consul的服务名称
    config:
      label: master  # git仓库分支
      server:
        git:
          uri: https://gitee.com/username/repository-name  # 配置git仓库的地址,不要有.git
          search-paths: src/yourApp  # git仓库地址下的相对地址,可以配置多个,用,分割。
          username: # git仓库的账号
          password: # git仓库的密码

4. 验证服务端

在与工程目录同级的目录下新建一个,config-repo目录
在目录中新建consumer-dev.properties 文件
配置hello属性hello=hello, I’m consumer-dev.properties
再新建consumer-pro.properties 文件

关于文件格式的规范
仓库中的配置文件会被转换成相应分web接口,访问规则如下

  • /{application}/{profile}/[/{profile}]
  • /{application}-{profile}.yml
  • /{label}/ {application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/ {application}-{profile}.properties
    故创建配置文件也要借鉴以上规则来命名

配置hello属性hello=hello, I’m consumer-pro.properties
用来模拟验证一下
然后push到git仓库
访问 http://localhost:8896/consumer/dev
返回结果是这个样子的

{
    "name": "consumer",
    "profiles": [
        "dev"
    ],
    "label": null,
    "version": "2adf7cabed9746293bxxxxxxaec47d5fbf955",
    "state": null,
    "propertySources": [
        {
            "name": "https://gitee.com/xxxxxx/xxxxxxxxxxxxx........",
            "source": {
                "hello": "hello, I'm consumer-dev.properties"
            }
        }
    ]
}

如果再访问http://localhost:8896/consumer/pro,也与此大致相同,即说明完成配置

客户端实现

修改升级my-consumer,:

1. 添加必要依赖

 <!-- spring-cloud-config -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
 <!-- spring-cloud-starter-bootstrap 如果不添加在启动时无法加载bootstarp.yml配置文件 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

2. 添加修改配置文件

添加一个bootstarp.yml文件,添加配置中心,并把注册中心的配置移动到这里,因为通过配置中心查找配置是需要通过注册中心发现服务
bootstarp.yml

spring:
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        serviceName: my-consumer	# 注册到consul的服务名称
    circuitbreaker:
      hystrix:
        enabled: true
    config:
      discovery:
        enabled: true  # 开启服务发现
        serviceId: my-config # 配置中心服务名称
      name: consumer  # 对应{application}部分
      profile: dev  # 对应{profile}部分
      label: master  # 对应git的分支,如果配置中心使用的是本地存储,则该参数无用

application.yml 拆分后就,必要配置变成下面这个样子

server:
  port: 8881
spring:
  application:
    name: secret-consumer
# 开放健康检查接口
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

3. 添加控制器

添加一个ConfigController控制器,添加注解@Value 声明hello属性从配置文件中读取

@RestController
class ConfigController {
    
    @Value("${hello}")
    private String hello;

    @RequestMapping("/hello")
    public String from() {
        return this.hello;
    }
    
}

4. 验证客户端

访问 http://localhost:8881/hello 可得到配置信息的内容
表明客户端搭建完成

2. 使用Rabbit MQ集成消息总线,完成部署

升级服务端

1. 添加必要依赖

之前的用法:

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

但是我使用 spring-cloud-starter-bus-amqp 一直报错无法解决,百度了好长时间,才发现应该用以下的依赖

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

2. 添加必要配置

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

在添加RabbitMQ和接口开放之后的相关配置,这样的服务段代码改造就完成了

升级客户端

1. 添加必要依赖

添加如下:

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

2. 添加修改必要配置

bootstarp.yml 最终所需的必要配置:

spring:
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        serviceName: my-consumer	# 注册到consul的服务名称
    config:
      discovery:
        enabled: true  # 开启服务发现
        serviceId: my-config # 配置中心服务名称
      name: consumer  # 对应{application}部分
      profile: dev  # 对应{profile}部分
      label: master  # 对应git的分支,如果配置中心使用的是本地存储,则该参数无用
  rabbitmq:
    host: localhost
    port: 5672
    username: guest #我们先使用默认的RabbitMQ的username
    password: guest #我们先使用默认的RabbitMQ的password

application.yml最终所需的必要配置:

server:
  port: 8881
spring:
  application:
    name: secret-consumer
# 开放健康检查接口
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

3.修改配置类

在ConfigController中加上@RefreshScope注解

@RefreshScope
@RestController
class ConfigController {
    
    @Value("${hello}")
    private String hello;

    @RequestMapping("/hello")
    public String from() {
        return this.hello;
    }
    
}

4. 添加配置类

如果不添加配置类在当前版本仍会报错:
org.springfamework.brans.factory.NoSuchBeanDefinitionException
在这里插入图片描述
在这里插入图片描述

1. 在启动类的同级目录新建 RetryConfiguration 类
public class RetryConfiguration {
	
	@Bean
	@ConditionalOnMissingBean(name = "configServerRetryInterceptor")
	public RetryOperationsInterceptor configServerRetryInterceptor() {
		return RetryInterceptorBuilder.stateless().backOffOptions(1000, 1.2, 5000).maxAttempts(10).build();
	}
	
}
2. 在resource目录下新建META-INF,并在里面新建spring.factories文件

spring.factories中的内容

org.springframework.cloud.bootstrap.BootstrapConfiguration={你的RetryConfiguration类的包名}.RetryConfiguration

然后客户端就可以正常启动

当配置文件更新之后访问客户端 http://localhost:8881/hello 还不能刷新更新的内容
需要访问配置中心服务端 http://localhost:8896/actuator/refresh 进行刷新,即可
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值