Quick entry and practice of distributed configuration center of SpringCloud series

You can introduce the following configuration in maven yourself:

org.springframework.cloud spring-cloud-config-server uses the annotation @EnableConfigServer to indicate this config server project

package com.example.springcloud.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class SpringcloudConfigServerApplication {

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

}

Create a new bootstrap.yml configuration file and specify the address of the github warehouse:

server:
port: 8761
spring:
application:
name: springcloud-config-server
cloud:
config:
server:
git:
uri: https://github.com/your_github_account/springCloudExamples
username: your_github_account
password: your_github_password
search-paths: config-repository
5. Config Client code implementation
Also create a new SpringBoot Initialize project and quickly create
it. Insert the picture here to describe the
pom configuration file:

org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-config yaml configuration, uri is specified as the address of the config server, profile is an environment variable, which can be specified as dev (development environment), label Indicates branch, master refers to the main branch of github

server:
port: 8082
spring:
application:
name: springcloud-config-client
cloud:
config:
uri: http://127.0.0.1:8761/
profile: dev
label: master

Then, we have to create a new configuration file in the github repository: the naming convention must be the client's spring. application.name plus profile,
insert the picture description here .
In the configuration file, just write:

config.client.profile=springcloud-config-client-dev
test: start the config server first, then start the config client, write an example test, add @RefreshScope to realize the refresh function

@RestController
@RefreshScope
public class ConnfigClientController {

@Value("${config.client.profile}")
private String profile;

@GetMapping(value = "/test")
public String test() {
    return this.profile;
}

}

Start the SpringBoot project and test:
insert the picture description here.
During the config server startup process, you can see that the config server pulls down the configuration file from github to the local cache, specifically the C drive AppData directory
. Insert the picture description here

6. Implementation of pull refresh on the
client side How to implement pull-down configuration data on the client side? You can integrate spring-boot-starter-actuator to achieve:

pom configuration:

org.springframework.boot spring-boot-starter-actuator spring-boot-starter-actuator配置:include加上rehresh配置

management:
endpoints:
web:
# Prefix name, the default is also actuator
base-path: /actuator
# By default, only info, health access is available, plus refresh
exposure:
include: info,health,refresh
endpoint:
health:
show-details: always
refresh:
enabled: true

Visit the client link, pay attention to the post method, http://localhost:8082/actuator/refresh

If the github configuration file is not updated:
insert the picture description here,
modify the configuration file, commit and push to github,
insert the picture description here
, when calling the interface, you can see that the config client gets data from the config server:
insert the picture description here

7.
What is the Spring Cloud Bus message bus?
In a system with a microservice architecture, a lightweight message broker is usually used to construct a common message topic, and all microservice instances in the system are linked. Since the message generated in this topic will be monitored and consumed by all instances, it is called the message bus.
What is Spring Cloud Bus?
Spring Cloud Bus is a framework used to link distributed system nodes with lightweight message systems. It integrates Java's event processing mechanism and message middleware functions

Spring Cloud Bus can manage and propagate messages between distributed messages, just like a distributed executor, which can be used to broadcast state changes, event push, etc., and can also be used as a communication channel between microservices

After reading the theory, it seems that you don't understand? So let's start from the previous study. The previous introduction explains that as a distributed configuration center, it should at least have the function of pushing messages, so the role of this configuration center can be played by the config server, and the effect achieved is config server (configuration center). Refresh the data, the client can update synchronously, draw a picture for explanation:
insert the picture description here

1. The configuration center (config server) executes bus-refresh, the refresh interface provided by spring cloud bus, and the configuration center pulls down data from the git warehouse to the local git warehouse.
2. After executing bus-refresh, it sends the message to the spring cloud bus (message Bus), the message bus writes the message to the topic of the message queue (rabbitMQ)
3. As long as you subscribe to the message queue topic, you can listen to the spring cloud bus message (based on rabbitmq)
4. After listening, the config client will start from the config server Pull to update configuration data.
In short, Config Client instances all listen to the same topic in RabbitMQ. When a service refreshes data, it will put this information into the topic, so that other services that listen to the same topic can get it. Notify, and then update your own configuration. Of course, this refresh operation does not have to be placed on the config server, but can also be triggered by a client, as long as the message is sent to the message bus.

8. Docker installation and deployment RabbitMQ
mainly introduces Docker version, common docker image operation:
insert picture description here

Query rabbitMQ mirror:

Management version, if not specified, it will default to the latest version latest

docker search rabbitmq:management
insert picture description here to
pull the image:

docker pull rabbitmq:management to
view the list of docker images:


Docker images Docker container operation:
ok, after the above command is executed, the image has been pulled to the local warehouse, and then the container operation can be carried out to start rabbitMQ

Simple version

docker run -d -p 5672:5672 -p 15672:15672 --name rabbitmq rabbitmq:management
-d background operation
-p implicit port
-name specify rabbitMQ name
complex version (set account password, hostname)

docker run -d -p 15672:15672 -p 5672:5672 -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin --name rabbitmq --hostname=rabbitmqhostone rabbitmq:management
-d background operation
-p implicit port --name
specifies the name of rabbitMQ
RABBITMQ_DEFAULT_USER designated user account
RABBITMQ_DEFAULT_PASS designated account password After
executing the above command, visit: http://ip:15672/

Default account password: guest/guest
insert picture description
here insert picture description here
Other common container commands:

View running containers

View all containers with the command docker ps -a

docker ps
start the container

eg: docker start 9781cb2e64bd

docker start CONTAINERID [container ID]
stop container

docker stop CONTAINERID[ContainerID]
delete a container

docker rm CONTAINERID[container ID]
view Docker container log

eg:docker logs 9781cb2e64bd

docker logs container-name[container-name]/container-id[container ID]
9. Spring Cloud Bus dynamic refreshing
With the previous learning, proceed to code example practice, config server pom configuration:

org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-bus-amqp yaml配置:开放bus-refresh

management:
endpoints:
web:
base-path: /actuator
exposure:
include: info,health,refresh,bus-refresh
endpoint:
health:
show-details: always
refresh:
enabled: true
加上rabbitmq配置

RabbitMQ configuration

rabbitmq:
host: 192.168.6.155
port: 5672
username: guest
password: guest
virtual-host: /
Config Client code example modification, pom configuration:

org.springframework.cloud spring-cloud-starter-bus-amqp must also be configured with rabbitMQ, so that you can subscribe to updates:

RabbitMQ configuration

rabbitmq:
host: 192.168.6.155
port: 5672
username: guest
password: guest
virtual-host: /
Client must be modified, refresh and enabled must be changed to true, trace is for tracking and can be turned on as needed

spring:
cloud:
bus:
enabled: true
refresh:
enabled: true
trace:
enabled: true
Note: In order to update in real time, @RefreshScope must be added

Configuration center for bus refresh
Shenzhen website optimization www.zg886.cn

Guess you like

Origin blog.csdn.net/weixin_45032957/article/details/108530361