微服务-springcloud-bus-kafka

£可爱£侵袭症+ 提交于 2020-03-10 20:31:51

消息总线bus的作用:实现各个联通配置中心的微服务能够动态的批量的更新配置

服务端

引入依赖

 <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--&lt;!&ndash;使用消息总线&ndash;&gt;-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-kafka</artifactId>
        </dependency>

修改配置

server.port=8888
spring.application.name=company-info-config-client
spring.cloud.config.server.git.uri=https://github.com/zzzqqq123/springcloudtest.git
spring.cloud.config.server.git.username=zhangq@webyun.cn
spring.cloud.config.server.git.password=zhangqiang@123
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
#设置注册表检查时间,默认是30秒,在客户端配置
eureka.client.registry-fetch-interval-seconds=5
#调整客户端应用状态上报的周期
eureka.client.instance-info-replication-interval-seconds=5
#Eureka 客户端应用实例状态 URL,点击eureka的时候跳转得地址,默认是info
eureka.client.service-url.defaultZone=http://localhost:60000/eureka
eureka.instance.prefer-ip-address=true
#点开status下的实例跳转的页面
eureka.instance.statusPageUrlPath=/actuator/health
#页面中status下的数据
eureka.instance.instance-id=${spring.application.name}/${server.port}

修改启动类


@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
@EnableConfigServer

启动服务
客户端

引入依赖

<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>

        <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-bus-kafka</artifactId>
        </dependency>

##修改配置
application.properties

spring.application.name=company-info-config-client
server.port=8889
#http://localhost:8889/actuator/refresh 需要进行刷新
management.endpoints.web.exposure.include=*
#  health,info,env
management.endpoint.health.show-details=always

bootstrap.properties

spring.cloud.config.uri=http://localhost:8888/
spring.cloud.config.name=application
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.kafka.bootstrap-servers=localhost:9092

修改启动类
启动服务

@RestController
@RefreshScope//刷新
@RequestMapping("user")
public class UserController {
    @Value("${user.name}")
    private String name;
    @GetMapping("name")
    public String testValue(){
        return name;
    }
}

访问服务端POSt请求:post访问: http://localhost:8888/actuator/bus-refresh,然后在访问客户端,发现数据改变,不使用消息总线bus的时候刷新的是客户端,数据才能发生改变

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!