springCloud configure the local distribution center SpringCloudConfig

Multi-environment configuration

In a typical development process if debugging proceed locally, the configuration may be provided more than one environment, switching back and forth between the local and online configuration.

springcloud default access configuration file name is application.properties,

If we want to create a multi-environment configuration file, the file name format should be: application- {profile} .properties

{Profile} which is used to identify the different environment, such as the file can be used to configure application-native.properties local environment, application-prod.properties file can be used to configure a production environment.

springcloud to specify {profile} By "spring.profiles.active" attributes, such as spring.profiles.active = native, using the application-native.properties profile.

 

Establish a local distribution center

Configuration Center is divided into two parts: the client and server.

Server is a separate service, you need to be registered to the registry.

And the client is present in each of the other service.

 

Server configuration center can either save the configuration file in a remote git repository, you can save these configuration files locally, as usual with the development of more local distribution center, the main talk of setting up a local distribution center.

 

Server

First establish a distribution center in the boot entry:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class ConfigServer {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(ConfigServer.class, args);
    }

}

 

Its pom file only need to add the following dependencies:
 

<dependency>
<groupId>org.springframework.cloud</groupId> 
<artifactId>spring-cloud-config-server</artifactId> 
</dependency>

Profile application.properties as follows:

#配置中心端口
server.port=8887
spring.application.name=config-server
spring.profiles.active=native
#申明本地配置文件的存放位置
spring.cloud.config.server.native.searchLocations=file:D:\\etc\\native
#注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
#刷新时,关闭安全验证
management.security.enabled=false
#开启消息跟踪
spring.cloud.bus.trace.enabled=true
启动后,此服务端会被注册到注册中心。

Client

The client exists in all services, only need to create a profile bootstrap.properties in need of services, you can obtain the relevant configuration from the configuration center server.

bootstrap.properties:

#配置中心的地址

spring.cloud.config.uri=http://localhost:8887/

#对应的是配置文件规则中的{application}部分

spring.cloud.config.name=native

#对应的是配置文件规则中的{profile}部分,可以是多个,用逗号隔开。

spring.cloud.config.profile=common,mysql,activemq

#对应的是配置文件规则中的{label}部分,本地可以不写

spring.cloud.config.label=master

As can be seen by the client bootstrap.properties profile, in fact, which wrote two things:

spring.cloud.config.uri center address is configured, positioned and arranged to center.

The remaining three are "rule profile", used to locate the "specific profile."

Let us concerned about, what is the "Profile Rule":

Before we talk about the configuration center server when the definition of a spring.cloud.config.server.native.searchLocations = file: D: \\ etc \\ native parameters

The hard path is filled with specific configuration files, and each client is to fetch these profiles. Specific file as shown below:

 

 

We can see from the figure, all of the files naming convention which follow: {application} - {profile} .properties

So we spring.cloud.config.name = native client bootstrap.properties parameter file, in fact, in order to match {application} prefix the configuration file,

Therefore spring.cloud.config.profile = common, mysql, activemq matching parameter is a suffix {profile}.

 

So the configuration center client examples above are actually only get native-common.properties, native-mysql.properties, native-activemq.properties three profiles.

Guess you like

Origin blog.csdn.net/caidingnu/article/details/90698113