SpringCloudConfig和bus的基本使用

SpringCloudConfig和bus的基本使用

一、SpringCloudConfig服务端配置

(1)添加依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</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.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
(2)修改配置
spring:
  application:
    name: config-center
  cloud:
    config:
      server:
        git:
          uri: git地址
          search-paths:
            - 项目名
          username: 用户名
          password: 密码
      label: master
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
      
management:
  endpoints:
    web:
      exposure:
        include: "*"
(3)启动类
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigCenterApplication3005 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterApplication3005.class,args);
    }
}
(4)访问地址

http://config服务的IP:PORT/master/config-test.yml

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oOYI2D4O-1646126488796)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220301165437459.png)]

注意:如果上面没有配置git的用户名和密码,很可能出现如下的错误:

Authentication is required but no CredentialsProvider has been registered

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-E8oSv6kc-1646126488798)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220301151516772.png)]

二、SpringCloudConfig客户端配置

(1)引入依赖
 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-config</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.boot</groupId>
</dependency>

(2)创建配置文件bootstrap.yml
server:
  port: 3006

spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://localhost:3005
      name: config
      profile: test
      label: master
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
management:
  endpoints:
    web:
      exposure:
        include: "*"
(3)主启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication3006 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication3006.class,args);
    }
}
(4) 获取配置
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

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

    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return configInfo+"/t"+port;
    }
}
(5)使用curl发送post请求
curl -X POST "http://ip:config客户端/actuator/refresh"

当git上出现更新的时候,客户端也会更新,如果不采用上述方式,服务端会更新,客户端不能更新。

以上内容都是单应用节点的,当出现多节点需要引入bus进行下述配置

三、SpringCloudBus的配置

以下使用的是rabbitmq的方式

(1)引入依赖
<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
(2)增加rabbitmq的配置
spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
(3)使用curl 发送post请求
curl -X POST "http://localhost:3005/actuator/bus-refresh"

如果单独想刷新个别应用,使用如下方式:

curl -X POST "http://localhost:3005/actuator/bus-refresh/${spring.application.name}:${server.port}"

s p r i n g . a p p l i c a t i o n . n a m e 和 {spring.application.name}和 spring.application.name{server.port}需要替换成当前需要刷新的服务的配置文件中的内容

efresh"


**如果单独想刷新个别应用,使用如下方式:**

```java
curl -X POST "http://localhost:3005/actuator/bus-refresh/${spring.application.name}:${server.port}"

' s p r i n g . a p p l i c a t i o n . n a m e 和 {spring.application.name}和 spring.application.name{server.port}需要替换成当前需要刷新的服务的配置文件中的内容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值