Spring Cloud Config配置中心

536 阅读2分钟

配置中心

配置中心的由来

配置中心被用作集中管理不同环境和不同集群配置,以及在修改配置之后能将修改的配置即使的更新到系统。

配置中心具备的功能

  • Open API
  • 业务无关
  • 配置生效监控
  • 一致性K-V存储
  • 统一配置实时推送
  • 配合灰度与更新
  • 配置全局恢复,备份与历史
  • 高可用集群

Config配合git的工作原理

配置的客户端在启动的时候活像配置服务端发请求,服务端在接收到客户端的请求之后会在之前配置好的地址去git残酷拉去一份配置到本地,创建一个临时文件,这个目录就是一个git的本地仓目录,然后服务端读取本地文件返回给客户端。这样做的好处就是,当git服务器故障或者网路出现异常后仍然可以工作。

例子

服务端和客户端公共的依赖如下:

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

server端的依赖如下:

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

服务端的配置:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/chendom/SpringCloudConfig.git
          username: xx
          password: xx
          search-paths: config
  application:
    name: config-server
server:
  port: 8098

启动类:

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

客户端的依赖如下:

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

客户端配置如下:

server:
  port: 8099
spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      uri: http://localhost:8098
      name: client-config
      profile: dev

测试类的代码

@RestController
@RequestMapping("/demo")
public class DemoController {

@Value("${springcloud.configserver}")
 private String value;

@RequestMapping("/getValue")
public String getValue(){
    return value;
}
}

分别启动服务端和客户端

访问http://localhost:8098/client-config/dev/master

访问http://localhost:8099/demo/getValue

手动刷新

pom依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-client</artifactId>
    </dependency>
    <!--简单的安全和端点开放-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
</dependencies>
</project>

bootstrap.yml配置文件

server:
  port: 8100
spring:
  application:
    name: refresh-config-client
  cloud:
    config:
      label: master
      uri: http://localhost:8098
      name: client-config
      profile: dev
management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always

请求

@RestController
@RequestMapping("/demo")
@RefreshScope//刷新注解
public class DemoController {

@Value("${springcloud.configserver}")
 private String value;

@RequestMapping("/getValue")
public String getValue(){
    return value;
}
}

我们知道之前的配置是这样的 springcloud: configserver: this is test dev

现在该成如下图所示的样子:

此时如果没有刷新还是访问http://localhost:8100/demo/getValue还是不会有变化

此时我们可以访问 Post:http://localhost:8100/actuator/refresh

访问 http://localhost:8100/demo/getValue

以上就完成了手动刷新的例子,但是微服务一般服务都很多,如果有个别的例子没有进行刷新就很可能出现错误,开锁所以就需要使用到自动刷新