SpringClound-分布式配置中心及其高可用

Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持。配置服务器为各应用的所有环境提供了一个中心化的外部配置。它实现了对服务端和客户端对Spring Environment和PropertySource抽象的映射,所以它除了适用于Spring构建的应用程序,也可以在任何其他语言运行的应用程序中使用。作为一个应用可以通过部署管道来进行测试或者投入生产,我们可以分别为这些环境创建配置,并且在需要迁移环境的时候获取对应环境的配置来运行。

配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。当然他也提供本地化文件系统的存储方式,下面从这两方面介绍如何使用分布式配置来存储微服务应用多环境的配置内容。

构建Config Server

创建configserver项目
这里写图片描述

我们首先创建程序主程序,只需要多加上@EnableConfigServer 注解即可


/**
 * 分布式配置中心服务端
 * 2018年6月15日14:13:01
 * yangzhap
 */
@SpringBootApplication
@EnableConfigServer
public class ConfigserverApplication {

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

配置文件中配置GIt信息

spring.application.name=config-server
server.port=8766

# git管理配置
spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/
#配置git仓库位置
spring.cloud.config.server.git.searchPaths=respo
#配置仓库路径下的相对搜索位置,可以配置多个
spring.cloud.config.label=master
spring.cloud.config.server.git.username=your username
#访问git仓库的用户名
spring.cloud.config.server.git.password=your password
#访问git仓库的用户密码

这里写图片描述
这里写图片描述
这里写图片描述
URL与配置文件的映射关系如下:

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

接下来我们启动项目,访问
http://localhost:8766/config-client/dev/master这里写图片描述

另外Spring Cloud Config也提供本地存储配置的方式。我们只需要设置属性spring.profiles.active=native,Config Server会默认从应用的src/main/resource目录下检索配置文件。也可以通过spring.cloud.config.server.native.searchLocations=file:F:/properties/属性来指定配置文件的位置。虽然Spring Cloud Config提供了这样的功能,但是为了支持更好的管理内容和版本控制的功能,还是推荐使用git的方式。
我们现在configserver项目已经可以抓取到git中配置文件的信息,我们现在创建configclient项目
这里写图片描述

构建Config Client

创建程序主类

/**
 * 分布式配置中心客户端
 * 2018年6月15日14:21:55
 * yangzhao
 */
@SpringBootApplication
@RestController
public class ConfigclientApplication {

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

    @Value("${democonfigclient.message}")
    private String from ;

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

创建bootstrap.properties配置

spring.application.name=config-client
#配置zull就不行 不知道为什么 抓取不上
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:8766/

server.port=8767


#只和git中文件路径有关 和server(除了地址)中没关系
#spring.application.name:对应前配置文件中的{application}部分
#spring.cloud.config.profile:对应前配置文件中的{profile}部分
#spring.cloud.config.label:对应前配置文件的git分支
#spring.cloud.config.uri:配置中心的地址

这里需要格外注意:上面这些属性必须配置在bootstrap.properties中,config部分内容才能被正确加载。因为config的相关配置会先于application.properties,而bootstrap.properties的加载也是先于application.properties
启动configclient项目,访问http://localhost:8767/from
这里写图片描述

扫描二维码关注公众号,回复: 3530226 查看本文章

高可用问题

我们来说一下配置中心高可用问题,传统的做法就是我们将多个configserver项目访问同一个Git,接着在client和server项目之间加上负载均衡实现高可用集群。
另外一种更简单的方法就是我们将我们的项目注册为微服务,然后访问同一个Git,我们就可以通过微服务集群实现高可用问题了。接下来我们改造我们的server以及client项目
configServer 项目我们只需要在程序主类中加上@EnableDiscoveryClient 注解,并在配置文件中指定注册中心地址即可eureka.client.serviceUrl.defaultZone= http://localhost:8761/eureka/
configClient 项目我们只需要在程序主类中加上@EnableDiscoveryClient 注解,并修改我们的bootstrap.properties 文件

spring.application.name=config-client
server.port=8767

eureka.client.serviceUrl.defaultZone= http://localhost:8761/eureka/

spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
spring.cloud.config.profile=dev

#通过eureka.client.serviceUrl.defaultZone参数指定服务注册中心,用于服务的注册与发现,
#再将spring.cloud.config.discovery.enabled参数设置为true,开启通过服务来访问Config Server的功能,\
#最后利用spring.cloud.config.discovery.serviceId参数来指定Config Server注册的服务名。\
#这里的spring.application.name和spring.cloud.config.profile如之前通过URI的方式访问时候一样,用来定位Git中的资源。

接下来启动我们的注册中心,一起两个微服务
访问http://localhost:8761/
这里写图片描述
可以看到两个微服务已经注册到服务中心接下来访问http://localhost:8767/from
一样ok
这里写图片描述

总结

  1. 简单来说高可用的实现就是通过注册为微服务
  2. 整个配置中心的实现:server项目通过配置Git项目url(spring.cloud.config.server.git.uri)以及配置文件确定 唯一文件夹(spring.cloud.config.server.git.searchPaths),client项目则通过application,profile,label等确定唯一的文件(pring.application.name,spring.cloud.config.profile) 最后在接口中通过
@Value("${democonfigclient.message}")
    private String from ;

确定配置文件中的具体信息,从而获得value

猜你喜欢

转载自blog.csdn.net/yz357823669/article/details/81385969