Spring cloud uses spring cloudconfig to build a remote configuration center

@[toc]

Spring cloud using spring cloudconfig to configure remote configuration center

Preface

With more and more services, more and more modules, more and more configuration files of our various services. At the same time, the way of multiple services using configuration files in projects is becoming more and more inadequate. It is often troublesome to change the configuration files at one time. At this time, spring cloudconfig appears, which makes us centralized configuration of configuration files and centralized management of the server, It is extremely convenient for the client to read the configuration file at one time and update the configuration file in real time by changing the push mode, which greatly liberates the productivity of long-term modification of multiple configuration files.

Remote git warehouse

First of all, you need to register a remote storage repository for storing configuration files.

Build a new git warehouse

Then create a new mysql configuration file One test A dev

Then submit to Github

Click the red area to get the git link

The contents are as follows

# Database driver:
driverClassName=com.mysql.jdbc.Driver
# Database link address:
url=jdbc:mysql://101.10.10.10:3306/db_app?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
#Database user name:
username=root 
# Database password: 
password=root 

Configure registry

Import config server dependency


	<!--    springcloud Dependency management-->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Greenwich.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
				<exclusions>
				</exclusions>
			</dependency>
		</dependencies>
	</dependencyManagement>


	<!--    Subfunctional dependency-->

	<dependencies>
<!--		config-server rely on-->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>

	</dependencies>
 

Configure the yml file

1. You need to configure to register this service in the registry. You can also register yourself here. Registering in the registry allows other services to read the configuration. It should be noted that when the config server is started, the remote git warehouse will be pulled to the local location. As for the local location, the following basedir configuration is used. 2. Remote git can use account password or configure private key access, for example, configure private key

#Specify service name
spring.application.name=config-server
#tomcat port
server.port=5001

#Configure service registry

#Configure git warehouse address
spring.cloud.config.server.git.uri=https://github.com/pomestyle/spring-config-server.git
#Configure warehouse Path
#spring.cloud.config.server.git.searchPaths=respo
#Configure branch of warehouse
spring.cloud.config.label=master
# User name to access git warehouse
#spring.cloud.config.server.git.username=your username
#User password to access git warehouse
#spring.cloud.config.server.git.password=your password



main startup class

Start the SpringCloudConfig server through @ enableconfig server in the program's start class ConfigServerApplication

//Enable configServer
@EnableConfigServer
@SpringBootApplication
public class SpringcloudconfigserverApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringcloudconfigserverApplication.class, args);
	}

}

Access profile

Start config server After startup, we can access the config server server server according to the following rules

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

Label: branch of GIT warehouse

Application: the file name of the configuration file, such as "clientOne" in clientOne.yml in my cruelty, corresponds to the application name of the service, which will be discussed later

profile: This is very familiar. The suffix of the configuration file can correspond to the service enabled environment

Access profile

  • Default access The default access is the master branch
  • Branch access

Access mysql-dev.properties

Visit mysql-test.properties

The configuration in yaml format is the same as that in properties format

perhaps http://127.0.0.1:5001/mysql/dev Visit

Config client client

Add dependency


        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!--eurekaClient-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

main method

//Start the eureka client service
@EnableEurekaClient

yml

Change application.properties to bootstrap.properties The bootstrap.properties file takes precedence over the application.properties file when the server starts and accesses the remote configuration center

# Cloud config client configuration
 
spring.application.name=config-server
#Indicates the branch of the remote warehouse
spring.cloud.config.label=master
spring.cloud.config.name=mysql-test
#Indicates the profile environment
#dev development environment configuration file
#Test test environment
#pro formal environment
spring.cloud.config.profile=test
#Indicates the web address of the configuration service center
spring.cloud.config.uri=http://127.0.0.1:5001/
server.port=8080

Get file content

The startup class ConfigClientApplication of the program passes @Value Get the content of the password value of the server

@SpringBootApplication
@RestController
public class SpringcloudconfigclientApplication {

    @Value("${password}")
    private String password;

    @GetMapping("/getMessage")
    public String getPassword(){
        System.out.println("password =  " + password);
        return this.password;
    }


    public static void main(String[] args) {
        SpringApplication.run(SpringcloudconfigclientApplication.class, args);
    }

}

Visit

2 use eureka as the registry configuration

Server configuration

The application.properties configuration file is basically unchanged

#Specify service name
spring.application.name=config-eureka-server
#tomcat port
server.port=5003
#Configure service registry
#Configure git warehouse address
spring.cloud.config.server.git.uri=https://github.com/pomestyle/springcloud.git
#Configure warehouse Path
spring.cloud.config.server.git.searchPaths=springcloud-config/spring-config-server
#Configure branch of warehouse
spring.cloud.config.label=master
# User name to access git warehouse
#spring.cloud.config.server.git.username=your username
#User password to access git warehouse
#spring.cloud.config.server.git.password=your password


#Add eureka registry address
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:6868/eureka/


Add eureka startup dependency

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

main method @ EnableEurekaClient starts the eureka server provider service

//Enable configServer
@EnableConfigServer
@EnableEurekaClient
@SpringBootApplication
public class SpringcloudconfigserverApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringcloudconfigserverApplication.class, args);
    }

}

Launch accessible

client

The configuration application name of the pull configuration application name spring. Application. Name = MySQL test is the name of the configuration file

bootstrap.properties configuration file

#tomcat port
server.port=5004

#password=12333333

# Pull configuration application name
spring.application.name=mysql-test

#springboot 1.5.X and above start to open security authentication by default
management.security.enabled=false

#Indicates the branch of the remote warehouse
spring.cloud.config.label=master
#Indicates the profile
spring.cloud.config.profile=dev

#Add eureka registry address
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:6868/eureka/
#Is to read files from the configuration center.
spring.cloud.config.discovery.enabled=true
#The serveid and service name of the configuration center. Go to Eureka registration center to find services through the service name
spring.cloud.config.discovery.serviceId=config-eureka-server

spring.cloud.config.server.git.uri: configure git warehouse address spring.cloud.config.server.git.searchPaths: configure the warehouse Path spring.cloud.config.label: branch of configuration warehouse spring.cloud.config.server.git.username: user name to access git warehouse spring.cloud.config.server.git.password: user password to access git warehouse eureka.client.serviceUrl.defaultZone: eureka registry address

main method start

@SpringBootApplication
@EnableEurekaClient
@RestController
@RefreshScope // The annotated class will automatically update the new configuration to the corresponding fields of the class when receiving the spring cloud configuration center configuration refresh.
public class SpringcloudconfigserverApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringcloudconfigserverApplication.class, args);
    }

    @Value("${password}")
    private String password;

    @GetMapping("/getMessage")
    public String getPassword() {
        System.out.println("password =  " + password);
        return this.password;
    }


}

Code address Portal

Tags: Programming Spring git MySQL Database

Posted on Wed, 18 Mar 2020 08:30:35 -0400 by shaundunne