Spring Cloud Config Distributed Configuration Center

Source: Internet
Author: User
Tags aop bind svn

Distributed systems, with a growing number of services, each time the service configuration changes, each service needs to shut down, restart, for operations, is unacceptable.
In order to facilitate the unified management of service profiles, real-time updates, you need a distributed configuration center. In spring cloud, there are Spring Cloud Config components that support the placement of configurations on remote servers and centralize the management of cluster configurations. In the Spring Cloud Config component, there are two roles, one config server and the config client

Spring Cloud Config uses git as the default configuration center and SVN support. For our team's historical reasons, use SVN as the configuration center


first, on SVN, build directory and related configuration file


Es-config-dev.properties Content

myconfig.version=1.0
Myconfig.source=dev #dev开发环境, Pro on-line environment, test testing environment
Myconfig.info=trunk

Ii. Creating a Spring Cloud Config Server service

Pom.xml Introduction Package

	<parent> <groupId>org.springframework.boot</groupId> <artifactId>
	spring-boot-starter-parent</artifactid> <version>2.0.1.RELEASE</version> <relativepath/> </parent> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding > <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version> 1.8</java.version> <spring-cloud.version>Dalston.SR3</spring-cloud.version> </properties > <dependencies> <!--Introduction to Configuration Center Package--<dependency> <groupid>org.springframework.cloud</ groupid> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency > <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud-starter-eureka</
		Artifactid> </dependency> <!--supports dynamic refresh configuration--<dependency>	<groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-actuator</ Artifactid> </dependency> <!--use SVN to do the configuration file version management, need to introduce SVNKITR package---<!--Https://mvnrepository.com/ar Tifact/org.tmatesoft.svnkit/svnkit-<dependency> <groupId>org.tmatesoft.svnkit</groupId> &

		lt;artifactid>svnkit</artifactid> <version>1.9.0</version><!----> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId> spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies > <dependencyManagement> <dependencies> <dependency> <groupid>org.springframework.cl Oud</groupid> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud
			.version}</version> <type>pom</type>	<scope>import</scope> </dependency> </dependencies> </dependencyManagement> <bui ld> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactid&
 gt;spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build>

Application.yml

Server:
  port:8888  #应用程序端口

Eureka:
  client:
    serviceurl:
      defaultzone:http://localhost : 1001/eureka/  #注册中心

Spring:
  application:
    name:spring-cloud-config-server  #应用程序名
  Cloud:
    config:
      enabled:true
      server:
        svn:
          uri:svn://xxxxxxxx/config-repo #svn地址
          Username:ershuai #svn用户名
          password:123456  #svn密码
        default-label:trunk  #trunk, directory
  of configuration files Profiles:
    active:subversion  #表示这是采用的svn进行的spring Cloud configuration

#动态刷新配置, configure ignore permissions to intercept
management:
  Security:
    enabled:false

Logging:
  levels:
    Org.springframework.boot.env.PropertySourcesLoader:TRACE
    Org.springframework.cloud.config.server:DEBUG

Start the registry, and then start Spring Cloud Config Server

Visit: Http://localhost:8888/es-config/dev


The URL that accesses the configuration information is mapped to the configuration file as follows:

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

/{application}-{profile}.yml

/{label}/{application}-{profile}.yml

/{application}-{profile}.properties

/{label}/{application}-{profile}.properties

According to the rules, multiple poses

Http://localhost:8888/es-config/dev/config

Http://localhost:8888/es-config/dev/trunk

Http://localhost:8888/es-config-dev.properties

...


Third, create the Spring Cloud Config Client service

Pom.xml Introduction Package

<parent> <groupId>org.springframework.boot</groupId> <artifactId> spring-boot-starter-parent</artifactid> <version>2.0.1.RELEASE</version> <relativepath/> <!--lookup parent from repository--</parent> <properties> <project.build.sourceencoding>u Tf-8</project.build.sourceencoding> <project.reporting.outputencoding>utf-8</ Project.reporting.outputencoding> <java.version>1.8</java.version> <spring-cloud.version> dalston.sr3</spring-cloud.version> </properties> <dependencies> <dependency> &LT;GROUPID&G
		T;org.springframework.cloud</groupid> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactid&gt ;spring-cloud-starter-eureka</artifactid> </dependency> <dependency> <grouPid>org.springframework.boot</groupid> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--supports dynamic refresh configuration--<dependency> <groupid>org.springframework.boot</groupid > <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--support the retry mechanism, as well as the need to open Spring.cloud.config.fail-fast=true-<dependency> <groupid>org.springframework.retry</groupid > <artifactId>spring-retry</artifactId> </dependency> <dependency> <groupid>org .springframework.boot</groupid> <artifactId>spring-boot-starter-aop</artifactId> </
			Dependency> <!--support retry mechanism--<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency > </dependencies> &LT;DEPENDENCYMANAGEMENT&GT <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactid >spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type >pom</type> <scope>import</scope> </dependency> </dependencies> </dependency management> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid > <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> &LT;/BUILD&G T

Application.yml

Server:
  port:8889  #应用程序端口

Bootstrap.yml. Why Bootstrap.yml, you can understand the definition of application.yml and BOOTSTRAP.YML

eureka:client:serviceurl:defaultzone:http://localhost:1001/eureka/spring:applicati   On:name:spring-cloud-config-client Cloud:config:fail-fast:true #失败快速响应 is also the precondition of the retry mechanism enabled:true #高可用配置中心通过ervice-id to automatically discover Config-server service group Service-id:spring-cloud-config-server #高可用配置中心通过服务Id去自动发现config-server Service Group Name:es-config #对应 {application} section, corresponding to Es-config-dev.properties Profile:dev #对应 {profile} section, corresponding to Es-config-dev . properties, development environment configuration Dev, testing environment configuration test, online environment Configuration Pro #uri: http://localhost:8888/#配置中心的具体地址 default-label:trunk #tru NK, config file directory #动态刷新配置, configure ignore permissions to intercept Management:security:enabled:false # # # Retry Parameters # # # #spring. Cloud.config.retry.max-atte The mpts=6 configures the retry count by default to 6 #spring. cloud.config.retry.multiplier=1.1 interval Multiplier, default 1.1 #spring. Cloud.config.retry.initia  l-interval=1000 Initial retry interval, default 1000ms #spring. cloud.config.retry.max-interval=2000 maximum interval, default 2000ms # # # retry parameter # # # 

Create a controller

Package Com.ershuai.controller;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.beans.factory.annotation.Value;
Import Org.springframework.cloud.context.config.annotation.RefreshScope;
Import org.springframework.core.env.Environment;
Import org.springframework.web.bind.annotation.RequestMapping;

Import Org.springframework.web.bind.annotation.RestController; /** * * @author Ershuai * @date April 18, 2018 PM 2:50:32 */@RefreshScope//Allow dynamic Refresh configuration @RestController public class Config
 
    Clientcontroller {@Value ("${myconfig.version}") private String version;
    @RequestMapping ("/version") public String version () {return this.version;
	
	} @Autowired private Environment env;
	@RequestMapping ("/myconfig-env") public String myconfigenv () {return Env.getproperty ("Myconfig.version"); }//@RequestMapping ("/myconfig-env/{key}")//public string myconfigenv (@PathVariable ("key") string key) {////////can also be env to get/if (StringutiLs.isblank (key)) {//return null;//}//Return Env.getproperty (key); //	}
}

Visit: http://10.36.65.204:8889/myconfig-env



Four, dynamic refresh configuration

Modify the. properties file locally to submit the SVN

Access by post : Http://10.36.65.204:8889/refresh


Visit again: http://10.36.65.204:8889/myconfig-env


Configuration changes without restarting the server


v. Retry mechanism

Client side, Pom.xml, introduce the following package

<!--support the retry mechanism, as long as you need to turn Spring.cloud.config.fail-fast=true--
		<dependency>
			<groupId> org.springframework.retry</groupid>
			<artifactId>spring-retry</artifactId>
		</ dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			< artifactid>spring-boot-starter-aop</artifactid>
		</dependency>
		<!--support retry mechanism--

BOOTSTRAP.YML, adding configuration

Spring:  
  Cloud:
    config:
      fail-fast:true #失败快速响应, also a prerequisite for the retry mechanism

#动态刷新配置, configure the Ignore permissions to intercept
management:
  Security:
    Enabled:false

Test. Close the server side and start client only, and you will find that 6 connections are retried by default




Reference: HTTP://WWW.SHUAIFENGJIE.COM/BLOG/859AC3F640444B85B88449C79155EDDB

Reference: https://segmentfault.com/a/1190000010180245

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.