Getting started with spring-cloud-config to build a configuration center

Spring Cloud Config provides server and client support for external configuration in distributed systems. With Config Server, you can manage your application's external properties in all environments. Concept mapping on client and server is the same as Spring  Environmentand PropertySourceabstraction, so they fit well with Spring applications, but can be used with any application running in any language. As the application moves through the deployment process from developer to test and production, you can manage the configuration between these environments and be sure that the application has everything it needs to run when it migrates. The default implementation of the server storage backend uses git, so it easily supports tabbed versioned configuration environments, as well as access to various tools for managing content. Alternative implementations can easily be added and plugged in using Spring configuration.

spring_cloud_config_server

Create a new simplest springboot project named spring_cloud_config_server_demo.
Add the annotation @EnableConfigServer to enable the configuration center on the startup program of the project, and you can configure the port of the service and the address of the configuration center in the application.yml configuration file.
server:
  port: 8888
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/Haiyoung/spring_cloud_config_center.git

spring_cloud_config_center

Create a new git warehouse spring_cloud_config_center, and create a new file my-config.yml in the root directory of the warehouse. The content of the file is as follows:
test:
  name: haiyoung001
  city: shanghai

start spring_cloud_config_server

Run SpringCloudConfigServerDemoApplication, start the configuration service, and access it in the browser http://localhost:8888/my-config/master , the returned result is as follows:
{
    "name":"my-config",
    "profiles":[
        "master"
    ],
    "label":null,
    "version":"702f63a715c1875b25d94edbcbec50a3fe47e799",
    "state":null,
    "propertySources":[
        {
            "name":"https://github.com/Haiyoung/spring_cloud_config_center.git/my-config.yml",
            "source":{
                "test.name":"haiyoung",
                "test.city":"shanghai"
            }
        }
    ]
}
It can be seen that the configuration service is successfully started.

spring_cloud_config_client

新启一个最简单的springboot项目,命名为 spring_cloud_config_client_demo.
在 resources 目录下新建配置文件 bootstrap.yml,配置内容如下:
spring:
  application:
    name: my-config #要访问的配置文件
  cloud:
    config:
      uri: http://localhost:8888 #配置服务中心地址
server:
  port: 8080
  servlet:
    context-path: /config_client #自定义应用上下文
新建测试类 ConfigController:
/**
 * Created by Haiyoung on 2018/5/6.
 */
@RestController
public class ConfigController {

    @Value("${test.name}") //获取配置中的属性 name
    private String name;

    @Value("${test.city}") //获取配置中的属性 city
    private String city;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String helloSpringCloudConfig(){

        return "Hello SpringCloudConfig, name is "+name+", city is "+ city +".";
    }
}
运行 SpringCloudConfigClientDemoApplication,启动应用,浏览器中访问 http://localhost:8080/config_client/,返回如下所示:
Hello SpringCloudConfig, name is haiyoung, city is shanghai.
可见远程配置中心搭建成功,实现了最基本的功能。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325447011&siteId=291194637