Config server

来源:互联网 发布:苹果mac键盘鼠标失灵 编辑:程序博客网 时间:2024/05/22 04:27
一、安装rebbitmq
1.下载安装
2.ip和端口
1) mq后台配置:localhost:5672
2) mq管理界面:localhost:15672
注:管理界面是一个插件,不会自动安装。
安装插件命令:rabbitmq-plugins enable rabbitmq_management

二、服务端config-server配置
1.pom.xml添加配置:
  <dependency>                <groupId>org.springframework.cloud</groupId>                <artifactId>spring-cloud-starter-bus-amqp</artifactId>        </dependency>        <dependency>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-starter-actuator</artifactId>        </dependency>

2.application.yml配置mq:
spring:    application:      name: config-server                             # 项目名称尽量用小写  cloud:    config:      server:        git:          uri: https://git.oschina.net/ruihin/xms/              # 配置git仓库的地址          search-paths: config-repository                       # git仓库地址下的相对地址,可以配置多个,用,分割。          username:                                             # git仓库的账号          password:                                             # git仓库的密码    bus:      trace:        enabled: true     # 开启cloud bus的跟踪  rabbitmq:    host: 10.83.20.167    port: 5672    username: guest    password: guest

3.**Application.java类前添加:@EnableConfigServer

三、客户端config-client配置
1.pom.xml配置:
     <!-- Spring-boot 执行器:Spring-boot自带的监控模块 -->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-actuator</artifactId>        </dependency>        <!-- 整合配置文件 config-server -->        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-config</artifactId>        </dependency>        <!-- 自动更新配置文件的 MQ -->        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-bus-amqp</artifactId>        </dependency>

2.application.yml配置mq:
spring:    cloud:      config:        uri: http://10.83.3.13:8040/        profile: dev  # 指定profile,对应config-repository中文件名的后缀{profile}        label: master                     # 指定git仓库的分支,对应config-server所获取的配置文件的{label}  rabbitmq:    host: 10.83.20.167    port: 5672    username: guest    password: guest  management:   #springboot 1.5.X 以上默认开通了安全认证,需要改为false,否则更新curl -X POST http://localhost:8001/refresh时,会报401没权限    security:      enabled: false

3.使用配置文件参数的service或controller:添加@RefreshScope
@RestController@RequestMapping("/user")@RefreshScopepublic class ProviderUserController {    @Value("${profile}")    private String profile;    @GetMapping("/hello")    public String hello() {        return this.profile;    }}

四、更新操作
1.修改.properties文件并git提交
2.触发配置更新操作
1)方式一(适于开发期):执行shell 或命令
命令:curl -X POST http://localhost:8001/refresh
或是shell:
chmod +x /Users/XHZ/Ruihin/test/RefreshConfigClient.sh
./Ruihin/test/RefreshConfigClient.sh
2)在webhook上配置:
a.http://localhost:8001/refresh
注:localhost改成config-client的外网地址
b.勾选:push和tag push
c.提交