天天看點

消息總線

SpringCloudBus:事件、消息總線,用于在叢集(例如,配置變化事件)中傳播狀态變化,可與Spring Cloud Config聯合實作熱部署。

在上一篇寫出了springcloud對微服務的集中配置,那麼就出現了一個問題,如果修改配置了怎麼實作不需重新開機服務來實作配置的更新,下面有集中解決方法。

1.使用/refresh手動重新整理配置

缺點:單點重新整理,如果叢集服務多的話,無論是工作量還是維護上都十分麻煩。

使用上一篇的config-client服務,加入依賴,

spring-boot-starter-actuator
           

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.dalaoyang</groupId>
    <artifactId>springcloud_config_client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springcloud_config_client</name>
    <description>springcloud_config_client</description>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</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-config</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>${spring-cloud.version}</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>
           

在Controller類上加入@RefreshScope注解,由于上一篇controller寫在了啟動類上,是以直接加在啟動類上,代碼如下:

package com.dalaoyang;

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

@SpringBootApplication
@RestController
@RefreshScope
public class SpringcloudConfigClientApplication {

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


    @Value("${title}")
    String title;

    @RequestMapping("/getTitle")
    public String getTitle(){
        return title;
    }

}
           

配置檔案新增配置management.security.enabled=false,在重新整理時關閉安全驗證。

代碼如下:

spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=test
spring.cloud.config.uri= http://localhost:9000/

##注冊中心位址
eureka.client.service-url.defaultZone=http://eureka.dalaoyang.cn/eureka/

## 重新整理時,關閉安全驗證
management.security.enabled=false
           

分别啟動項目config-server,config-client。通路

http://localhost:8080/getTitle

,結果如下圖

image

修改git上配置,修改為dalaoyang_test_change,在次請求,結果沒有改變,使用postman或者其他工具post請求

看到傳回如下結果。

在次通路

,如下圖

2.使用springcloudbus重新整理配置

springcloudbus需要使用輕量消息代理,本文使用rabbitmq,啟動rabbitmq如下圖:

通路

http://localhost:15672/#/

如下圖

建立項目springcloud_bus,同時改造config-client,pom檔案加入bus依賴,代碼如下:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.dalaoyang</groupId>
    <artifactId>springcloud_bus</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springcloud_bus</name>
    <description>springcloud_bus</description>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</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-config</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</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>
           

加入rabbitmq配置,配置檔案如下:

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

##端口号
server.port=8881

## 重新整理時,關閉安全驗證
management.security.enabled=false

spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=test
spring.cloud.config.uri= http://localhost:9000/

##注冊中心位址
eureka.client.service-url.defaultZone=http://eureka.dalaoyang.cn/eureka/
           

啟動類加入注解@RefreshScope,代碼如下:

package com.dalaoyang;

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

@SpringBootApplication
@RestController
@RefreshScope
public class SpringcloudBusApplication {

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


    @Value("${title}")
    String title;

    @RequestMapping("/getTitle")
    public String getTitle(){
        return title;
    }
}
           

config-client加入同樣依賴和配置,重新開機config-client,啟動springcloud_bus,先去

http://eureka.dalaoyang.cn/

,可以看到

先将git上配置改回dalaoyang_test,分别請求

http://localhost:8881/getTitle

結果如下:

然後用postman使用post請求通路

http://localhost:8881/bus/refresh

再次分别請求

從圖中可以看出重新整理配置成功。

3.局部重新整理配置,配置與第2種方法一樣,隻是在使用postman重新整理時略加改變,在本文中使用

http://localhost:8881/bus/refresh?destination=config-client:8881

可以重新整理服務名為config-client端口為8881的服務,如果想要重新整理服務名為config-client的所有服務可以寫成

http://localhost:8881/bus/refresh?destination=config-client:**

源碼下載下傳 :

大老楊碼雲

個人網站:

https://www.dalaoyang.cn