Spring Cloud2.x用kafka实现消息总线的坑

本文介绍了在Spring Boot 2.0.3和Spring Cloud Finchley版本下,使用Kafka作为消息总线时遇到的404错误问题。配置错误分析包括:config-client的依赖添加、配置文件设置以及@RefreshScope注解的使用。同时,指出了Spring Boot 2.0中/bus/refresh路径变更为/actuator/bus-refresh的重要更新。

框架版本

Spring Boot版本:2.0.3
Spring Cloud版本:Finchley
Kafka版本:2.0.0

遇到的问题

我按照博客:史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)(Finchley版本)配置之后,在发送post请求:http://localhost:8881/actuator/bus-refresh 后,报出了404的错误,半天摸不到头绪,不过肯定是配置上出了问题,一定要确保以下几点配置:
1.必须在config-client中pom文件添加以下依赖:

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

2.必须在config-client配置文件中添加以下配置:

management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

3.必须在调用@Value注解类上添加注解:

@RefreshScope 

4.请求刷新的页面由原来1.5.x的localhost:8881/bus/refresh,变成:http://localhost:8881/actuator/bus-refresh,因为Spring boot 2.0的改动较大,/bus/refresh全部整合到actuador里面了。

config-server配置:

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

    <groupId>com.gewdata</groupId>
    <artifactId>config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>config-server</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.gewdata</groupId>
        <artifactId>springCloudConfigDemo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependencies>

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

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


</project>

2.配置文件application.properties:

spring.application.name=config-server
server.port=8888

# 配置git仓库地址
spring.cloud.config.server.git.uri=http://139.159.143.78:10086/wangjunyao/SpringCloudDemo.git
# 配置仓库路径
spring.cloud.config.server.git.searchPaths=config
# 配置仓库分支
spring.cloud.config.label=master
# 访问git仓库用户名
spring.cloud.config.server.git.username=wangjunyao
# 访问git仓库用户密码
spring.cloud.config.server.git.password=12345678
# 服务注册地址
eureka.client.serviceUrl.defaultZone=http://localhost:8889/eureka/

config-client配置

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

    <groupId>com.gewdata</groupId>
    <artifactId>config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>config-client</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.gewdata</groupId>
        <artifactId>springCloudConfigDemo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-kafka</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-web</artifactId>
        </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>

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

2.配置文件bootstrap.properties:

# 这里的配置是和git上文件名相对应的
spring.application.name=wjy-client
# 指明分支
spring.cloud.config.label=master
spring.cloud.config.profile=dev
# 指明配置服务中心网址
#spring.cloud.config.uri= http://localhost:8888/
server.port=8881

# 高可用
# 指定服务注册地址
eureka.client.serviceUrl.defaultZone=http://localhost:8889/eureka/
# 是从配置中心读取文件
spring.cloud.config.discovery.enabled=true
# 配置中心的servield,即是服务名
spring.cloud.config.discovery.serviceId=config-server

#消息总线配置
spring.cloud.bus.enabled=true
spring.cloud.bus.trace.enabled=true
management.endpoints.web.exposure.include=bus-refresh
#Kafka的服务端列表,默认localhost
spring.cloud.stream.kafka.binder.brokers=localhsot:9092
#Kafka服务端的默认端口,当brokers属性中没有配置端口信息时,就会使用这个默认端口,默认9092
spring.cloud.stream.kafka.binder.defaultBrokerPort=9092
#ZooKeeper节点的默认端口,当zkNodes属性中没有配置端口信息时,就会使用这个默认端口,默认2181
spring.cloud.stream.kafka.binder.defaultZkPort=2181

3.在调用到@Value方法的类上添加注解@RefreshScope:

@SpringBootApplication
@RestController
@RefreshScope // 刷新配置
public class ConfigClientApplication {

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

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

    @RequestMapping(value = "/hi")
    public String hi(){
        return msg;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值