Spring Cloud微服务【Finchley.RELEASE版本】(六)高可用配置及自动刷新配置

Spring Cloud微服务【Finchley.RELEASE版本】(六)高可用配置及自动刷新配置

思路

实现高可用有两种方式:
第一种:按照传统的做法,构建多个配置中心,都指向同一个git配置仓库,然后利用负载均衡,分发来自客户端的请求。
第二种:我们也可以将配置中心作为服务注册到,服务注册中心,就和客户端一样。
这里采用第二种方式:

(一)构建配置中心config-server-git-highava

本demo接上篇继续改造,将config-server-git改造一下,具体如下:

依赖引入

多引入了:spring-cloud-starter-netflix-eureka-client

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

启动类添加注解

@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class ConfigServerGitApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerGitApplication.class, args);
    }
}

application.yml配置

spring:
  application:
    name: config-server

  cloud:
    config:
      server:
        git:
          uri: https://github.com/Aslan007/spring-cloud-finchley-demo
          #配置文件在仓库的路径
          searchPaths: /
          default-label: master
          username:
          password:
      label: master

server:
  port: 1002

#将自己【配置中心】注册为服务
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1001/eureka/

配置完即可。

(二)配置客户端

依赖引入

多引入spring-boot-starter-actuator,用于刷新配置。除了刷新配置之外,spring-boot-starter-actuator 还具有监控等功能。

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

启动类添加注解

@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

新建bootstrap.yml替代application.properties配置

注意spring-boot-starter-actuator 2.0.3之后,需要多配置下面management部分的内容,暴露接口,而且暴露出来的接口需要以post的方式调用。

spring:
  application:
    name: config-client
  cloud:
    config:
    #配置信息从注册中心的config-server服务里面拉取,config-server服务里面的配置是从git仓库获取的
       discovery:
            enabled: true
            service-id: config-server
       profile: dev

server:
  port: 2006

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1001/eureka/

#spring-boot-starter-actuator 2.0.3 需要配置将如下三个接口暴露出来,才可以直接调接口,而且是POST方法 访问/actuator/refresh刷新
management:
  endpoints:
    web:
      exposure:
        include: refresh,health,info

编写测试controller,用于获取git仓库里面配置的内容

@RefreshScope 这个注解如果不加,会导致取不到最新的配置。

@RefreshScope
@RestController
public class TestController {
    @Value("${version}")
    private String version;

    @RequestMapping("/version")
    public String from() {
        return this.version;
    }
}

测试

首先,git里面配置如下:
这里写图片描述
然后访问http://localhost:2106/version,获取配置的version信息:
这里写图片描述

然后我们修改配置,将version改为:
version: client-git-dev-3.0

修改之后我们刷新访问http://localhost:2106/version,发现并没有改变为3.0。接着我们刷新配置。
使用postman的post的方式访问:http://localhost:2106/actuator/refresh,刷新配置:
这里写图片描述
再次刷新:http://localhost:2106/version
这里写图片描述
可以发现已经成功刷新到了最新的配置版本。

(三)改进思路

我们每次都手动调接口的方式去刷新固然是比重启项目来得好,但是依旧不完美。现有改进的思路如下:
我们可以使用使用git的webhook,每次有新内容push到仓库就自动调用一次刷新接口。
因为目前我没有这个需求,就暂时没有实现。

猜你喜欢

转载自blog.csdn.net/qq_29534483/article/details/81363282