springCloud--configuration center config automatic update

First, install rabbitmq

1.1
If you want to use spring cloud amqp, you need to install rabbitmq. We can download through the official website https://www.rabbitmq.com/download.html . I use a mac, after downloading and decompressing, execute $ RABBITMQ_HOME / sbin / rabbitmq-server to start rabbitmq.

The default user and password of rabbitmq are both guests, and the default port is 5672

I won't say much about other rabbitmq.

Second, transform config-server and client-a

2.1
Add pom files under the two modules config-server and client-a

<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>

Here it is explained that spring-boot-starter-actuator is the monitoring module that comes with spring-boot. If we want to use spring-cloud-starter-bus-amqp, it must also be added.

2.2
Modify the configuration file of the client-a module, mainly to add the configuration of rabbitmq, the modification is as follows, and the configuration file of config-server does not need to be modified

server:
  port: 8910

eureka:
  client:
    serviceUrl:
          defaultZone: http://localhost:8010/eureka/

spring:
  application:
      name: client-a
  cloud:
      config:
        discovery:
          enabled: true #开启通过服务来访问Config Server的功能
          service-id: config-server
        profile: dev
        label: master

  rabbitmq:
      host: localhost
      port: 5672
      username: guest
      password: guest

2.3
Note that to achieve automatic configuration updates, here you need to modify the Client-a TestController, add @RefreshScope annotation

@RestController
@RefreshScope
public class TestController {
    ...
}

2.4
Restart config-server and client-a

You can pay attention to the startup log, there should be a section of
osbaemvc.EndpointHandlerMapping: Mapped "{[/ bus / refresh], methods = [POST]}"
This is how to trigger configuration refresh.

Open http: // localhost: 8910 / getProperties and you should see the configuration is still the old one

Modify the configuration file on git

Access http: // localhost: 8030 / bus / refresh in the configuration center as a post to trigger the configuration update, see the local log, config-server and client-a will have the log of refresh configuration

Open http: // localhost: 8910 / getProperties again and you should see the configuration has been updated

2.5
Although the configuration can be updated without restarting the service, we still need to manually operate it, which is still undesirable.
Therefore, git webhooks will be used here to achieve automatic configuration updates.

Open the address of the configuration repository on git and add webhooks

The above Payload URL fills in the address of our configuration center to trigger the refresh, of course, you can not write localhost here, you need to access the address from the Internet.

There is also a Secret key verification in it. If it is filled in here, the encryption.key should be written in the configuration file corresponding to it.

Published 7 original articles · 69 praises · 200,000+ views

Guess you like

Origin blog.csdn.net/u014320421/article/details/79700947
Recommended