In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
Brief introduction
Spring Cloud provides a platform for deploying micro-services, including common components in micro-services: configuration central services, API gateways, circuit breakers, service registration and discovery, distributed traceability, OAuth3, consumer-driven contracts, etc. We don't have to know what each component does first, but as the tutorial progresses, we will gradually come into contact with them. The general structure of a distributed service is shown in the following figure (picture: spring.io):
Using Spring Cloud to build a distributed system is very simple, we only need a few lines of simple configuration to start a series of components, and then we can control, use and manage these components in the code. Spring Cloud uses Spring Boot as the basic framework. You can refer to my previous blog about how to create a Spring Boot project, Spring Boot 2.0.1 getting started. This tutorial will teach you how to configure the service center service and read the configuration through the web client.
Basic environment JDK 1.8Maven 3.3.9IntelliJ 2018.1Git project source code
Gitee Code Cloud
Create Web Client
First, create a Maven project with IntelliJ. The pom.xml file is as follows:
4.0.0 cn.zxuqian web 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.0.1.RELEASE org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-actuator Org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-dependencies Finchley.M9 pom import 1.8 Org.springframework.boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/libs-milestone false dependencyManagement can specify a uniform version number for all dependencies The dependent versions of Spring-cloud here are all Finchley.M9, and then use repository to specify the repository for 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: local message}") private String message; @ RequestMapping ("/ message") public String message () {return this.message;}}
A simple controller matches the / message path and returns the value of the message variable. Don't worry about the @ RefreshScope note here. We'll talk about it later. @ Value takes the configuration item from the configuration center service, or the local environment variable, and so on. Here, it takes the value of the configuration center's message and gives it a default value "local message", that is, if the remote configuration center is not available, this variable will be initialized with the default value.
Add profile bootstrap.xml
We need to load the configuration items for the configuration center before the web client project is fully started, so we need to create a bootstrap.yml file under src/main/resources, and then specify the name of the client and the uri of the remote configuration center:
Spring: application: name: web-client cloud: config: uri: http://localhost:8888
Yml is more concise than properties files. It does not need to write many duplicate prefixes, and the above content can be converted into the corresponding properties:
Spring.application.name=web-clientspring.cloud.config.uri= http://localhost:8888spring.application.name specifies the name of this project, which is used to take the configuration file with the same file name of 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 the http://localhost:8888 creation configuration center project
Create a new Maven project with the following pom configuration:
4.0.0 cn.zxuqian config-server 1.0-SNAPSHOT jar org.springframework.boot spring-boot-starter-parent 2.0.1.RELEASE UTF-8 1.8 org.springframework.cloud spring-cloud-config-server org.springframework.boot Spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies Finchley.M9 pom import org.springframework .boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/libs-milestone false
Spring-cloud-config-server 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);} @ EnableConfigServer can start the Maven project as a configuration center service. New Git Warehouse
Configuration center files are based on version control, so you need to create a new git repository locally to save the configuration files. Or you can use public remote git repositories, github, Mayun, etc. Create a blank folder anywhere (such as the parent directory of the project), which is called config, and you 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: this message is from cofig server
Note that this file name needs to be consistent with the spring.application.name previously configured in the web project. The content of this file is the value of the message to be obtained by the web client. After the creation is completed, run the following git command to submit to the local warehouse:
$git add web-client.yml$ git commit-m "added web-client.yml" configure git warehouse location
We need to specify the address of the git warehouse created above for the configuration center. Create an applicaiton.yml file under src/main/resources and provide the following:
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. If it is a remote repository, you can specify the url address directly. At this point, the configuration center service has been created.
test
Start the configuration center service first, using the spring-boot maven plug-in: spring-boot:run. After starting successfully, start the web client and access http://localhost:8080/message. If you see this message coming from cofig server, the configuration is successful. Then close the configuration center service, restart the web client, visit http://localhost:8080/message, and we will see the local message.
Dynamically update configuration
Is it troublesome to restart every time the configuration is updated? Spring boot provides spring-boot-starter-actuator components for maintenance of the production environment, such as checking health information. Remember HelloController's @ RefreshScope note above? Using it, we can dynamically load the modified configuration of the configuration center. Then we need to add the following to the web-client.yml in the configuration center to expose the / refresh terminal api of the acurator.
Message: this message comes from cofig servermanagement: endpoints: web: exposure: include: "*"
After the update is completed, submit the git repository, and then restart the configuration center service and the web client. Modify message to
Message: update: this message is from cofig server
Then send an empty post request to / refresh
$curl http://localhost:8080/actuator/refresh-d {}-H "Content-Type: application/json"
After refreshing the page, you can see the updated message.
Summary
Now we have set up a configuration center service that determines which configuration file to read based on the spring.application.name of each component, and then we use acurator's / refreshapi to refresh the configuration items at run time. In addition, the configuration items are based on version control and can be easily restored and updated. From this tutorial, you can see that the configuration of the components of Spring Cloud is quite simple, and you can basically create a complete service component with a single annotation.
Welcome to my blog: Spring Cloud getting started tutorial-Building configuration Center Service
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.