Brief introduction
Spring Cloud provides a platform for deploying microservices, including components that are common in MicroServices: Configuration Center Services, API gateways, circuit breakers, service registration and discovery, distributed traceability, OAuth2, consumer-driven contracts, and more. We don't have to know what each component does, and as the tutorials go deeper, we're getting to them. The general structure of a distributed service is shown (image from: Spring.io):
Using spring Cloud to build a distributed system is simple, and we can start a series of components in just a few simple configurations, and then control, use, and manage those components in your code. Spring Cloud uses spring boot as the base framework, and you can refer to my previous blog for a tutorial on how to create a Spring boot project, spring boot 2.0.1. This tutorial teaches you how to configure the service center service and read the configuration through a Web client.
Basic Environment
- JDK 1.8
- Maven 3.3.9
- IntelliJ 2018.1
- Git
Project Source
Gitee Code Cloud
Create a Web Client
First create a MAVEN project with IntelliJ, the Pom.xml file reads 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> Cn.zxuqian</groupid> <artifactId>web</artifactId> <version>1.0-SNAPSHOT</version> & Lt;parent> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter- parent</artifactid> <version>2.0.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> spring-cloud-starter-config</artifactid> </dependency> <dependency> <groupid >org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> ; <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-web</artifacti d> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artif Actid>spring-cloud-dependencies</artifactid> <version>Finchley.M9</version> <type>pom</type> <scope>import</scope> </dependency> & lt;/dependencies> </dependencyManagement> <properties> <java.version>1.8</java.versio n> </properties> <build> <plugins> <plugin> <groupid& Gt;org.spriNgframework.boot</groupid> <artifactId>spring-boot-maven-plugin</artifactId> & lt;/plugin> </plugins> </build> <repositories> <repository> < id>spring-milestones</id> <name>spring milestones</name> <url>https://rep O.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories></project>
dependencyManagement
You can specify a uniform version number for all dependencies, where the Spring-cloud dependent version is FINCHLEY.M9, and then use the repository
warehouse that specifies this version.
spring-cloud-starter-config
Provides an API interface to access the Configuration Center service.
Add Controller
Create a new controller class cn.zxuqian.controllers.HelloController
and add the following code:
package cn.zxuqian.controllers;import org.springframework.beans.factory.annotation.Value;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RefreshScope@RestControllerpublic class HelloController { @Value("${message: 本地消息}") private String message; @RequestMapping("/message") public String message() { return this.message; }}
A simple controller that matches the /message
path and returns message
the value of the variable. This note is not to @RefreshScope
be used here, and will be spoken later. Takes the configuration @Value
item from the Configuration Center service, or the local environment variable, and so on, takes the value of the configuration center message
and gives it a default value of "local message," which is initialized with the default value if the Remote configuration center is unavailable.
Add configuration file Bootstrap.xml
We need to load configuration center configuration items before the Web client project is fully started, so you need to src/main/resources
create the file below and bootstrap.yml
then specify the name of this client and the URI of the Remote Configuration center:
spring: application: name: web-client cloud: config: uri: http://localhost:8888
Yml compared to the properties file is more concise, do not write a lot of duplicate prefixes, the upper content can be converted to the corresponding properties:
spring.application.name=web-clientspring.cloud.config.uri=http://localhost:8888
spring.application.name
Specifies the name of this project, which is used to configure the configuration file with the same file name as the configuration center, that is, the configuration center should have a configuration file named web-client.yml
.
spring.cloud.config.uri
Specifies the URI address of the Remote Configuration Center service, which defaults to http://localhost:8888
Create a Configuration Center project
Create a new MAVEN project using the following POM configuration:
<?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> Cn.zxuqian</groupid> <artifactId>config-server</artifactId> <version>1.0-snapshot</ version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot< /groupid> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.release< ;/version> <relativePath/> <!--lookup parent from repository to </parent> <properti Es> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1. 8</java.version> </properties> <depeNdencies> <dependency> <groupId>org.springframework.cloud</groupId> < Artifactid>spring-cloud-config-server</artifactid> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-test</artifa ctid> <scope>test</scope> </dependency> </dependencies> <dependencym Anagement> <dependencies> <dependency> <groupid>org.springframework. Cloud</groupid> <artifactId>spring-cloud-dependencies</artifactId> <ver Sion>finchley.m9</version> <type>pom</type> <scope>import</sco pe> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <ARTIFACTID&G t;spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> < repositories> <repository> <id>spring-milestones</id> <name>spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots& Gt <enabled>false</enabled> </snapshots> </repository> </repositories>< /project>
spring-cloud-config-server
That is, the core dependency of the Configuration Center service.
Configure Application
Create a new Java class cn.zxuqian.Application
and add the following code:
package cn.zxuqian;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;@EnableConfigServer@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
- Use
@EnableConfigServer
this comment to start the MAVEN project as a configuration center service.
Create a new Git repository
Configuration Center files are version-controlled, so you need to create a new Git repository locally to save the configuration file. Or you can also use a public remote Git repository, GitHub, code cloud, etc. Create a blank folder in any location (such as the project's parent directory), which is called config
, and can use any name. Then go to this folder and run
$ git init
To initialize the Git repository, then create a new file, named web-client.yml
, and add the following:
message: 此条消息来自于cofig server
Note This file name needs to be consistent with what was previously configured in the Web project spring.application.name
. The content of this file is the value of the message that the Web client wants to get. After the creation is complete, run the following git command to submit to the local repository:
$ git add web-client.yml$ git commit -m "added web-client.yml"
Configure the Git warehouse location
We need to specify the GIT warehouse address created above for the configuration center. src/main/resources
under Create applicaiton.yml
file, provide the following content:
server: port: 8888spring: cloud: config: server: git: uri: ${HOME}/development/codes/backend/gitee/config
This file specifies the port number of the configuration Center service, and the Git repository directory where the configuration file is saved, and if it is a remote repository, you can specify the URL address directly. To this, the Configuration Center service creation is complete.
Test
Start the Configuration Center service first, using the Spring-boot maven plugin: Spring-boot:run. Start the Web client again after successful startup, Access http://localhost:8080/message
if you see 此条消息来自于cofig server
that the configuration is successful. Then close the Configuration Center service, restart the Web client, and http://localhost:8080/message
we'll see 本地消息
.
Dynamic Update Configuration
Is it a hassle to restart after every configuration update? Spring Boot provides components for the maintenance of the spring-boot-starter-actuator
production environment, such as checking health information. Do you remember HelloController
the @RefreshScope
notes above? With it we can dynamically load configuration center modified configuration. Then we also need to add the following in the configuration center to web-client.yml
expose the Acurator /refresh
Terminal API.
message: 此条消息来自于cofig servermanagement: endpoints: web: exposure: include: "*"
After the update is complete, commit the Git repository, and then restart the Configuration Center service and the Web client. Modify the message to
message: 更新:此条消息来自于cofig server
Then send an empty post request to the/refresh
$ curl http://localhost:8080/actuator/refresh -d {} -H "Content-Type: application/json"
After you refresh the page, you will see the updated message.
Summarize
Now that we have a configuration center service, it depends on each component spring.application.name
to decide which profile to read, and then we use the Acurator API to /refresh
refresh the configuration items at run time. In addition, the configuration items are versioning-based and can be easily restored and updated. With this tutorial, you can see that the configuration of the components of Spring cloud is quite simple, and basically you can create a complete service component with just one note.
Welcome to my Blog original: Spring Cloud Getting Started Tutorial-build Configuration Center Service
Spring Cloud Getting Started Tutorial-build Configuration Center Service