[Spring Cloud] 05--Config configuration center

1, Basic introduction


The yml configuration file is saved to a git server, such as github.com or gitee.com
When the microservice starts, get the configuration file from the server

2, Preparatory work

1) Create a new folder config under the parent project springcloud1

2) Copy the configuration files of items 2, 3 and 4 to the config directory and modify the name

Note: generally, dev is used for running configuration, test is used for testing, and prod is used for production. The official document says so. Of course, there is no requirement for naming, which will be more professional.
3) Add a configuration to each of the three configuration files
By default, the commands in git warehouse will overwrite the local command parameters. Parameters such as port cannot be modified arbitrarily, so the settings are not overwritten.

cloud:
    config:
      override-none: true


Note that it is under spring
4) Create warehouse
Double click shift

You can select the parent project as the warehouse

5) Submission of documents

If no remote warehouse is created, create a remote warehouse.
push

3, Build configuration center server

1. Create a new module


Add eureka client,config server dependency

2. Modify pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud1</artifactId>
        <groupId>com.drhj</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.drhj</groupId>
    <artifactId>sp09-config</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sp09-config</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3. Configure application.yml

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/ronghuading/springcloud1
          search-paths: /config #/Subdirectory / subdirectory / subdirectory need not write / tree/master
server:
  port: 6001
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka

If you can't get up, delete the annotation
Note that the uri here is the warehouse address, and search paths is the subdirectory of the warehouse where config is located

4. Add @ EnableConfigServer to the startup class

package com.drhj.sp09;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer
@SpringBootApplication
public class Sp09ConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(Sp09ConfigApplication.class, args);
    }

}

5. Test

1) Start the eureka server and check whether the registration information of config server is returned
http://eureka1:2001/

2) Access configuration files in the configuration center
http://localhost:6001/item-service/dev

http://localhost:6001/user-service/dev

http://localhost:6001/order-service/dev

4, Configuration center client

1. Log off the original configuration of the service

Log off all configurations in application.yml of item service, user service and order service

2. Add config dependency

Add the following to pom.xml of the three services

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

3. Configure bootstrap.yml

Create the bootstrap.yml file. Take the user service as an example. Because the user information is added to the user service, it cannot be started without obtaining the configuration information

eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
spring:
  cloud:
    config:
      discovery:
        enabled: true #Discover the address of the configuration center from the registry
        service-id: config-server
      name: user-service
      profile: dev

Item service and order service are configured in the same way, and the corresponding name is changed

4. Restart the service

Restart the user service, get the warehouse configuration, and start it successfully

5, config bus + rabbitmq message bus configuration refresh


post requests the message bus to refresh the endpoint. The server will issue a refresh message to rabbitmq. The micro service receiving the message will request the configuration server to refresh the configuration information
Bus is a component provided by spring cloud config to assist in messaging operations and configuration refresh
RabbitMQ see: RabbitMQ basic application

1. Preparation

Add actor dependency in configuration item config

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Add bus, rabbitmq, and bind rabbit dependencies to the items that need to refresh the configuration, i.e. item service, user service, order service and configuration item config

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>

Add the configuration in the yml file of the configuration item config

1) Exposure bus refresh: m.e.w.e.i = bus refresh
2) rabbitmq connection

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/ronghuading/springcloud1
          search-paths: /config
  rabbitmq:
    host: 192.168.64.140
    port: 5672
    username: admin
    password: admin
    virtual-host: /drhj #If you have created a virtual space, add it. Pay attention to adding slashes. Use the default and do not need to add
server:
  port: 6001
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

2. Configure refresh

Modify the three files in the config directory and submit

1) Add connection for rabbitmq

2) Submit push to remote warehouse

Start project test

Note that the startup sequence is
eureka -> config -> item,user,order -> zuul
After normal startup, clear the console information
Using postman to access http://localhost:6001/actuator/bus-refresh note that all parameters, request headers, etc. should be cleared. It is best to create a new test

View console

The above indicates that the refresh is successful
To refresh only one service, add a sub path, such as: http://localhost:6001/actuator/bus-refresh/item-service

3. Summary

Therefore, to change the configuration, you only need to modify the corresponding yml file in the config where the yml file is stored, then upload it to the git remote warehouse, and manually refresh the configuration by using rabbitmq.

Tags: Spring Cloud config

Posted on Sat, 23 Oct 2021 03:01:38 -0400 by Irresistable