SpringCloud之集成配置中心及自动刷新

环境:SpingBoot2.0 ,SpringCloud Finchley.RELEASE

 登录github创建一个仓库 myspringcloudconfig 然后再创建一个config-client1文件夹

编写application.yml

name: zhangsan

application-dev.yml

name: zhangsan-dev

application-test.yml

name: zhangsan-test

3个文件并上传到config-client1目录下

搭建rabbitmq服务器

省略...

搭建Config Server项目 取名 zns-config

<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>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

启动类

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
@EnableConfigServer
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

bootstrap.yml

server:
  port: 10005
  servlet:
    context-path: /
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
spring:
    application:
        name: zns-config
    cloud:
        config:
          server:
            git:
              uri: https://github.com/zns/myspringcloudconfig.git  #Git仓库的地址
              search-paths: config-client1  #仓库中配置文件所在文件夹路径 默认 /
              username: [email protected]
              password: 123456
              label: master  #仓库的分支
        bus:
          trace:
            enabled: true
    rabbitmq: #本地环境可以不需要配置mq,但是需要启动mq,Springboot会自动连接本地的mq服务器
        host: localhost
        port: 5672
        username: guest
        password: guest

management: #打开bus/refresh刷新开关
  endpoints:
    web:
      exposure:
        include: "*"

 搭建Config Client项目 取名 zns-config-client

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

启动类

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

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

bootstrap.yml

server:
  port: 10006
  servlet:
    context-path: /
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
spring:
    application:
        name: zns-config-client
    profiles:
        active: dev #使用哪个环境配置文件
    cloud:
        config:
          fail-fast: true
          discovery:
            enabled: true
            service-id: zns-config  #配置中心项目服务的应用名称
    rabbitmq: #本地环境可以不需要配置mq,但是需要启动mq,Springboot会自动连接本地的mq服务器
        host: localhost
        port: 5672
        username: guest
        password: guest
@RefreshScope加在需要读取配置的类上
@RestController
@RefreshScope
public class MyController {

    @Value("${name}")
    String name;

    @RequestMapping("/getName")
    public String getName(){
        return name;
    }
}

测试

1.启动rabbitmq

2.启动注册中心eureka

3.启动配置中心项目

4.启动配置客户端项目

访问 getName 控制器url,可以看到已经读取到了git上dev环境的name属性值

git上修改name的值保存提交,再调用配置中心服务刷新配置url  

http://localhost:10005/actuator/bus-refresh

客户端再次访问getName,可以看到name的最新值已经同步更新显示

猜你喜欢

转载自www.cnblogs.com/zengnansheng/p/11167183.html