Spring Cloud Bus
Spring Cloud Config server is responsible for the release git svn stored in the configuration file into REST interface, clients can obtain configuration from the server REST interface. But the client does not take the initiative to perceive changes in the configuration, so take the initiative to acquire new configuration, which requires each client to trigger the respective / refresh via the POST method.
Nodes of a distributed system connected by a Spring Cloud Bus lightweight message broker. This can be used to change the status of a broadcast (e.g., configuration changes), or other management instruction. Spring Cloud Bus provides access via POST method endpoint / bus / refresh, this interface is usually called by git hook function to inform each Spring Cloud Config the client to update the server configuration.
SpringCloudConfig combined SpringCloudBus implement a distributed configuration workflow
- Submit code to trigger a post request to bus / refresh
- server receives the request and sent Spring Cloud Bus
- Spring Cloud bus and receive messages to notify other clients
- Other client receives a notification request Server clients to obtain the latest configuration
- All clients are to get the latest configuration
Message Agent
Message Broker (Message Broker) is a message authentication, transmission, routing architecture pattern. It is a message broker middleware product, its core is a message routing program for receiving and distributing messages, and forwarding to the correct application according to predetermined message processing flow. It includes a separate communication and messaging protocol enables the network communication between the internal tissues and tissue. The purpose is to be able to design proxy incoming messages from the application, and perform some special operations.
Currently Spring Cloud Bus Support
- RabbitMQ:spring-cloud-starter-bus-amqp
- Kafka:spring-cloud-starter-bus-kafka
RabbitMQ way
Configuration RabbitMQ
Create a user
rabbitmqctl add_user [username] [password]
Set tag
rabbitmqctl set_user_tags username [tag1] [tag2] ...
User Authorization
rabbitmqctl set_permissions [-p <vhostpath>] <user> <conf> <write> <read>
log in
Browser to access http: // localhost: 15672
Config Server server configuration
Dependencies file POM.xml
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.springcloud</groupId>
<artifactId>springcloud-root</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>springcloud-eureka-provider</artifactId>
<name>springcloud-eureka-provider</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Application.yml configuration file
spring:
application:
name: springcloud-config-server
freemarker:
prefer-file-system-access: false
security:
user:
name: admin
password: 123456
cloud:
config:
# 配置仓库的分支
label: master
server:
git:
# 配置git仓库地址
# uri: [email protected]/springcloud-config.git
uri: file:E:/SpringBoot/SpringCloud/springcloud-root/springcloud-config
# 配置仓库路径
search-paths: config-file
# 访问git仓库的用户名
username: zhaojq
# 访问git仓库的用户密码
password: 123456
bus:
enabled: true
trace:
enabled: true
refresh:
enabled: true
rabbitmq:
host: localhost
port: 5672
username: zhaojq
password: 123456
server:
port: 8130
management:
endpoints:
web:
exposure:
include: '*'
eureka:
instance:
hostname: eureka-config-server.com
instance-id: eureka-config-server
client:
service-url:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer1.com:8897/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer2.com:8898/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer3.com:8899/eureka/
Startup Items springcloud-config-server
rabbit,kafka, and no default binder has been set.错误
org.springframework.context.ApplicationContextException: Failed to start bean 'outputBindingLifecycle'; nested exception is java.lang.IllegalStateException: A default binder has been requested, but there is more than one binder available for 'org.springframework.cloud.stream.messaging.DirectWithAttributesChannel' : rabbit,kafka, and no default binder has been set.
Kafka and RabbitMQ can configure only one, and if kafka RabbitMQ dependencies exist, the above error will be reported on startup.
Client Configuration Config Client
Dependencies file POM.xml
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.springcloud</groupId>
<artifactId>springcloud-root</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>springcloud-eureka-provider</artifactId>
<name>springcloud-eureka-provider</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
bootstrap.yml file
spring:
application:
name: springcloud-eureka-provider
freemarker:
prefer-file-system-access: false
security:
user:
name: admin
password: 123456
cloud:
config:
#profile: dev
label: master
fail-fast: true
#指明配置服务中心的网址
#uri: http://eureka-config-server.com:8130
discovery:
service-id: springcloud-config-server
enabled: true
bus:
enabled: true
trace:
enabled: true
refresh:
enabled: true
rabbitmq:
host: localhost
port: 5672
username: zhaojq
password: 123456
# Spring Boot 2.x已淘汰的方式:management.security.enabled=false
management:
endpoints:
web:
exposure:
include: '*'
eureka:
instance:
hostname: eureka-provider2.com
instance-id: eureka-provider2
client:
service-url:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer1.com:8897/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer2.com:8898/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer3.com:8899/eureka/
Modify the configuration class
Plus @RefreshScope annotation, @ RefreshScope must be added to the need to update the configuration class, otherwise the client will receive an update message server, but can not be updated, because they do not know where the update.
package org.springcloud.eureka.provider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableDiscoveryClient
@RestController
@RefreshScope
public class ConfigProviderApplication {
@Value("${content}")
String content;
@Value("${server.port}")
String port;
@RequestMapping("/config")
public String Home(@RequestParam String name) {
return "Hello "+name+", This is from serverport:" + port+",content="+content;
}
public static void main(String[] args) {
SpringApplication.run(ConfigProviderApplication.class, args);
}
}
In order to start the project
springcloud-eureka-cluster-peer1
springcloud-eureka-cluster-peer2
springcloud-eureka-cluster-peer3
springcloud-config-server
springcloud-eureka-provider1
springcloud-eureka-provider2
springcloud-eureka-provider3
An unexpected connection driver error occured错误
2020-02-23 01:17:02.141 INFO 26300 --- [SeGTZQPlTXZGQ-9] o.s.a.r.c.CachingConnectionFactory : Attempting to connect to: [localhost:5672]
2020-02-23 01:17:02.145 ERROR 26300 --- [ 127.0.0.1:5672] c.r.c.impl.ForgivingExceptionHandler : An unexpected connection driver error occured
After the user is granted a role, can only login to the console, but did not give permission to read and write and manage the queue to view the admin console button, refer to the user authorization, rabbitmqctl set_permissions
Testing Services
1, modify the configuration
Modify Git repository configuration
2, View Config Server
browser to access http://eureka-config-server.com:8130/application/test/master
Config Server is up to date values.
3, refresh configuration
POST request sent to: HTTP: //eureka-config-server.com: 8130 / Actuator / Bus-Refresh
springcloud-Eureka-Provider1 remote update request is received
Received remote refresh request. Keys refreshed [config.client.version, content]
Attempting to connect to: [localhost:5672]
Created new connection: rabbitConnectionFactory.publisher#9282cf2:0/SimpleConnection@6f1560a7 [delegate=amqp://[email protected]:5672/, localPort= 53197]
DiscoveryClient_SPRINGCLOUD-EUREKA-PROVIDER/eureka-provider1 - registration status: 204
DiscoveryClient_SPRINGCLOUD-EUREKA-PROVIDER/eureka-provider1: registering service...
DiscoveryClient_SPRINGCLOUD-EUREKA-PROVIDER/eureka-provider1 - registration status: 204
springcloud-eureka-provider2, springcloud-eureka-provider3 also received the above request.
4, another visit Config Client
Access:
http://eureka-provider1.com:8001/config?name=zhaojq,
http://eureka-provider2.com:8002/config?name=zhaojq
HTTP: // Eureka-provider3 .com:? 8003 / config name = zhaojq
have been refreshed configuration
Kafka way
Placed Kafka
zookeeper
- Download the installation package
http://zookeeper.apache.org/releases.html#download - ZooKeeper unzip and enter the directory, such as: E: \ SpringBoot \ apache-zookeeper-3.5.7-bin \ conf
- The "zoo_sample.cfg" rename "zoo.cfg"
- Open the "zoo.cfg" find and edit dataDir = E: \ SpringBoot \ apache-zookeeper-3.5.7-bin \ tmp
- Adding system variables: ZOOKEEPER_HOME = E: \ SpringBoot \ apache-zookeeper-3.5.7-bin
- Edit path system variable, add the path:% ZOOKEEPER_HOME% \ bin
- Zookeeper modify the default port (default port 2181) in zoo.cfg file
- Press Shift + right, open a new PowerShell, enter "zkServer", run Zookeeper
- Command line prompt is as follows: Description local Zookeeper start successfully
Note: This window does not close
Kafka
- Download the installation package
http://kafka.apache.org/downloads - Kafka unzip and enter the directory, such as: E: \ SpringBoot \ kafka_2.13-2.4.0
- Enter config directory to find the file and open server.properties
- Locate and edit log.dirs = log.dirs = E: \ SpringBoot \ kafka_2.13-2.4.0 \ kafka-logs
- Locate and edit zookeeper.connect = localhost: 2181
- Kafka will by default runs on port 9092 and connects zookeeper default port: 2181
- Kafka into the installation directory E: \ SpringBoot \ kafka_2.13-2.4.0, press Shift + Right, open a new PowerShell, open a command line, type:
.\bin\windows\kafka-server-start.bat .\config\server.properties
- Command line prompt is as follows: Description local Kafka start successfully
Note: This window does not close
Test Kafka
Kafka into the installation directory E: \ SpringBoot \ kafka_2.13-2.4.0, press Shift + Right, open a new PowerShell, open a command line, type:
Creating Topic
.\bin\windows\kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
View Topic
.\bin\windows\kafka-topics.bat --list --zookeeper localhost:2181
Creating Producer
.\bin\windows\kafka-console-producer.bat --broker-list localhost:9092 --topic test
Creating consumer
.\bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --from-beginning
Config Server server configuration
Dependencies file POM.xml
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.springcloud</groupId>
<artifactId>springcloud-root</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>springcloud-config-server</artifactId>
<name>springcloud-config-server</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Application.yml configuration file
spring:
application:
name: springcloud-config-server
freemarker:
prefer-file-system-access: false
security:
user:
name: admin
password: 123456
cloud:
config:
# 配置仓库的分支
label: master
server:
git:
# 配置git仓库地址
# uri: [email protected]/springcloud-config.git
uri: file:E:/SpringBoot/SpringCloud/springcloud-root/springcloud-config
# 配置仓库路径
search-paths: config-file
# 访问git仓库的用户名
username: zhaojq
# 访问git仓库的用户密码
password: 123456
bus:
enabled: true
trace:
enabled: true
refresh:
enabled: true
#rabbitmq:
#host: localhost
#port: 5672
#username: zhaojq
#password: 123456
kafka:
bootstrap-servers:
- localhost:9092
server:
port: 8130
management:
endpoints:
web:
exposure:
include: '*'
eureka:
instance:
hostname: eureka-config-server.com
instance-id: eureka-config-server
client:
service-url:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer1.com:8897/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer2.com:8898/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer3.com:8899/eureka/
Startup Items springcloud-config-server
Client Configuration Config Client
Dependencies file POM.xml
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.springcloud</groupId>
<artifactId>springcloud-root</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>springcloud-eureka-provider</artifactId>
<name>springcloud-eureka-provider</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
bootstrap.yml file
spring:
application:
name: springcloud-eureka-provider
freemarker:
prefer-file-system-access: false
security:
user:
name: admin
password: 123456
cloud:
config:
#profile: dev
label: master
fail-fast: true
#指明配置服务中心的网址
#uri: http://eureka-config-server.com:8130
discovery:
service-id: springcloud-config-server
enabled: true
bus:
enabled: true
trace:
enabled: true
refresh:
enabled: true
#rabbitmq:
#host: localhost
#port: 5672
#username: zhaojq
#password: 123456
kafka:
bootstrap-servers:
- localhost:9092
# Spring Boot 2.x已淘汰的方式:management.security.enabled=false
management:
endpoints:
web:
exposure:
include: '*'
eureka:
instance:
hostname: eureka-provider2.com
instance-id: eureka-provider2
client:
service-url:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer1.com:8897/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer2.com:8898/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer3.com:8899/eureka/
In order to start the project
springcloud-eureka-cluster-peer1
springcloud-eureka-cluster-peer2
springcloud-eureka-cluster-peer3
springcloud-config-server
springcloud-eureka-provider1
springcloud-eureka-provider2
springcloud-eureka-provider3
springcloud-eureka-provider1 output:
2020-02-24 02:16:36.436 INFO 12888 --- [ main] o.a.kafka.common.utils.AppInfoParser : Kafka version: 2.3.1
2020-02-24 02:16:36.436 INFO 12888 --- [ main] o.a.kafka.common.utils.AppInfoParser : Kafka commitId: 18a913733fb71c01
2020-02-24 02:16:36.436 INFO 12888 --- [ main] o.a.kafka.common.utils.AppInfoParser : Kafka startTimeMs: 1582481796436
2020-02-24 02:16:36.457 INFO 12888 --- [ main] o.a.k.clients.consumer.ConsumerConfig : ConsumerConfig values:
allow.auto.create.topics = true
auto.commit.interval.ms = 100
auto.offset.reset = latest
bootstrap.servers = [localhost:9092]
check.crcs = true
client.dns.lookup = default
client.id =
client.rack =
connections.max.idle.ms = 540000
default.api.timeout.ms = 60000
enable.auto.commit = false
exclude.internal.topics = true
fetch.max.bytes = 52428800
fetch.max.wait.ms = 500
fetch.min.bytes = 1
group.id = anonymous.81ab530a-354f-407f-b513-84d6641b561c
group.instance.id = null
heartbeat.interval.ms = 3000
interceptor.classes = []
internal.leave.group.on.close = true
isolation.level = read_uncommitted
key.deserializer = class org.apache.kafka.common.serialization.ByteArrayDeserializer
max.partition.fetch.bytes = 1048576
max.poll.interval.ms = 300000
max.poll.records = 500
metadata.max.age.ms = 300000
metric.reporters = []
metrics.num.samples = 2
metrics.recording.level = INFO
metrics.sample.window.ms = 30000
partition.assignment.strategy = [class org.apache.kafka.clients.consumer.RangeAssignor]
receive.buffer.bytes = 65536
reconnect.backoff.max.ms = 1000
reconnect.backoff.ms = 50
request.timeout.ms = 30000
retry.backoff.ms = 100
sasl.client.callback.handler.class = null
sasl.jaas.config = null
sasl.kerberos.kinit.cmd = /usr/bin/kinit
sasl.kerberos.min.time.before.relogin = 60000
sasl.kerberos.service.name = null
sasl.kerberos.ticket.renew.jitter = 0.05
sasl.kerberos.ticket.renew.window.factor = 0.8
sasl.login.callback.handler.class = null
sasl.login.class = null
sasl.login.refresh.buffer.seconds = 300
sasl.login.refresh.min.period.seconds = 60
sasl.login.refresh.window.factor = 0.8
sasl.login.refresh.window.jitter = 0.05
sasl.mechanism = GSSAPI
security.protocol = PLAINTEXT
send.buffer.bytes = 131072
session.timeout.ms = 10000
ssl.cipher.suites = null
ssl.enabled.protocols = [TLSv1.2, TLSv1.1, TLSv1]
ssl.endpoint.identification.algorithm = https
ssl.key.password = null
ssl.keymanager.algorithm = SunX509
ssl.keystore.location = null
ssl.keystore.password = null
ssl.keystore.type = JKS
ssl.protocol = TLS
ssl.provider = null
ssl.secure.random.implementation = null
ssl.trustmanager.algorithm = PKIX
ssl.truststore.location = null
ssl.truststore.password = null
ssl.truststore.type = JKS
value.deserializer = class org.apache.kafka.common.serialization.ByteArrayDeserializer
Testing Services
1, modify the configuration
Modify Git repository configuration
2, View Config Server
browser to access http://eureka-config-server.com:8130/application/test/master
Config Server is up to date values.
3, refresh configuration
POST request sent to: http: //eureka-config-server.com: 8130 / actuator / bus-refresh
4, another visit Config Client
visit: http: //eureka-provider3.com:? 8003 / config name = zhaojq
have been refreshed configuration.