java面试题框架篇spring cloud之spring config server

1.前言

什么是spring cloud?

Spring cloud为用户提供了一整套快速开发分布式应用的解决方案,包括配置管理,服务发现,断路开关,智能路由,微代理,控制总线,一次token,全局锁,leader选举,分布式session,集群状态等功能。spring boot作为spring cloud等基础应用,可以快速搭建一个微服务。

2.功能点

  • Distributed/versioned configuration  分布式配置
  • Service registration and discovery  服务注册发现
  • Routing   路由
  • Service-to-service calls  服务间调用
  • Load balancing  负载均衡
  • Circuit Breakers  断路开关
  • Global locks  全局锁
  • Leadership election and cluster state leader选举和集群状态
  • Distributed messaging  分布式管理

3.分布式配置中心config server

3.1 应用

spring boot会有很多配置属性,在bootstrap或者application.properties中,区别不大,但是一般系统级别的配置在bootstrap中。

那么问题来了,每个人都修改配置文件,势必引起冲突或者不稳定,利用微服务思想,我们把配置中心也设计成一个微服务。

配置中心有两种方式:

  • 本地配置中心,一般是本地的配置服务器
  • 远程配置中心,一般是git仓库

但是无论那种方式都要保证高可用。配置中心的所有属性客户的和服务端都可以使用。

主要配置如下:

  • 本地config server
server.port=9090
spring.cloud.config.server.native.searchLocations=file://${user.home}/CentralRepo/
SPRING_PROFILES_ACTIVE=native
  • git server
spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/
spring.cloud.config.server.git.searchPaths=respo
spring.cloud.config.label=master
spring.cloud.config.server.git.username=your username
spring.cloud.config.server.git.password=your password

3.2 优势:

  • 修改配置无需重新部署微服务
  • 便于统一管理,提供了profile属性,可以统一切换配置属性文件,比如dev.properties, test.properties,production.properties.

上面两点保证微服务遵守了约定:

Build just once and deploy it in any environment.

一次build,永久部署

参考:https://dzone.com/articles/microservices-and-spring-cloud-config-server

3.3 缺点:

  • 没有cache策略
  • 没有负载均衡策略,没有failover机制,如采用本地文件可以采用共享文件存储(ClusterFS等),防止disk损害,但是git却比较麻烦,需要把config server部署到load balancer后面,client连接vip获取config
  • 如果存储git,存在网络问题导致属性加载出错
  • 如果使用git存储,push之后,配置文件不变化,更新配置文件后,需要使用postman post请求访问 http://localhost:2009/refresh 手动刷新。因为git虽然发生变化,但是config server并不知道。所以需要手动刷新。如何避免手动刷新呢?Git webhooks和 Spring Cloud Bus

关于git webhooks

https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks

3.4 高可用实现

由于config server不提供高可用实现,一般做法有以下两种:

  • 利用其它负载均衡实现。
  • 配置中心服务化,注册到eureka,利用eureka提供负载均衡,服务熔断等功能

这个就是使用一个负载均衡设备来隐藏后面的配置服务器集群,这是很容易想到的,不过Spring Cloud给我另一种实现,那就是服务化配置中心

也就是把配置中心也注册到Eureka-Server上称为一个服务,这样,就和其他普通的服务一样做到了多实例,负载均衡,当然也做到了高可用,还有其他的一些功能特性,比如,熔断降级啊,失败策略等。如下图所示,一般采用下面的应用

config-server

3.5 与zookeeper比较

zookeepr采用namespace体系,存储任意数据,监听机制等等,config server的优势是与spring紧密结合,利用spring抽象的

4 项目应用

  • 本地应用

我们的项目中配置了分布式config server,但是并没有启用,还是应用的本地配置文件,bootstrap中的配置如下:

spring.cloud.config.uri= @config.url@
spring.cloud.config.enabled=true
[email protected]@
spring.cloud.config.profile=config
[email protected]@

如果注销第一行,启动日志如下:

2018-09-10 18:13:14.837  INFO 19893 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://localhost:8888
2018-09-10 18:13:14.929  WARN 19893 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/big-goose-card-web-server/config/dev": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
2018-09-10 18:13:14.930  INFO 19893 --- [           main] com.puhui.goosecard.BootApplication      : The following profiles are active: dev

发现没有配置,默认连接本地8888端口,如果启用第一行,日志如下:

2018-09-10 18:16:14.471  INFO 19923 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://p.config.pub.puhuifinance.com
2018-09-10 18:16:15.715  WARN 19923 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://p.config.pub.puhuifinance.com/big-goose-card-web-server/config/dev": p.config.pub.puhuifinance.com; nested exception is java.net.UnknownHostException: p.config.pub.puhuifinance.com
2018-09-10 18:16:15.718  INFO 19923 --- [           main] com.puhui.goosecard.BootApplication      : The following profiles are active: dev
  • @config.url@ 等变量是maven的内置变量,配置在pom文件中
  • pom文件中默认激活的profile是dev   <activeByDefault>true</activeByDefault>
  • 三种profile,dev,test,production,方便切换属性配置文件
  <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!--默认固定启用的是config,增加环境配置的原因是为了SwaggerCondition区分不同环境的配置 -->
                <profiles.active>dev</profiles.active>
                <config.url>http://p.config.pub.puhuifinance.com</config.url>
                <config.branch>dev</config.branch>
            </properties>
            <dependencies>
                <dependency>
                    <groupId>io.springfox</groupId>
                    <artifactId>springfox-swagger-ui</artifactId>
                    <version>2.5.0</version>
                </dependency>
            </dependencies>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
                <config.url>http://p.config.pub.puhuifinance.com</config.url>
                <config.branch>test</config.branch>
            </properties>
            <dependencies>
                <dependency>
                    <groupId>io.springfox</groupId>
                    <artifactId>springfox-swagger-ui</artifactId>
                    <version>2.5.0</version>
                </dependency>
            </dependencies>
        </profile>
        <profile>
            <id>production</id>
            <properties>
                <profiles.active>production</profiles.active>
                <config.url>http://p.config.pub.puhuifinance.com</config.url>
                <config.branch>production</config.branch>
            </properties>
        </profile>
    </profiles>
  • git结合

  • 属性变更监听:利用git server,如果监听到git的push事件,发送rest请求通知其中一个service,接受到的service发送消息到spring cloud bus,其它service已经监听事件,接受到事件后自动refresh属性配置
  • 配置中心服务化:config server和其它服务一样都是注册到eureka server,进行统一管理,也能够进行负载均衡,故障切换等
  • spring cloud bus:目前集成支持了rabbitmq和kafka

猜你喜欢

转载自blog.csdn.net/hanruikai/article/details/82587492
今日推荐