微服务架构专题十:Spring-Cloud 之 config 分布式配置中心

一、config是什么?

我们既然要做项目, 那么就少不了配置,传统的项目还好,但是我们微服务项目, 每个微服务就要做独立的配置, 这样难免有点复杂, 所以, config项目出来了,它就是为了解决这个问题: 把你所有的微服务配置通过某个平台:
比如 github, gitlib 或者其他的git仓库 进行集中化管理(当然,也可以放在本地)。
可能这样讲有点抽象,我们来看一张图:大概是这样一个关系
在这里插入图片描述

二、怎么使用config?

刚刚讲完理论, 那么我们来实践一下, 怎么配置这个confi呢? 我们刚刚说过 由一个config server 来管理所有的配置文件, 那么我们现在新建一个config server 项目 然后引入依赖:

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

spring-cloud 的依赖我们就不提了,然后启动类上面加入注解EnableConfigServer:

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class AppConfigServer {

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

yml配置:

server:
  port: 8080

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:3000/eureka/  #eureka服务端提供的注册地址 参考服务端配置的这个路径
  instance:
    instance-id: config-1 #此实例注册到eureka服务端的唯一的实例ID
    prefer-ip-address: true #是否显示IP地址
    leaseRenewalIntervalInSeconds: 1
    leaseExpirationDurationInSeconds: 3

spring:
  application:
    name: config
  cloud:
    config:
      server:
        git:
          uri: https://github.com/405990230/server-demo.git #配置文件在github上的地址
          username: 1111  #连接git的用户名。
          password: 1111 #连接git的用户名密码。
          #Configserver会在 Git仓库根目录、 config-file子目录,以及所有以 bar开始的子目录中查找配置文件。
          search-paths: config-file,bar*
          #启动时就clone仓库到本地,默认是在配置被首次请求时,config server才会clone git仓库
          clone-on-start: true
          #配置为true表示如果本地副本是脏的,将使Spring Cloud Config Server强制从远程存储库拉取配置。
          force-pull: true
          

配置好以后,我们先试试通过config server来读取配置
这里我在github上有一些配置文件:
在这里插入图片描述
我们来看看test-config的内容:

在这里插入图片描述

那么如何通过config server来访问呢?
启动项目后, 我们可以通过名字来读取里面的配置信息:
在这里插入图片描述
那我们要获取dev环境/或者test环境下的配置呢? 通过-隔开即可。
我们现在来访问 test-config-dev:
在这里插入图片描述
同理 如果要访问test环境下的配置, 改为test即可
其实,config访问配置文件,是需要一个具体的访问规则的, 那么这个访问规则到底是什么呢? 我们可以在官网找到:

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

application就是配置文件的名字, profile就是对应的环境 label就是不同的分支 由这个规则可见, 我们使用的是第二种规则, 剩下的规则, 同学们可以自己去试试 , 对于yml 和properties类型config可以完美转换, 也就是说你存的是yml 但是可以读取为properties类型的反过来也是如此:

在这里插入图片描述

三、客户端从config上获取配置

刚刚给大家简单演示了一下config 以及怎么读取配置, 不过实际开发中,更多的不是我们人为去获取,而是由微服务从config上加载配置, 那么, 怎么来加载呢?
首先,我们需要在我们的微服务加入一个依赖声明他是config的客户端:

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

需要注意的是,这个依赖不包括spring -boot依赖, 也就是说, 假设你这个项目要当作spring boot来启动的话,还得依赖spring boot
启动类不需要做改动, 标准的spring boot启动类即可,需要注意的是yml文件
以前我们对于spring boot的配置 是在application.yml里面配置的,现在从config上读取配置的话,还得需要一个bootstrap.yml配置文件。

解释一下这个bootstrap.yml:
spring cloud有一个“引导上下文"的概念,这是主应用程序的父上下文。引导上下文负责从配置服务器加载配置属性,以及解密外部配置文件中的属性。和主应用程序加载application.(yml或 properties)中的属性不同,引导上下文加载(bootstrap.)中的属性。配置在 bootstrap.*中的属性有更高的优先级,因此默认情况下它们不能被本地配置。
那么我们application.yml配置文件里面 只需要做一些简单的配置就可以了:
这里我是通过注册中心获取信息从server-config服务获取配置信息

spring:
  application:
    name: client-user
    

重点在于bootstrap.yml:
在这里插入图片描述

spring:
  cloud:
    config:
      name: application-user #这是我们要读取的配置文件名 对应获取规则的{application}
      #profile: dev   #这个是要获取的环境 对应的便是{profile}
      label: master #这个就是获取的节点 对应的是{label}
      discovery:
        enabled: true   #是否通过注册中心服务
        service-id: config  #注册中心配置服务的名词
        #uri : http://localhost:8080/ #这就是我们config server的一个地址


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

启动时从server-config服务读取配置
在这里插入图片描述
远程配置application-user.yml的内容:

server:
  port: 5000
eureka:
  client:
    serviceUrl:
        defaultZone: http://localhost:3000/eureka/ #eureka服务端提供的注册地址 参考服务端配置的这个路径
  instance:
    instance-id: user-1 #此实例注册到eureka服务端的唯一的实例ID
    prefer-ip-address: true #是否显示IP地址
    leaseRenewalIntervalInSeconds: 10 #eureka客户需要多长时间发送心跳给eureka服务器,表明它仍然活着,默认为30(与下面配置的单位都是秒)
    leaseExpirationDurationInSeconds: 30 #Eureka服务器在接收到实例的最后一次发出的心跳后,需要等待多久才可以将此实例删除,默认为90秒

spring:
  application:
    name: client-user #此实例注册到eureka服务端的name

feign:
  hystrix:
    enabled: true

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 20000  #默认超时时间
      circuitBreaker:
        requestVolumeThreshold: 20
        sleepWindowInMilliseconds : 5000

management:
  server:
    port: 5008  #自定义actuator请求接口,不配置默认就是服务端口
  endpoints:
    web:
      exposure:
        include: "*"
    health:
      show-details: always
      

我们来测试一下 看看他会不会使用这个5000端口启动:
在这里插入图片描述
这里 我们查看启动信息,能发现他现在使用的是我们从config server上读取到的配置。
然后就可以正常访问了!
在这里插入图片描述

四、spring cloud config 高可用

config 高可用可以通过很多种方式, 比如说搭建一个nginx:
在这里插入图片描述

方式二:
或者config server注册到eureka上,client端也注册到eureka上,则已经实现高可用
如何注册就不提了,需要注意的点就是当config server都注册完之后 client的配置文件进行以下改动:
启动两个server-config服务,进行服务注册
在这里插入图片描述

spring:
  cloud:
    config:
      name: application-user #这是我们要读取的配置文件名 对应获取规则的{application}
      #profile: dev   #这个是要获取的环境 对应的便是{profile}
      label: master #这个就是获取的节点 对应的是{label}
      discovery:
        enabled: true
        service-id: config
        #uri : http://localhost:5088/ #这就是我们config server的一个地址


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

这样当其中一个配置服务出问题时,会自动去另外一个服务获取配置。
以上均为鲁班学院学习资料,欢迎大家报班学习,真心推荐!

发布了143 篇原创文章 · 获赞 49 · 访问量 25万+

猜你喜欢

转载自blog.csdn.net/weixin_36586564/article/details/104074796