Spring Cloud之 Spring Cloud Config(一)

17 篇文章 0 订阅
8 篇文章 0 订阅

 一、介绍

Spring Cloud Config在分布式系统中提供服务端和客户端去支持额外的外部配置。配置服务中心(Config Server)为所有应用提供各种环境的外部配置。默认采用Git的方式存储配置文件,因此我们很容易部署修改,有助于对环境配置进行版本管理。

二、构建config server

创建一个spring boot 项目,在pom.xml添加spring-cloud-config-server 依赖

 

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

在程序的入口SpringCloudConfigServerApplication类加上@EnableConfigServer注解开启配置服务器。

 

 

 

 

@EnableConfigServer
@SpringBootApplication
public class SpringCloudConfigServerApplication {

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

 

 

 

创建一个application.yml 文件,内容如下

server:
  port: 8888
spring:
  cloud:
    config:
      server:
        git:
          # uri: https://github.com/luosai001/SpringCloudConfigRepo
          uri: https://github.com/luosai001/config-repo.git  # default uri 当匹配不到仓库时,用这个默认的uri
          search-paths: file # 查找仓库根目录和file 目录, 应用正则表达式
          username: luosai001
          password: ******

          repos:  # 配置多个仓库,各个应用通过模式匹配找到对应的仓库,读取配置
            project-a:
              pattern: spring-cloud-config-client/*   # {application}/{profile}
              uri: https://github.com/luosai001/SpringCloudConfigRepo.git
            project-b:
              pattern: project-b/* # {application}/{profile}
              uri: https://github.com/luosai001/config-repo.git
              search-paths: file  # 查找仓库根目录和file 目录, 应用正则表达式


 

uri是git仓库,用来存储配置信息的

username 是git仓库用户名

password 是git仓库密码

启动应用 访问localhost:8888

创建一个远程仓库,本示例在github上创建了一个仓库仓库地址为 https://github.com/luosai001/SpringCloudConfigRepo

在本地上传了一个文件到仓库,文件名为spring-cloud-config-client-dev.yml 

文件配置信息如下:

 

server:
  port: 8003 #应用端口号
  
spring:
    cloud:
        config:
            allowOverride: true
            failFast: true
            overrideNone: true #覆盖非系统属性 true不覆盖
            overrideSystemProperties: false #覆盖系统属性 false不覆盖
 

foo: i am foo,but i changed! changed!changed!

 

 

 

访问localhost:8888/spring-cloud-config-client/dev 

返回信息

{
    "name":"spring-cloud-config-client",
    "profiles":[
        "dev"
    ]
,
    "label":null,
    "version":"cadbabdf73cc54dc8b56afe84d47554efe585cb9",
    "state":null,
    "propertySources":[
        {
            "name":"https://github.com/luosai001/SpringCloudConfigRepo/spring-cloud-config-client-dev.yml",
            "source":{
                "server.port":8003,
                "spring.cloud.config.allowOverride":true,
                "spring.cloud.config.failFast":true,
                "spring.cloud.config.overrideNone":true,
                "spring.cloud.config.overrideSystemProperties":false,
                "foo":"i am foo,but i changed! changed!changed!"
            }
        }
    ]

}

 

 

http请求地址和文件映射如下:

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

 

普通文本读取: /{name}/{profile}/{label}/{path}

name: 应用名

profile:激活的配置

lable:哪个分支

path:普通文本文件名

三、创建Config Client 

创建一个spring boot 项目 添加依赖spring-cloud-starter-config,spring-boot-starter-web,spring-boot-starter-actuator(刷新配置)

 

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

 

 

 

程序入口类:

   

@SpringBootApplication
@RestController
@RefreshScope
public class SpringCloudConfigClientApplication {
   @Value("${foo}")
   String foo ;
   public static void main(String[] args) {
      SpringApplication.run(SpringCloudConfigClientApplication.class, args);
   }
   @RequestMapping("hi")
   public String hi(){
      return foo ;
   }
}

 

在这个主入口中,我们将foo通过${foo}注入给foo变量,并返回。 @RefreshScope 指可以刷新配置。如果配置中foo 的值改变了,在本地刷新 /refresh ,再次访问/hi 就会发现返回的值变了。

 

创建一个bootstrap.yml文件

 

spring:
  application:
    name: spring-cloud-config-client
  profiles:
    active: dev
  cloud:
    config:
      uri: http://localhost:8888
      profile: dev

management:
  security:
    enabled: false
server:
  port: 9000

 

应用名为spring-cloud-config-client 

激活的文件:dev

uri:配置中心地址

management.security.enabled:false 调用/refresh刷新的时候需要。否则会没有权限访问

启动应用 访问:localhost:9000/hi 返回配置里定义的信息。

在远程外部配置文件中我们开启了不覆盖本地系统属性,不覆盖本地非系统提供属性,所以启动时,远程外部配置中端口并没有作为应用的端口。默认都是覆盖,即远程port 8003 会覆盖本地9000端口。

 

修改外部配置foo的值,需要手动调用localhost:9000/refresh才能刷新foo的值。如果一个值被多个进程使用,那么这几个服务都要手动调用/refresh方法,很麻烦。下一节我们将通过Spring Cloud Bus 来解决这个问题,只需要调用一次即可。

 

参考文档 https://cloud.spring.io/spring-cloud-config/single/spring-cloud-config.html

 

 

 

 

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值