Spring Cloud Learning-Configuration Center (config)

Source: Internet
Author: User
Tags using git

Spring Cloud Learning-Configuration Center (config)
    • A brief introduction to Spring Cloud Config
    • Two write Config Server
    • Three write config Client
    • Four manually refresh the configuration using the Refresh Endpoint
    • Five Spring Config server works with Eurelka
    • High availability of six Config servers

I. Introduction to Spring Cloud Config

Micro-service to achieve centralized management of micro-service configuration, different environments, different configurations, can be dynamically adjusted during operation, configuration changes can be automatically updated after the requirements, Spring Cloud Config also meet the above requirement. The Spring Cloud config is divided into config server and config client, which is a scale-out, centralized configuration servers, using Git to store configuration content by default.

Schematic diagram of Spring Cloud Config:

Second, write Config Server

1. Create a new repository on GitHub and add the configuration file.

2. To create a new spring boot project, add the following dependencies in Pom.xml:

    <Dependencies><Dependency><Groupid>org.springframework.cloud</Groupid><Artifactid>spring-cloud-config-server</Artifactid></Dependency><Dependency><Groupid>org.springframework.boot</Groupid><Artifactid>spring-boot-starter-web</artifactid> </ dependency> <dependency> <groupid>org.springframework.boot</< Span class= "Hljs-title" >groupid> <artifactid> Spring-boot-starter-test</artifactid> <scope>test</ scope> </dependency> </DEPENDENCIES>       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

3. Add @enableconfigserver annotations on the startup class to indicate that the class is a config Server

@SpringBootApplication@EnableConfigServerpublic class SpringConfigServerApplication { public static void main(String[] args) { SpringApplication.run(SpringConfigServerApplication.class, args); }}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

4. Configuring the Application.yml File

server: port: 8001spring: cloud:  config:   server:    git:     uri: https://github.com/songxiansen521/spring-cloud-config-repo.git username: ****your git name**** password: ****your git pw****
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

5. Start the project, Access Http://localhost:8001/microservice-foo/dev, see the following page, Config Server configuration succeeded.

6. About the endpoint of config server.
You can use the endpoint of config server to get the contents of the configuration file, which is the following mapping rule:

/{application}/{profile}[/{label}]
    • 1

This example: Http://localhost:8001/microservice-foo/dev

/{application}-{profile}.yml/{application}-{profile}.properties
    • 1
    • 2

This example: http://localhost:8001/microservice-foo.properties

/{label}/{application}-{profile}.yml/{label}/{application}-{profile}.properties
    • 1
    • 2

This example: http://localhost:8001/config-label-v1.0/microservice-foo-dev.properties

Three write config Client

1. Create a new spring boot project and add the following 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><Dependency><Groupid>org.springframework.boot</Groupid><Artifactid>spring-boot-starter-actuator</artifactid> </ dependency> <dependency> <groupid>org.springframework.boot</< Span class= "Hljs-title" >groupid> <artifactid> Spring-boot-starter-test</artifactid> <scope>test</ scope> </dependency> </DEPENDENCIES>       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

2. Writing the configuration file application.yml

server.port=8002
    • 1

3. Write the configuration file bootstrap.yml. The properties that are configured in Bootstrap.xml have a higher priority and are not overwritten locally by default.

spring: application:  #对应config server中配置文件的{application}  name: microservice-foo cloud:  config:    #访问config server的地址    uri: http://localhost:8001    #对应config server中配置文件的{profile}    profile: dev    #对应config server中配置文件的{label} label: master
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

4. Add the Controller class,

Package Com.swc.controller;import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import Org.springframework.web.bind.annotation.RestController; /** * Created by Chao on 2017-11-8. */ @RestController public class  Configclientcontroller { @Value ( "${profile}") private String profile;  @GetMapping ( "/getprofile")  Public String hello () {return  This.profile; }}  
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22

4. Launch the application, Access Http://localhost:8002/getProfile, and get the following page, stating that the client can successfully get the configuration of the corresponding environment in the GIT Repository via config server.

Iv. manually refreshing the configuration using the/refresh endpoint

Many times, you need to dynamically adjust the configuration during run time, and you can use/refresh to implement a refresh of the microservices configuration.
In the Config client project above, I have added a dependency:

<dependency>            <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId></dependency>
    • 1
    • 2
    • 3
    • 4

Add @refereshscope on the controller, which will be handled specially when the configuration changes.

Package Com.swc.controller;Import Org.springframework.beans.factory.annotation.Value;Import Org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.web.bind.annotation.GetMapping; import Org.springframework.web.bind.annotation.RestController; /** * Created by Chao on 2017-11-8. */ @RestController  @RefreshScope public class configclientcontroller { @Value ( "${profile}") private string profile;  @GetMapping ( "/getprofile")  Public String hello () {return  This.profile; }} 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22

3. Start the project, Http://localhost:8002/getProfile access to modify the contents of the configuration file in the Git repository, visit again and find the results unchanged.
At this point, a manual refresh is required: Access Http://localhost:8001/refresh as a POST request
Third access, results, configuration refresh

Five. Spring Config server works with Eurelka

Both the config server and the config client are registered on the Eureka server.
Modify Config Client

Spring:application: #对应config config file in server {application} Name:microservice-foo Cloud:config:  address of the #访问config server  #uri: http://localhost:8001  #对应config configuration file in server {profile} Profile:dev  #对应config server configuration {label} label:master discovery:  #表示使用服务发现组件中提供的Config Server, default is False  #开启通过服务发现组件访问Config server function enabled: true  #指定Config server Serviceid in the Service Discovery component is Configserver Service-id:microservice-config-server-eurekaeureka: client:service-url:defaultzone:http://localhost:8888/eureka/          
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22

Modify Config Server

server: port: 8001spring: appliation:  name: microservice-config-server-eureka cloud:  config:   server:    git:     uri: https://github.com/songxiansen521/spring-cloud-config-repo.git username: ***your git name*** password: ***your git pw***eureka: client: service-url: defaultZone: http://localhost:8888/eureka/
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

Start Eureka Server,config Server and config client three items to get the contents of the configuration file in the Git repository.

Vi. High availability of Config server

The high availability of Config server can be achieved with load balancing, which is illustrated by the following schematic:

The high availability of Config server can also be achieved with Eureka.

Spring Cloud Learning-Configuration Center (config)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.