There are many microservice projects in the actual project. For convenience, centralized management of configuration files is required. Next, I will write about the implementation of the distributed configuration center. Explain: Because it is relatively simple, we will directly talk about the high-availability configuration center here, and will not explain it in separate articles.
1. Implement the configuration center project
1. Create a project
Create a new spring boot project:
Select Config Server and Eureka Server:
The complete Pom.xml is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mayi.springcloud</groupId>
<artifactId>commonservice-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>commonservice-config</name>
<description>配置中心</description>
<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>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.M9</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<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>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
2. Modify the configuration
Here, we use the local method to implement the configuration center
bootstrap.yml
spring:
application:
name: service-config
cloud:
config:
server:
native:
search-locations:
- classpath:/shared/
profiles:
active:
- native
server:
port: 8888
Explanation:
- classpath: Indicates the configuration file storage path
- native: native mode
3. Code implementation
It is very simple to open a registry, just modify the startup class and add the @EnableConfigServer annotation .
package com.mayi.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class CommonserviceConfigApplication {
public static void main(String[] args) {
SpringApplication.run(CommonserviceConfigApplication.class, args);
}
}
2. Client Integration
1. Modify the configuration
From entry to mastery with spring-cloud 2.0 microservice architecture [3] Service provider/service consumer (ribbon)
The service-user microservice in this article is an example:
First add configuration dependencies to pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
Modify bootstrap.yml of service-user
spring:
application:
name: service-user
cloud:
config:
discovery:
enabled: true
service-id: service-config
profile: dev
label: master
explain:
service-id : Points to the microservice name of the configuration center, thus achieving high availability
profile: This configuration is used to facilitate the switching of configuration files for development, testing, and online environments
2. Add a configuration file to the configuration center
Add service-user-dev.yml to the shared directory in the service-config configuration center project
server:
port: 8801
Note: The name of this configuration file needs to follow the service-user's application.name + underscore + profile convention, otherwise it cannot be read
The directory after adding is as follows:
3. Test
Start Eureka Server --- service config --- service-user in turn
Enter: http://localhost:8761/
It is found that the service-user port is 8801. As can be seen from the above, the configuration of this port is read from the configuration center
Next, I will update the articles in turn until the whole structure is completed. If you are interested, please follow the author or add me on WeChat and pull you into the spring cloud community group.
WeChat public account: java architect practice
This official account will publish a complete architecture article according to the route of JAVA senior software architect actual combat training. The difficulty is from shallow to deep. It is suitable for those who have a certain development foundation and want to switch to architecture and are doing primary architecture development.