spring cloud config 配置管理中心

yls
2020/5/5

创建配置管理中心

1.添加依赖包

        <!--分布式配置中心 start-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!--分布式配置中心 end-->

2.在启动类上添加注解@EnableConfigServer

3.创建配置文件application.yml

spring:
  application:
    name: itoken-config
  cloud:   #分布式配置中心,将配置文件保存到git远程仓库上,访问方式:http://ip:port/{application}/{profile}[/{label}]
    config:
      label: master    #git分支
      server:
        git:
          uri: https://github.com/1612480331-itoken/itoken-config.git    #仓库地址,需要在远程仓库自行创建
          search-paths: respo #仓库中放配置的目录
          username:  *     #远程仓库用户名
          password: *     #远程仓库密码

server:
  port: 8888

4.在仓库的respo目录下添加配置文件,一般在本地创建好再上传到仓库

注意文件的名称不是乱起的,例如 config-single-client-dev.yml 和 config-single-client-prod.yml
这两个是同一个项目的不同版本,项目名称为 config-single-client, 一个对应开发版,一个对应正式版。
config-eureka-client-dev.yml 和 config-eureka-client-prod.yml 则是另外一个项目的,
项目的名称就是 config-eureka-client

5.启动服务,通过浏览器直接访问配置文件,例如:访问 http://localhost:8888/itoken-eureka/dev/master

Spring Cloud Config 有它的一套访问规则,我们通过这套规则在浏览器上直接访问就可以。

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
{application} 就是应用名称,对应到配置文件上来,就是配置文件的名称部分,例如我上面创建的配置文件。

{profile} 就是配置文件的版本,我们的项目有开发版本、测试环境版本、生产环境版本,对应到配置文件上来就是以 application-{profile}.yml 加以区分,例如application-dev.yml、application-sit.yml、application-prod.yml。

{label} 表示 git 分支,默认是 master 分支,如果项目是以分支做区分也是可以的,那就可以通过不同的 label 来控制访问不同的配置文件了。

上面的 5 条规则中,我们只看前三条,因为我这里的配置文件都是 yml 格式的。根据这三条规则,我们可以通过以下地址查看配置文件内容:

http://localhost:3301/config-single-client/dev/master

http://localhost:3301/config-single-client/prod

http://localhost:3301/config-single-client-dev.yml

http://localhost:3301/config-single-client-prod.yml

http://localhost:3301/master/config-single-client-prod.yml

通过访问以上地址,如果可以正常返回数据,则说明配置中心服务端一切正常。

微服务使用配置管理中心

1.添加依赖包

<!--分布式配置中心 start-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!--分布式配置中心 end-->

2.添加application.yml配置文件

spring:
  cloud:
    config:   #云配置
      uri: http://localhost:8888  #spring cloud config云配置服务ip+端口
      name: itoken-eureka   # 服务名称
      label: master  #git仓库版本
      profile: prod  #配置文件类型,dev,test,prod
      # 下面一部分是通过注册中心连接配置中心,与上面的url直连是一样的效果,推荐使用下面这个
      #discovery:
        #service-id: itoken-config #配置中心的服务名称
        #enabled: true

3.启动项目,在日志中查看是否配置成功

在线更新配置信息(只有使用了 bus-amqp才行,本文未使用,可以不看)

如果要更新所有客户端的配置,使用如下指令

curl -X POST http://local:8888/bus/refresh

这条指令可以在运行配置管理中心的机器上运行。
如果要远程执行,将localhost改为相关的ip地址

如果只更新某一个应用的配置,执行如下指令

#orderweb表示要更新的应用名称
curl -X POST http://local:8888/bus/refresh?destination=orderweb:**

注意:
并不是所有配置信息都能在线更新生效,例如有关连接数据源的配置,在线更新不能修改,因为应用启动时已经建立了数据库的连接

如果配置是通过程序来动态读取的,必须在程序的开头加上 @RefreshScope才能生效

@RestController
@RefreshScope
public class Test1{
@Value("${port}") String port;
}
原文地址:https://www.cnblogs.com/yloved/p/12832076.html