SpringBoot actual electricity supplier item mall (20k + star) Address: github.com/macrozheng/...
Summary
Spring Cloud Bus using lightweight message broker is connected to each of the micro-service architecture service, for broadcast change the state (e.g., distribution center configuration change), or other management instruction, it will be described herein in detail to its usage.
Spring Cloud Bus Profile
We usually use the message broker to build a theme, and then put all the services in the micro-service architecture are connected to this subject up, when we send a message to the topic, all subscribed to the topic of service will receive messages and consume. Use Spring Cloud Bus can easily build this mechanism, it is also known as Spring Cloud Bus message bus. Spring Cloud Bus with Spring Cloud Config can use dynamic refresh configuration. Spring Cloud Bus currently supports two Message Broker: RabbitMQ and Kafka, for example using the function below to RabbitMQ Spring Cloud Bus refresh dynamically configured under to demonstrate.
RabbitMQ installation
- Install Erlang, Download: erlang.org/download/ot...
- Install RabbitMQ, Download: dl.bintray.com/rabbitmq/al...
- After installation is complete, enter the sbin directory under the RabbitMQ installation directory:
- In the address bar, type cmd and press Enter to start the command line, enter the following command to start the management features:
rabbitmq-plugins enable rabbitmq_management
复制代码
- Access address to see if the installation was successful: HTTP: // localhost: 15672 /
- Enter the account password and login: guest guest
Dynamic refresh configuration
Use Spring Cloud Bus dynamically refreshed with Spring Cloud Config configuration needs to be used with, we use the one in the config-server, config-client module to demonstrate this function.
Adding to the config-server message bus support
- Add its dependencies in pom.xml:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
复制代码
- Add a profile application-amqp.yml, mostly adds RabbitMQ configuration and exposed the Actuator endpoints refresh configuration;
server:
port: 8904
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/macrozheng/springcloud-config.git
username: macro
password: 123456
clone-on-start: true # 开启启动时直接从git获取配置
rabbitmq: #rabbitmq相关配置
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
management:
endpoints: #暴露bus刷新配置的端点
web:
exposure:
include: 'bus-refresh'
复制代码
Config-client to add a message bus support
- Add its dependencies in pom.xml:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
复制代码
- Add a profile bootstrap-amqp1.yml and bootstrap-amqp2.yml used to start two different config-client, only two profiles different port numbers;
server:
port: 9004
spring:
application:
name: config-client
cloud:
config:
profile: dev #启用环境名称
label: dev #分支名称
name: config #配置文件名称
discovery:
enabled: true
service-id: config-server
rabbitmq: #rabbitmq相关配置
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
management:
endpoints:
web:
exposure:
include: 'refresh'
复制代码
Dynamic refresh configuration demo
- Let's start related services, start eureka-server, with application-amqp.yml to configure the boot config-server, to bootstrap-amqp1.yml to configure the boot config-client, configured to bootstrap-amqp2.yml to restart a config- client, after starting the registry is shown below:
- After all the services start, we log RabbitMQ console can be found in Spring Cloud Bus creates a queue called springCloudBus switch and three to springCloudBus.anonymous beginning:
- Let's modify the configuration file config-dev.yml Git repository dev branch:
# 修改前信息
config:
info: "config info for dev(dev)"
# 修改后信息
config:
info: "update config info for dev(dev)"
复制代码
- Call registry interface refresh all configuration: HTTP: // localhost: 8904 / Actuator / Bus-Refresh
- Refresh after each call http: // localhost: 9004 / configInfo and http: // localhost: 9005 / configInfo for configuration information, found have been refreshed;
update config info for dev(dev)
复制代码
- If you only need to refresh the specified instance can be refreshed using the following format: http: // localhost: 8904 / Actuator / Bus-Refresh / {} Where do you want , we are here to refresh the running config-client on port 9004 for example http: localhost //: 8904 / Actuator / Bus-Refresh / config-Client: 9004 .
Use with WebHooks
WebHooks equivalent of a hook function, we can configure this hook function when the trigger when Git repository to push code to Gitee here as an example of its use to the next, but here when we configure the warehouse to push the code will automatically refresh the service configuration .
To use the module
springcloud-learning
├── eureka-server -- eureka注册中心
├── config-server -- 配置中心服务
└── config-client -- 获取配置的客户端服务
复制代码
Project Source Address
No public
mall project a full tutorial serialized in public concern number the first time to obtain.