1. 程式人生 > 其它 >第八篇: Spring Cloud Bus(Hoxton版本)

第八篇: Spring Cloud Bus(Hoxton版本)

技術標籤:SpringCloudspringcloud

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"
>
<modelVersion>4.0.0</modelVersion> <parent> <groupId>com.xiaobu</groupId> <artifactId>springcloud-demo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.xiaonbu</groupId> <
artifactId
>
config-bus</artifactId> <version>0.0.1-SNAPSHOT</version> <name>config-bus</name> <description>config-bus project for Spring Boot</description> <properties> <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</artifactId> </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-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <!-- 使用dependencyManagement進行版本管理 --> <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>

bootstrap.properties

# 從config-server獲取配置資訊
#server.port=7002
#{application}
#spring.application.name=xiaobu
#{profile}
#spring.cloud.config.profile=dev
#{label}
#spring.cloud.config.label=master
# spring.cloud.config.uri=http://localhost:7001/
# 從eureka-server獲取配置資訊
#{application}
spring.application.name=xiaobu
#{profile}
spring.cloud.config.profile=dev
#{label}
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:7001/
eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
server.port=7003

ConfigBusApplication

package com.xiaonbu;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
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.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@RestController
@RefreshScope
@Slf4j
public class ConfigBusApplication implements CommandLineRunner {

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


    @Value("${server.port}")
    private String port;


    @Override
    public void run(String... args) throws Exception {
        log.info("config-client 在埠{}啟動成功", port);
    }


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

    @GetMapping("/from")
    public String from() {
        return from;
    }
}

依次啟動 eureka-server config-server config-bus

訪問http://localhost:8001/

1610613732.png

訪問 http://localhost:7003/from

git-dev-1.0 QAQ

修改程式碼倉庫的檔案內容 from=git-dev-1.0 hasagei!

先post一下 http://localhost:7003/actuator/bus-refresh

然後先前是G版本的 無論怎麼post都不行,後面查資料才知道Greenwich.M1版本的bus匯流排/actuactor/bus-refresh時發生問題。

所以改成了H版本 一下就成功了。

再訪問 http://localhost:7003/from 出現

git-dev-1.0 hasagei!

spring cloud config+bus配置中心,出現Dispatcher has no subscribers錯誤