(16) Integrate Spring Cloud architecture-refresh configuration with spring Cloud bus

Source: Internet
Author: User

We made the B2B2C e-commerce system with the spring Cloud distributed micro-service cloud architecture, in addition to the architecture itself with the system services, we have B2B2C business services to a fine-grained split, made a different business microservices.

As our business systems become more and more complex, the variety of configurations increases. As soon as a configuration file is modified, the Commonservice-config Configuration center stops the service and then restarts, finally making the configuration effective.

If the service is small, we can start it manually, but there must be some impact on the stability of the business and the system.

If hundreds of thousands of services are done by hand, I estimate that OPS or technicians will go insane.

In response to the above issues, the Commonservice-config server and the business microservices are configured separately, and the server is responsible for configuring the configuration files stored in git (SVN or local file system) (we use a local configuration scheme, Easy to update the configuration file directly to Linux),

The business microservices are configured to get the relevant configuration from the server-side configuration center, and if the configuration file changes, the latest configuration information is obtained by refreshing the business micro-service.

Spring Cloud Bus connects the nodes of a distributed system through a lightweight message broker. This can be used for broadcast state changes (such as configuration changes) or other management directives.

Next, we'll implement a dynamic refresh of the server configuration through the Spring Cloud bus scenario, with the following steps:

1. Commonservice-config service configuration can refer to the previous

2. Business Micro-service configuration (take Honghu-member-servcie member service as an example):

Pom File configuration:

<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:5012Spring:application:name:honghu-member-client profiles:active:dev,discoveryclient cloud:config:discovery:enabled:trueService-id:commonservice-config-Server<span style= "color: #ff0000;" ><strong>name:honghu-member Profile:dev bus:trace:enabled:true#开启消息跟踪 </strong> <strong>Rabbitmq:host:192.168.1.254Port:5672Username:honghu Password:honghu</strong> </span>eureka:client:serviceUrl:defaultZone:http://Honghu:[email protected]:8761/eureka/Instance:prefer-ip-address:trueLogging:level:root:INFO org.springframework.security:INFOmanagement:security:enabled:falsesecurity:basic:enabled:false</span>

Write a test Class (Membercontroller.java) to get the configuration items

<span style= "FONT-SIZE:16PX;" > PackageCom.honghu.cloud.controller;ImportOrg.springframework.beans.factory.annotation.Value;ImportOrg.springframework.cloud.context.config.annotation.RefreshScope;Importorg.springframework.web.bind.annotation.GetMapping;ImportOrg.springframework.web.bind.annotation.RestController;<strong> @RefreshScope </strong>@RestController Public classMembercontroller {@Value ("${profile}")    PrivateString profile; @GetMapping ("/profile")     PublicString GetProfile () {return  This. Profile; }}</span>

3. Check the registration center, Commonservice-config, Honghu-member-service Service has been registered successfully


4. Access profile to obtain configuration information (original configuration):

Visit Http://localhost:7071/profile = = "Access results: 123456

5. Modify the configuration file in Config Configuration Center to change the profile=123456 to honghu123456

Visit Http://localhost:7071/profile = = "Access results: 123456

6. Use the Spring Cloud Bus refresh scenario (test with the Post Man Test tool)

Http://localhost:7071/bus/refresh

Visit Http://localhost:7071/profile = = "Access Result: honghu123456

To this, the entire Commonservice-config Configuration Center dynamic Refresh solution finished!

Welcome to join me in learning Spring Cloud to build a micro-service cloud architecture, I will take the recent development of the spring cloud micro-service cloud architecture and the essence of the building process to record, to help more interested in the development of Spring cloud framework of friends, we come together to explore the spring How cloud architecture is built and how it can be used in enterprise projects.

Source source technical support for complete projects 1791743380

(16) Integrate Spring Cloud architecture-refresh configuration with spring Cloud bus

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.