SpringCloud(3) Config统一配置中心

config会从git拉取配置文件到本地,然后读取本地文件

1.config server端

pom.xml文件引入依赖

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

application.yml配置git信息

eureka:
  client:
    service-url:
      #注册服务端地址
      defaultZone: http://localhost:8761/eureka
spring:
  application:
    name: config
  cloud:
    config:
      server:
        git:
          #配置文件git地址
          uri: https://gitee.com/yejiaomin/config-repo
          #git用户名密码
          username: xxxx
          password: yyyyy
          #git文件保存本地地址
          basedir: D:\2.workspace\config\basedir

 启动类加配置

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

遇到的坑1: 访问 http://localhost:9090/order-dev.yml 报错500 

日志 org.eclipse.jgit.errors.RepositoryNotFoundException: repository not found  D:\2.workspace\config\basedir

解决: 配置项 basedir: D:\2.workspace\config\basedir 这个目录需要时git的仓库和远程仓库连接才能下载文件

2. client 端

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

bootstrap.yml 

由于从配置中心拉取配置仓库要先于其它功能的启动,其它功能才能读取到配置。

所以配置文件需要增加一个bootstrap.yml,spring启动的时候,会先读取bootstrap.yml的配置来启动,然后再读取application.yml的配置来启动。

使用配置中心后,客户端服务的eureka配置和config配置必须写到bootstrap.yml里面去,其它配置可以写到application.yml里面去

eureka:
  client:
    service-url:
      #注册服务端地址
      defaultZone: http://localhost:8761/eureka
spring:
  application:
    name: order
  cloud:
    config:
      discovery:
        enabled: true
        ## config server的spring.application.name: config
        service-id: config
      ## 环境名称
      profile: dev
eureka的defaultZone不能放到git上。 因为找到eureca server,再去找config,然后去config server的git找配置文件
DataSource的配置都在git上

读取配置文件格式

/{name}-{profiles}.yml

/{label}/{name}-{profiles}.yml

name 使用端服务名, profiles:环境,label: 分支

http://localhost:8080/release/order-dev.ymlhttp://localhost:8080/order-dev.yml

 

二。config bus自动更新

2.1 使用@RefreshScope + /actuator/bus-refresh端点手动刷新配置

实际是config server端提供了post接口/actuator/bus-refresh,手工post信息或者依赖webhooks,从git获取最新配置再发送消息给rabbitmq,然后rabbitmq再发消息给config client,client接收到消息后从server端读取配置

(1) pom文件

config即server端 和order即client端都加上:

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

(2)配置application.yml

config server端:配置rabbitmq信息和 启动监控端点  Config Server暴露/actuator/bus-refresh端点

eureka:
  client:
    service-url:
      #注册服务端地址
      defaultZone: http://localhost:8761/eureka
spring:
  application:
    name: config
  cloud:
    config:
      server:
        git:
          #配置文件git地址
          uri: https://gitee.com/yyy/config-repo
          #git用户名密码
          username: yyy
          password: yyy
          #git文件保存本地地址,要是git仓库
          basedir: D:\workspace\1.weixin\config\basedir
  rabbitmq:
    host: localhost
    port: 5672
    password: guest
    username: guest
server:
  port: 9090
#actuator 启用所有的监控端点 “*”号代表启用所有的监控端点,可以单独启用,例如,health,info,metrics
# spring boot 升为 2.0 后,为了安全,默认 Actuator 只暴露了2个端点,heath 和 info
management: endpoints: web: exposure: include: "*"

order端: 增加rabbitmq配置(host有时不生效不知道为啥,使用 spring.rabbitmq.addresses: 192.168.31.211:5672

eureka:
  client:
    service-url:
      #注册服务端地址
      defaultZone: http://localhost:8761/eureka
spring:
  application:
    name: order
  cloud:
    config:
      discovery:
        enabled: true
        ## config server的spring.application.name: config
        service-id: config
      ## 环境名称
      profile: dev
  rabbitmq:
    host: localhost
    port: 5672
    password: guest
    username: guest
server:
  port: 8080

(3) 测试类使用 @RefreshScope可实现配置数据刷新

@RestController
@Slf4j
@RefreshScope
public class ClientController {
    @Value("${env}")
    String env;
    
    @RequestMapping("/getEnv")
    public String getEnv() {
        return env;
    }
}

(4) 启动eureka,confg,order\

访问http://localhost:15672可以看到两个队列

 

 

 修改git文件的配置,在发送POST信息到config server http://localhost:9090/actuator/bus-refresh,正常响应是没有信息的

 

 rabbitmq就可以看到发送了消息

 

看到env参数已经变成最新的了

 

2.2 spring cloud bus自动更新

(1)pom文件修改

config端:

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

(2)bootstrap.yml文件,

config client端配置 spring.cloud.bus.id:

eureka:
  client:
    service-url:
      #注册服务端地址
      defaultZone: http://localhost:8761/eureka
spring:
  application:
    name: order
  cloud:
    config:
      discovery:
        enabled: true
        ## config server的spring.application.name: config
        service-id: config
      ## 环境名称
      profile: dev
    bus:
      #Workaround for defect in https://github.com/spring-cloud/spring-cloud-bus/issues/124
      id: ${vcap.application.name:${spring.application.name:application}}:${vcap.application.instance_index:${spring.cloud.config.profile:${local.server.port:${server.port:0}}}}:${vcap.application.instance_id:${random.value}}
  rabbitmq:
    host: localhost
    port: 5672
    password: guest
    username: guest
server:
  port: 8080

(3)公网映射:

使用ngrok.exe

ngrok http 9090

可以用这个外网地址直接访问

 

 将映射的外网地址 http://4686e2c4.ngrok.io/monitor添加到gitee的webHooks上

 

 

 

 (4) 测试,gitee上修改order-dev.yml文件,order侧就会自动更新

参考:https://artisan.blog.csdn.net/article/details/89117473

posted @ 2020-04-13 22:05  haohao1234  阅读(333)  评论(0编辑  收藏  举报