Java spring cloud b2b2b2c o2o multi user shopping mall springcloud Architecture - refresh configuration with spring cloud Bus

We use the spring cloud distributed micro service Cloud Architecture to build b2b2c e-commerce system. In addition to th...

We use the spring cloud distributed micro service Cloud Architecture to build b2b2c e-commerce system. In addition to the system services provided by the architecture itself, we split the business services of b2b2c into different business micro services.

As our business system becomes larger and more complex, various configurations will increase. Once the configuration file is modified, the service will be stopped and then restarted for the commonservice config configuration center to make the configuration effective.

If there are few services, we can start it manually, but it will certainly have a certain impact on the stability of the business and system.

If hundreds of services are manually operated, I think the operation and maintenance personnel or technical personnel will go mad.

To solve the above problems, the commonservice config server and the business microservice are respectively configured. The server is responsible for configuring the configuration files stored in git (svn or local file system) (we use the local configuration scheme to directly update the configuration files to linux),

The business micro service obtains the relevant configuration from the server configuration center through configuration. If the configuration file changes, the latest configuration information will be obtained by refreshing the business micro service.

spring cloud Bus connects nodes of the distributed system through a lightweight message broker. This can be used to broadcast state changes, such as configuration changes, or other management instructions.

Next, we will implement the spring cloud Bus scheme to dynamically refresh the server configuration. The specific steps are as follows:

  1. For commonservice config service configuration, please refer to the previous link:

  2. Business microservice configuration (take the Hong member servcie member service as an example):

<span style="font-size: 16px;"> <dependency> <groupId>org.springframework.boot</groupId> <artifactId><span style="font-size: 16px;">spring-boot-starter-actuator</span></artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId><span style="font-size: 16px;">spring-cloud-starter-bus-amqp</span></artifactId> </dependency></span>

yml file configuration:

<span style="font-size: 16px;">server: port: 5012 spring: application: name: honghu-member-client profiles: active: dev,discoveryClient cloud: config: discovery: enabled: true service-id: commonservice-config-server <span style="font-size: 16px;"><strong>name: honghu-member profile: dev bus: trace: enabled: true #Enable message tracking < / strong > <strong>rabbitmq: host: 192.168.1.254 port: 5672 username: honghu password: honghu</strong> </span> eureka: client: serviceUrl: defaultZone: http://honghu:123456@localhost:8761/eureka/ instance: prefer-ip-address: true logging: level: root: INFO org.springframework.security: INFO management: security: enabled: false security: basic: enabled: false</span>

Write a test class (MemberController.java) to get configuration items

<span style="font-size: 16px;">package com.honghu.cloud.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; <strong>@RefreshScope</strong> @RestController public class MemberController { @Value("$") private String profile; @GetMapping("/profile") public String getProfile() { return this.profile; } }</span>
  1. Check whether the registration center, commonservice config and Honghu member service services have been registered successfully
  2. Access the profile to obtain the configuration information corresponding to the profile (original configuration):

Visit http://localhost:7071/profile = =, visit result: 123456

  1. Modify the configuration file in the config configuration center, and change the profile=123456 to honghu123456

Visit http://localhost:7071/profile = =, visit result: 123456

  1. Using spring cloud bus refresh scheme (testing with post man test tool)

http://localhost:7071/bus/refresh

Visit http://localhost:7071/profile = =, visit result: honghu123456

So far, the dynamic refresh scheme of the whole commonservice config configuration center has been sorted out

Welcome to join me in learning about spring cloud to build a microservice Cloud Architecture. I will record the process and essence of the recently developed spring cloud microservice Cloud Architecture, and help more friends who are interested in developing the spring cloud framework. Let's discuss the process of building the spring cloud architecture and how to apply it to enterprise projects.

Spring Cloud: B2B2C e-commerce platform built by distributed micro service cloud of large enterprises

Reprinted at: https://my.oschina.net/u/4045192/blog/2990761

weixin_45821812 75 original articles published, 63 praised, 2983 visited Private letter follow

9 February 2020, 23:41 | Views: 5374

Add new comment

For adding a comment, please log in
or create account

0 comments