springcloud动态拉取配置文件时的“坑”

springcloud动态拉取配置文件的各种坑中的个坑

本人比较头铁,用了2.2.1版本,在使用bus-amqp动态拉取git上的配置文件时那真叫一个惨,且听我一一道来。
首先我是看的慕课网的某师兄的视频搭建的
第一个坑:路径问题
一些老版本使用bus-amqp对外映射的供webhook发送post请求的路径一般都是bus-refresh,而新版本的映射路径为/actuator/bus-refresh,而且当你把这个接口放到webhook上的时候你会发现post后接收到的是400错误,然后网上有的给出的方法是写一个拦截器将返回的数据的body部分剥离,这个确实可以。但是,大可不必这么麻烦,直接引入monitor依赖就完事了:

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

使用monitor可以自动处理返回来的数据,直接将webhook配置的/actuator/bus-refresh路径变为/monitor就可以了
第二个坑:配置文件
老版本配置接口暴漏的时候management.endpoients.web下面是expose而新版本下面的配置如下:

management:
  endpoints:
    web:
      exposure:
        include:*

当然,它还有一个跟include意思相反的exclude配置,如果你不知道什么意思那就去翻译吧。
第三个坑:消息队列
出这个问题我觉得应该是我太傻了,问题是这样的,当我把配置上传到git上,在client(客户端)写完打印配置文件的controller并使用@RefreshScope注解定义的属性类,配置好webhook,打开内网穿透,configserver接收到git发来的post请求,然后在console栏打印出令人激动的日志后,更扯淡的东西来了,client端打印出来的信息显示的还是原来的配置,一脸懵逼的我又打开client端查看是不是我的RefreshScope写错了或者放错位置了,再三确定没问题后重启client端请求client的controller打印发现配置变化了,更改git配置再次请求client的controller发现配置还是没有刷新。经过各种尝试,查看各种帖子还是没有解决,最后发现了一件很蠢的事情,我的configserver和client用的不是一个消息队列,当时就气哭了…
我的configserver用的是本地的消息队列,而client用的是从git上拉取的配置文件中定义的云服务器上的消息队列,总线的消息怎么可能发送到各个监听段…

下面是我的maven依赖和@RefreshScop读取配置文件的代码:
config-server-maven:

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <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-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-monitor</artifactId>
        </dependency>
    </dependencies>

client-maven:

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.24</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <!-- 默认的版本为3.8.1,修改为4.x,因为3.x使用的为编程的方式,4.x为注解的形式。 -->
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.2.0.RC1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.2.0.RC1</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <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>
    </dependencies>

获取配置测试:

bean:

package com.qiuyang.order.dataobject;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

/**
 * @Author QiuYang
 * @Date: Created in 2019 2019/11/23 11:12
 **/
@Data
@Component
@ConfigurationProperties("girl")
@RefreshScope
public class Girl {

    private String name;

    private Integer age;
}

controller:

package com.qiuyang.order.controller;

import com.qiuyang.order.dataobject.Girl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author QiuYang
 * @Date: Created in 2019 2019/11/23 10:52
 **/
@RestController
public class profileController {

    @Autowired
    private Girl girl;

    @GetMapping("/getProfile")
    public String GetProfile(){
        System.out.println(girl.getAge()+girl.getName());
        return girl.getAge()+">>>>>"+girl.getName();
    }
}

把我的配置也给拉出来吧:
git:

spring:
  application:
    name: order
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: ******
    url: jdbc:mysql://127.0.0.1:3306/zhiqingchun?characterEncoding=utf-8&userSSL=false
  rabbitmq:
    port: 5672
    host: localhost
    username: guest
    password: guest
girl:
  age: 18
  name: jiajia
env:
  dev

config-server(application.yml):

spring:
  application:
    name: config
  cloud:
    config:
      server:
        git:
          uri: https://github.com/jiajia0622/springcloud-profile.git
          username: ******
          password: ******
          basedir: G:\config\basedir\
      profile: dev
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8086
management:
  endpoints:
    web:
      exposure:
        include: "*"

client(bootstrap.yml)

spring:
  application:
    name: order
  cloud:
    config:
      discovery:
        enabled: true
        service-id: CONFIG
      profile: dev
      label: master
eureka:
  client:
    server-url:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8087
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值