1: Understanding bootstrap.yaml
It will be loaded before the application, and if it has the same name attribute as the application, the first boot will be overwritten.
2: The configuration center and our registry must have a start-up first.
3: spring Configuration and Environmentalization
In Spring 3.0-)
<beans profile ="test">
<bean id="">
</beans>
4: Build a Configuration Center
Git svn local file
Read local files:
Understand ${user.dir}
New folder configs under configuration center / resources
Create three files eurekaserver-dev.yml eurekaserver-prod.yml eurekaserver-prod.yml
Introducing jar
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Introducing Core jar--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-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>
Configuration file:
server: port: 5000 spring: application: name: config-server cloud: config: server: git: ##Define the address to pull the configuration uri: D:\\lesson-1\\config-server\\src\\main\\resources\\configs management: endpoints: web: exposure: include: "*"
Open git initialization:
git init
git add .
git commit -m "frist commit"
Add the annotation @EnableConfigServer to the startup class
Configuration center completed.
Client Connection:
New file - bootstrap.yml
The configuration is as follows:
spring: application: name: eurekaserver cloud: config: name: eurekaserver uri : http://localhost:5000/ profiles: active: dev server: port: 10000
Then the jar package is introduced
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency>
Add @EnableDiscoveryClient to the startup class
When the client is finished, the startup can be accessed.
Depending on the online address, pull the configuration from git:
1: The configuration file is as follows:
##Define a name spring: application: name: dnconfig ##Configure the file address git repository of the server cloud: config: server: git: #This is the GIT address of the teacher. You can define a git address to get it by yourself. uri: https://github.com/yaojin156/tmp.git #${user.dir}\src\main\resources\configs #Defined as forcing pull configuration from git force-pull: true ##Define the current port server: port: 9090 ##This approach has been abolished in Open 2.0, which defines env and health #management: # security: # enabled: true ##The Open 2.X version that defines env and health should be written like this management: endpoints: web: exposure: include: env
2: Create three new configuration files on your computer (my files are in D: download tmp)
orderserver-prod.properties denotes production service configuration
orderserver-test.properties denotes the test configuration
orderserver-dev.properties denotes development configuration
Specify the configuration path as follows:
cloud: config: server: git: #This is the GIT address of the teacher. You can define a git address to get it by yourself. uri: https://github.com/yaojin156/tmp.git
3: Submit your configuration file to an online git
git clone https://github.com/yaojin156/tmp.git git add orderserver-*.properties git commit -m "fritst commit" git push
4: Configure forcing pull-out of git files
cloud: config: server: git: uri: https://github.com/yaojin156/tmp.git #${user.dir}\src\main\resources\configs #Defined as forcing pull configuration from git force-pull: true
6: Start testing whether the local configuration is in effect
http:localhost:9090/order/server-prod
Build a client configclient
First learn about bootstarp configuration
The bootstarp configuration file will be loaded first, and once the same name configuration exists in our application, it will directly take effect in the application configuration, so we can think of this as a process of overwriting.
Learn about springboot Actuator monitoring
Actuator is a monitoring method provided by spring boot.
Actuator is an integrated function of introspection and monitoring of application system provided by Spring Boot. It can view application configuration details, such as automated configuration information, created Spring beans and some environment attributes.
Actuator's monitoring can be divided into two categories: native endpoint and user-defined endpoint. Custom endpoint mainly refers to extensibility. Users can define some indicators of concern according to their practical application and monitor them at run time.
Sound endpoints are classified into three categories
- Application Configuration Class: You can view static information of application in run time, such as automatic configuration information, loaded spring bean information, yml file configuration information, environment information, request mapping information;
- Metric indicators: mainly dynamic information during runtime, such as stack, request link, some health indicators, metrics information, etc.
- Operational control class: mainly refers to shutdown, the user can send a request to shut down the monitoring function of the application.
Actuator provides 13 interfaces
We just need to understand the env/{name} we're going to use today to get specific attribute values based on the name.
1: Configure bootstrap.yml
spring: cloud: config: #Defined as the name of the file name: order-server #To get the address to the configuration center uri: http://127.0.0.1:9090/ #Get our profile name profile: test
2: Configure application.yml
##Define port number and instance name server: port: 8091 spring: application: name: spring-cloud-config-client ###Define Open env management: endpoints: web: exposure: include: env
3: Start config-client access
http://localhost:8091/actuator/env
You can see the configuration information inside our configuration center
4: Start getting configuration information from the configuration center
@Value("${name}") private String name; @Value("${id}") private String id; @RequestMapping("/hi") public String hi(){ System.out.println("The output is:id:"+id+",name:"+name); return "id:"+id+",name:"+name; }
5: If the server configuration changes, how will the client change?
Our configuration is not static. If I move the server, or I need to add a new node, then I need to modify the configuration in the configuration item. At this time, do we have to restart all our service nodes?
Add a new configuration in the configuration file orderserver-test.properties
dn.name = nick dn.age = 17
Refresh service, configuration can be displayed in the configuration center.
But on our client side
There is no corresponding configuration item, so the problem arises.
What should we do at this time?
spring provides us with a refresh way to solve the current problem by refreshing the configuration endpoints.
Specific actions are as follows: modify the client configuration application.yml
management: endpoints: web: exposure: #Enable client support env and refresh include: env,refresh
Send requests to our configuration server via postman http://localhost:8091/actuator/refresh Remember POST
So we can get the latest configuration after the modification.
Normally, we have modified the Bus configuration that requires cloud s, that is, the event bus, to pull our client notifications, but for the time being we will not talk about it. Stay behind and do it together. All we need to know here is that this way we can achieve our goals.
Understanding the role of refresh
Official Document Address:
In this passage, the official document mentions something called spring-cloud-config-monitor. The main meaning of this passage is that the change of our configuration center can be notified by spring cloud Bus. If Spring - Cloud configuration monitor library dependencies are added to the configuration server and Spring cloud bus is activated, the / monitor endpoint is enabled.
Note the paragraph in the document:
When the webhook is activated, the Config Server sends a RefreshRemoteApplicationEvent targeted at the applications it thinks might have changed.
When the webhook is activated, the configuration server sends a RefreshRemoteApplication Event with the goal of an application that it thinks may have changed.
Okay, let's not talk about this for the time being. Let's save it. Let's first understand the principle of refresh.
public synchronized Set<String> refresh() { //Call the following refresh method. Set<String> keys = this.refreshEnvironment(); //Clear RefreshScope Cache // RefreshScope regenerates beans with new environmental parameters this.scope.refreshAll(); return keys; } public synchronized Set<String> refreshEnvironment() { //Extract all parameter variables except standard parameters (SYSTEM,JNDI,SERVLET) Map<String,Object>before= this.extract(this.context.getEnvironment().getPropertySources()); //Reload the parameters in the original Environment into a new Spring Context container and close the new container after that. this.addConfigFilesToEnvironment(); //Compare changes Set<String> keys = this.changes(before, this.extract(this.context.getEnvironment().getPropertySources())).keySet();//Extracting updated parameters (excluding standard parameters) //Send EnvironmentChangeEvent //Publish environment change events and receive: EnvironmentChangeListener/Logging Rebinder this.context.publishEvent(new EnvironmentChangeEvent(this.context, keys)); return keys; }