SpringCloud the Configuration Center -Config

Config-Server

Spring Cloud Config is a new project Spring Cloud team created for distributed systems infrastructure and application services to provide centralized micro external configuration support, which is divided into server and client in two parts. Central server known as a distributed configuration, it is an independent micro-service applications, warehouse and configured to connect the client to provide access to configuration information, the encryption / decryption information access interface; micro-client architecture each micro service service applications or infrastructure, resources and applications to manage their business-related content through the configuration specified configuration center, and obtaining and loading configuration information from a configuration center at boot time.
Here Insert Picture Description

Spring Cloud Config achieve the abstract mapping of server and client environment variables and configuration properties, so it is applicable not only to build Spring applications outside, also can be used in any application to run in other languages. Since the Spring Cloud Config achieve configuration center default using Git to store configuration information, use the configuration server Spring Cloud Config constructed natural to support version management for micro-service application configuration information, and can be easily managed through the Git client tools configuration and access content. Of course, it also provides support for other storage methods, such as SVN repository, localization file system.

Getting Started

Configuration Services

  • A code cloud (GITEE) a new configuration file (optional GitHub, GitLab)

In the New Project code cloud config-server, create a new profile folder config in the new project, create application-test.properties in config, configuration file naming conventions should be used as much as possible: {application} - {profile} {properties | yml. }

Here Insert Picture Description

  • config-server/config/application-test.properties
name=mask
age=18
  • pom.xml
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
  • In application.properties project
server.port=8081
spring.cloud.config.server.git.uri=https://gitee.com/mask_0407/config-server.git
spring.cloud.config.server.git.username=****** #码云账号
spring.cloud.config.server.git.password=****** #码云密码
spring.cloud.config.server.git.search-paths=/config
  • App.java
@SpringBootApplication
@EnableConfigServer
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

After starting the project visit: HTTP: // localhost: 8081 / application-test.properties
Here Insert Picture Description

Config-Client

  • pom.xml
		<!--config client依赖-->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<!--ConfigurationProperties类所需依赖,手动添加的-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
  • Code cloud application-test.properties
server.port = 8082
name=mask
age=18
  • bootstrap.properties
server.port = 8082
spring.application.name = application #对应application-test.properties 中的application
spring.cloud.config.profile = test #对应application-test.properties 中的test
spring.cloud.config.uri=http://localhost:8081 # config-server 地址
# 开启所有的健康检查
management.endpoints.web.exposure.include=*
  • AppClient
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class AppClient {
	@Value("${server.port}")
	private String port;
	public static void main(String[] args) {
		SpringApplication.run(AppClient.class, args);
	}

	@RequestMapping("print")
	public String print() {
		return port;
	}
}

Startup Items visit http: // localhost: 8082 / print
Here Insert Picture Description

Published 19 original articles · won praise 8 · views 4537

Guess you like

Origin blog.csdn.net/M283592338/article/details/105043551