SpringBoot

踩坑-springcloud统一配置中心+bus-amqp+gi

2019-04-01  本文已影响0人  iGroove

Tips:
springcloud版本:Finchley.RELEASE
springboot版本:2.0.8.RELEASE
jdk:1.8
rabbitmq : 用默认用户guest

当你看到这篇时,你可能看了好多篇关于配置刷新的文章,但是都不能解决你的问题,要么版本不一致,配置出问题,mq有问题等等,放轻松,慢慢往下看,然后就很舒服。折腾了一天的坑,踩过了大大小小的坑,终于把config利用bus-amqp基于webhook的自动刷新给解决了。
首先,快速搭建一套eureka server、eureka client 、config-server ,@EnableDiscoveryClient在F版本可以省略了,三步走依赖、注解、配置。

一、 搭建项目

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

注解:@SpringBootApplication @EnableEurekaServer
配置:

### 注意 文件名为application.yml
spring:
  application:
    name: eureka-server
server:
  port: 8761
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
    fetch-registry: false
    register-with-eureka: false
        <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>

注解:@SpringBootApplication
配置:

### 注意 文件名为 bootstrap.yml
server:
  port: 8082
spring:
  application:
    name: comsumer 
  cloud:
    config:
      profile: dev      # profile对应config server所获取的配置文件中的{profile}
      label: master     # 指定Git仓库的分支,对应config server所获取的配置文件的{label}
      discovery:
        enabled: true
        service-id: config-server ##配置中心服务名
      name: eureka-client  ##仓库中对应配置的名子
  rabbitmq:
    host: 192.168.246.134 ##我虚拟机地址
        <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>

注解:@SpringBootApplication,@EnableConfigServer
配置:

### 注意 文件名为application.yml
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          # Git仓库地址 请写自己的
          uri: https://github.com/Guiee/spring-cloud-config-repo.git
  rabbitmq:
    host: 192.168.246.134
eureka:
  instance:
    prefer-ip-address: true
management:
  endpoints:
    web:
      exposure:
        include: "*"

不想自己写可以直接clone下来,看看结果,项目代码:https://github.com/Guiee/springcloud-config

二、 安装rabbitmq

这个网上搜一搜就ok了,注意的是如果mq安装在虚拟机里,本地主机是无法用guest用户登录的。

三、依次启动 eureka server(8761)、config server(8080)、eureka client(8082)

四、 webhook自动刷新

在config-server中把bus-refresh端点暴露出来,默认actuator只暴露 info health我就全暴了。这里省略建仓库那些的。

management:
  endpoints:
    web:
      exposure:
        include: "*"

可能很多人用 Finchley.RELEASE这个版本向config server 发送 post http://localhost:8080/actuator/bus-refresh都能成功,但是一用webhook就不能成功,要么404,405要么就报乱七八槽的错,还有config-server刷新了,客户端没有刷新。坑只有自己踩过,才知道是真的坑,哈哈。

@RestController
public class ConfigController {
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;

    @Value("${spring.application.name}")
    private String serviceId;

    @PostMapping("/refresh")
    public Object sync() {
        Map<String, Integer> map = new HashMap<String, Integer>();
        ServiceInstance serviceInstance = loadBalancerClient.choose(serviceId);
        System.out.println(serviceId);
        String url = String.format("http://%s:%s/actuator/bus-refresh", serviceInstance.getHost(), serviceInstance.getPort());
        System.out.println(url);
        restTemplate.postForObject(url, map, Object.class);
        map.put("code", 0);
        return map;
    }
}

写的可能很粗糙,很多地方没写清楚,但这是真真实实动态刷新了,没明白的可以简信我。

上一篇 下一篇

猜你喜欢

热点阅读