springcloud之config配置中心的使用

 

一、配置中心服务config server

(1) 配置中心是专门起一个服务。

在启动类上添加@EnableConfigServer注解即可,主要操作的是application.yml配置

  (2) 使用spring.cloud.config.server.git.uri 指定github或者其他git库的地址。

作用:其他服务通过当前服务(配置中心)间接去加载github上的yml配置文件。

(反正就是说,其他服务不能直接去git仓库获取自己的配置,在其他服务的角度来说,是从当前配置中心服务获得的配置文件)

二、普通springcloud服务从config server配置中心服务加载yml配置文件。

(1)首先了解一下bootstrap.yml和application.yml的

  • 加载顺序的先后,bootstrap会先加载,在结合configserver一起使用时,bootstrap属性文件更适合做一些引导下文的配置,比如下面这个就是普通服务从服务中心加载yml的配置,这个就得放在bootstrap.yml中。
# 默认配置
spring:
  application:
    name: demo
  cloud:
    config: #配置文件获取
      uri: http://localhost:8040
      label: management
      name: application-demo
  • 在结合configserver使用时,bootstrap更多的是配置一些不经常变化甚至不变化的属性。application属性文件可更灵活的配置一些属性。

(2)在普通服务中从配置中心服务加载配置文件,在bootstrap.yml配置如下:

spring:
  application:
    name: demo   #当前服务名称(这个可以放在application.yml里配置)
  cloud:
    config: #配置文件获取
      uri: http://localhost:8040   #不使用服务发现(eureka等),则直接通过uri指定配置中心的地址
      label: master         #github仓库的分支名(默认应该就是master,其他分支就在这指定)
      name: config-file-name  #name指定想要从从配置中心加载的配置文件名,不用加后缀,获取多个则以逗号隔开

以上是不使用服务发现的配置,如果要使用服务发现,则看更后面....

(3)这样就完成了,普通服务被启动时会先加载bootstrap.yml,然后根据bootstrap.yml的内容从配置中心加载配置文件,然后启动成功

 

注意:上面的(2)其实直接在url上面写ip+port不太好,如果配置中心服务的ip或者端口换了,那么所有使用该配置中心的其他服务都要更改ip或者端口。

查看了官方文档有另一种方法:把配置中心服务(config server)注册到eureka上,其他服务通过服务发现访问配置中心。至于怎么注册到eureka这里就不说了,和普通服务注册是一样的步骤。这里讲讲其他服务怎么通过服务发现discovery从配置中心加载配置的。。其他服务的bootstrap.yml配置如下:(别直接复制,直接复制的话注意一下空格有没有让yml生效)

spring:
  application:
    name: demo   #当前服务名称(这个可以放在application.yml里配置)
  cloud:
    config: 
      discovery:   #配置服务发现
        serviceId: my-config-server #服务发现(eureka)中,配置中心(config server)的服务名
        enabled: true  #是否启动服务发现
      label: master         #github仓库的分支名(默认应该就是master,其他分支就在这指定)
      name: config-file-name  #name指定想要从从配置中心加载的配置文件名,不用加后缀,获取多个则以逗号隔开

注意到了吧,uri被删除了,取而代之的是discovery.serviceId和discovery.enabled,其中discovery.enabled默认为false,设置为true是希望当前服务使用服务发现(discoveryClient)查找配置服务中心,而discovery.serviceId就是配置服务中心的名称标识了(spring.application.name,这个不会不懂吧?)

可能我说的不好,那么,请出官方文档(谷歌翻译中文版):

begin:

如果您使用DiscoveryClient实施,例如Spring Cloud Netflix和Eureka Service Discovery或Spring Cloud Consul,您可以将Config Server注册到Discovery Service。但是,在默认的“ Config First ”模式下,客户端无法利用注册。

如果您希望用于DiscoveryClient查找配置服务器,可以通过设置spring.cloud.config.discovery.enabled=true(默认设置false)来实现。这样做的最终结果是客户端应用程序都需要bootstrap.yml具有适当发现配置的(或环境变量)。例如,使用Spring Cloud Netflix,您需要定义Eureka服务器地址(例如,in eureka.client.serviceUrl.defaultZone)。使用此选项的价格是启动时额外的网络往返,以查找服务注册。好处是,只要发现服务是固定点,配置服务器就可以更改其坐标。默认服务ID是configserver,但您可以通过设置spring.cloud.config.discovery.serviceId(在服务器上,以通常的方式设置服务,例如通过设置spring.application.name)在客户端上更改它。

end。

翻译的看不明白?那四六级大神看英文版吧:

begin:

If you use a DiscoveryClient implementation, such as Spring Cloud Netflix and Eureka Service Discovery or Spring Cloud Consul, you can have the Config Server register with the Discovery Service. However, in the default “Config First” mode, clients cannot take advantage of the registration.

If you prefer to use DiscoveryClient to locate the Config Server, you can do so by setting spring.cloud.config.discovery.enabled=true (the default is false). The net result of doing so is that client applications all need a bootstrap.yml (or an environment variable) with the appropriate discovery configuration. For example, with Spring Cloud Netflix, you need to define the Eureka server address (for example, in eureka.client.serviceUrl.defaultZone). The price for using this option is an extra network round trip on startup, to locate the service registration. The benefit is that, as long as the Discovery Service is a fixed point, the Config Server can change its coordinates. The default service ID is configserver, but you can change that on the client by setting spring.cloud.config.discovery.serviceId(and on the server, in the usual way for a service, such as by setting spring.application.name).

end。

最后,如果还看不明白,给你官方文档的链接,点进去可以直接跳到这段话(不要和我说你没看到链接)

 

 

 

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值