Greenwich.SR2 version of the Spring Cloud Config + BUS examples

 

 

  Spring Cloud Config center with a unified configuration registry Eureka, as also Excellent service and client. The server used to store configuration information used by clients to read. Its advantage is based on the Git repository, it supports multi-environment, multi-branch configuration, dynamic refresh. We service gateway Zuul (see Spring Cloud Zuul instance Greenwich.SR2 version ) Config as a client, it's routing configuration (beginning zuul.routes configuration items) we configured from the Git, this way we directly modify the local Git after routing configuration and then push on to a remote warehouse, call / bus / refresh to take effect, achieve dynamic routing. Similarly, other micro-business service components can also be read as a configuration Config client.

  As for how to implement the configuration information to support dynamic updates, it requires an additional component of the shot: Spring Cloud BUS, message bus. The principle is the configuration updates on Git, by calling / bus / refresh interfaces, message-based middleware Config pushed to all clients. Here messaging middleware with Kafka. Config server also supports clustering, in order to allow the client calls Config Config server clusters convenience, we integrated Eureka to discover Config server.

  Look at the Config service side, three axes:

  1、pom:

 

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>configuration-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <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.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-kafka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-kafka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-bus</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

</project>

  2、application:

# Local port 
server.port = 8766 

# This machine service name 
spring.application.name = config- Server 

# registry address 
eureka.client.service -url.defaultZone = HTTP: // localhost: 8888 / Eureka / 

#git warehouse address 
spring.cloud.config.server.git.uri = HTTPS: // github.com/wuxun1997/spring-cloud-demo 

search path path under #Git warehouse 
spring.cloud.config.server.git.search -paths = config 

supports dynamic refresh 
spring.cloud.bus.refresh.enabled = to true 
management.endpoints.web.exposure.include = BUS- refresh 

# configure BUS use of messaging middleware Kafka used to live 
spring.kafka.bootstrap-servers=127.0.0.1:9092

  3, through the center of the main class launch configuration @EnableConfigServer:

package hello;

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

@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigServiceApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(ConfigServiceApplication.class, args);
    }
}

  Kafka need to start local server before starting the Config, otherwise it would have been prompted to connect failed. Rom if you git repository, plus application.properties user name, password, configuration:

#Git warehouse user, password 
spring.cloud.config.server.git.username = your Git repository user name XXX 
spring.cloud.config.server.git.password = your Git repository password XXX

  Now we create two profiles in the git repository:

  hello.properties:

  config/configclient-test.properties:

 

 

 

 

  We can directly Config server requests:

 

  Then we take a look at new projects Config client. Again three axes:

  1、pom:

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>configuration-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-kafka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-kafka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-bus</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

</project>

  2、bootstrap:

#本机端口
server.port=8778

#本机服务名
spring.application.name=configClient

#注册中心地址
eureka.client.service-url.defaultZone=http://localhost:8888/eureka/

#支持注册中心访问Config服务端
spring.cloud.config.discovery.enabled=true

#Config服务端服务名
spring.cloud.config.discovery.service-id=config-server

#git仓库配置文件分支(默认即为master)
spring.cloud.config.label=master

#git仓库配置文件环境信息
spring.cloud.config.profile=test

  3、主类通过@RefreshScope支持动态获取Config最新配置:

package hello;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication {

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

@RefreshScope
@RestController
class MessageRestController {

    @Value("${message:Hello default}")
    private String message;

    @RequestMapping("/message")
    String getMessage() {
        return this.message;
    }
}

  现在我们先通过Config客户端获取到Git仓库的master分支下config目录下的configClient(默认拿Config客户端的服务名作为properties文件名)-test(环境)的配置项:

 

   接下来我们动态刷新,直接去Git仓库上编辑message的值好了:

  提交后我们直接看Config服务端,也自动更新了:

  但是这是客户端并未更新,需要我们调用/bus-refresh接口去刷新一下服务端:

  这会儿再调用客户端就能获取最新配置了:

 

Guess you like

Origin www.cnblogs.com/wuxun1997/p/11227394.html