spring cloud config 和spring cloud Bus消息总线 来完成配置中心

11 篇文章 0 订阅
10 篇文章 0 订阅

1,先创建一个远程仓库

2,在仓库新建配置文件命名规则如下:

配置文件的命名方式:{application}-{profile}.yml或者{application}-{profile}.properties

{application}:为微服务应用名

{profile}:用于区分开发环境dev,测试环境test,生产环境pro等

例如:user-dev.yml

           user-test.yml

           user-pro.yml

3,在父工程中创建一个微服务,引入依赖

       <!--eureka客户端-->
        <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>
package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient //eureka客户端
@EnableConfigServer //启动配置中心服务
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class,args);
    }
}

4,配置

server:
  port: 18086

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/xxxxxx/test_springcloud_config.git
          #用户名
          #username:
          #密码
          #password:
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:18080/eureka/
logging:
  level:
    com: debug

5,开启eureka,开启我们的配置中心服务,访问127.0.0.1:18086/user-consumer-dev.yml可以发现可以获取到git上面的配置了


以上就算一个配置中心服务整好了,现在我们来改造一下我们的项目

1,在需要使用远程配置的模块中加入依赖

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

2,把模块的application.yml删除掉,创建bootstrap.yml。

bootstrap.yml优先级比application高。通常我们会把一般不需要变动的配置放到这个配置文件里面。

#注释版本
spring:
  cloud:
    config:
      name: user-consumer #与远程仓库的文件名的application保持一致
      profile: dev #与远程仓库配置文件名的profile一致
      label: master #与仓库中的版本一致,分支
      discovery:
        enabled: true #是否使用配置中心
        service-id: config-server #配置中心服务id
#注册中心,配置这个是为了通过上面的service-id通过服务名来获取端口
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:18080/eureka/


完成上面一系列操作,我们发现是可以从远程拉取配置了,但是有一个问题,就是我在远程修改配置后,需要重启服务,远程的配置才生效。我们可以通过Spring cloud Bus来解决。

1,在配置中心服务模块加入依赖

 <!--消息总线依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-bus</artifactId>
        </dependency>
        <!--rabbitmq依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
        </dependency>

2,修改配置中心微服务配置文件


server:
  port: 18086

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/dailianqun/test_springcloud_config.git
          #用户名
          #username:
          #密码
          #password:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
#暴露触发消息总线的地址
management:
  endpoints:
    web:
      exposure:
        #
        include: bus-refresh
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:18080/eureka/
logging:
  level:
    com: debug

3,修改我们需要远程配置的模块,添加依赖

<!--消息总线依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-bus</artifactId>
        </dependency>
        <!--rabbitmq依赖-->
        <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>

4,修改配置

#注释版本
spring:
  cloud:
    config:
      name: user-consumer #与远程仓库的文件名的application保持一致
      profile: dev #与远程仓库配置文件名的profile一致
      label: master #与仓库中的版本一致,分支
      discovery:
        enabled: true #是否使用配置中心
        service-id: config-server #配置中心服务id
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
#注册中心,配置这个是为了通过上面的service-id通过服务名来获取端口
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:18080/eureka/

对了这个还需要用到rabbitmq,没有开启rabbitmq的记得开启

5,启动类加入一个刷新配置的注解@RefreshScope //刷新配置的注解

package org.example;

import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker//开启熔断机制
@EnableFeignClients//
@RefreshScope //刷新配置的注解
public class UserConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserConsumerApplication.class,args);
    }

    @Bean
    @LoadBalanced //启动负载均衡
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

完成以上后http://localhost:18086/actuator/bus-refresh请求一下配置中心微服务这个地址,然后配置文件就会生效了

测试代码(17条消息) springcloud_parent.rar-Java文档类资源-CSDN下载

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值