springcloud-config配置中心

Spring Cloud Config为分布式系统中的外部化配置提供服务器端和客户端支持。使用Config Server,您可以在中心位置管理所有环境中应用程序的外部属性。客户端和服务器上的概念与Spring Environment和PropertySource抽象,因此它们非常适合Spring应用程序,但可以与以任何语言运行的任何应用程序一起使用。在应用程序从开发人员到测试人员再到生产人员的整个部署过程中,您可以管理这些环境之间的配置,并确保应用程序具有它们迁移时所需的一切。服务器存储后端的默认实现使用git,因此它轻松支持带标签的配置环境版本,并且可通过各种工具来访问以管理内容。添加替代实现并将其插入Spring配置很容易。

config server

  1. 父工程下创建一个项目
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud-demo</artifactId>
        <groupId>demo.cloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-server</artifactId>

    <name>config-server</name>

    <dependencies>
    	<!-- config server依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>

</project>
  1. 修改application.yml文件
server:
  port: 8888

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          #git仓库地址
          uri: https://github.com/dean4lee/springcloud-demo
          #仓库地址下的路径,多个用,隔开
          search-paths: conf
          #用户名
          username:
          #密码
          password:

git仓库地址有3个文件

  • hello-dev.yml
  • hello-pro.yml
  • hello-test.yml
  1. 开启配置服务器
@SpringBootApplication
//开启配置服务器
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}
  1. 测试
    浏览器访问http://localhost:8888/hello/dev
{
  "name": "hello",
  "profiles": [
    "dev"
  ],
  "label": null,
  "version": "5ffedb8bb6174039200a961d311fe2a6976d367f",
  "state": null,
  "propertySources": [
    {
      "name": "https://github.com/dean4lee/springcloud-demo/conf/hello-dev.yml",
      "source": {
        "user.username": "admin",
        "user.password": 12345
      }
    }
  ]
}

直接访问文件数据http://localhost:8888/hello-dev.yml

user:
password: 12345
username: admin

仓库中的配置文件会被映射成web接口:

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

hello-dev.yml中application为hello,profile为dev

config client

  1. 在父工程下创建一个项目
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud-demo</artifactId>
        <groupId>demo.cloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-client</artifactId>

    <name>config-client</name>

    <dependencies>
        <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>
    </dependencies>

</project>
  1. 创建一个bootstrap.yml
spring:
  cloud:
    config:
      #配置服务器地址
      uri: http://localhost:8888
      #对应{application}
      name: hello
      #对应{profile}
      profile: dev
      #git分支
      label: 
      #如果有密码验证
      username:
      password:

这些参数必须设置在bootstrap.yml中才能正确加载,由于config配置优先于application.yml文件加载

  1. application.yml
server:
  port: 8880

spring:
  application:
    name: config-client
  1. 创建测试类
@RestController
public class TestCtrl {

    @Value("${user.username}")
    private String username;

    @GetMapping("getUsername")
    public String getUsername(){
        return username;
    }
}
  1. 测试
    浏览器访问http://localhost:8880/getUsername

admin

项目路径


作者博客

作者公众号
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值