SpringCloud 的 config组件

4.config 组件

1.configserver 配置

1.写pom
        <!--config server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

2.写配置
server:
  port: 8848
spring:
  application:
    name: CONFIGS
  cloud:
    consul:
      host: localhost
      port: 8500
    config:
      server:
        git:
        #分支
          default-label: master
        #uri
          uri: https://gitee.com/breezewangsz/config.git
# 私有库要写用户名和密码
#          username:
#          password:

3.在启动类上写注解
@EnableConfigServer
@EnableDiscoveryClient

2.configclinet 配置

1.写pom
        <!--config client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

		<!--使用远程配置 bootstrap.yaml 要写pom 没有会保找不到yaml的错误-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
2.写配置
#注册中心要写本地
spring:
  cloud:
    consul:
      port: 8500
      host: localhost

    config:
      discovery:
      #配置所在服务Id
        service-id: CONFIGS
      #开启云配置
        enabled: true

#所在分支
      label: master
#服务名字
      name: configclient
#环境
      profile: dev

3.在启动类上写注解
@EnableDiscoveryClient

3.手动配置刷新

1.在controller类上加注解
@RefreshScope
//不要重启微服务的情况下将当前scope域中刷新为最新配置
2.curl -X POST http://localhost:客户端服务器端口/actuator/refresh

必须暴露端点(服务端客户端都要加)

management:
  endpoints:
    web:
      exposure:
        include: "*"

4.自动刷新

1.写pom
服务端
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--discovery 服务发现注册-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

        <!--健康检查依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--config client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

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

        <!--discovery 服务发现注册-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

        <!--健康检查依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--config server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <!--bus依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
2.写配置
服务端
server:
  port: 8848
spring:
  application:
    name: CONFIGS
  cloud:
    consul:
      host: localhost
      port: 8500
    config:
      server:
        git:
          default-label: master
          uri: https://gitee.com/breezewangsz/config.git
#          username:
#          password:
  rabbitmq:
    host: xxxxxxxxx
    port: xxxx
    username: xxxx
    password: xxxx
    virtual-host: /

management:
  endpoints:
    web:
      exposure:
        include: "*"
客户端
spring:
  cloud:
    consul:
      port: 8500
      host: localhost

    config:
      discovery:
        service-id: CONFIGS
        enabled: true

      label: master
      name: configclient
      profile: dev

#启动时当远程配置还没有完整时项目启动所以的失败都是运许的(没配见错误1)
      fail-fast: true

management:
  endpoints:
    web:
      exposure:
        include: "*"

3.注解
服务端
@EnableDiscoveryClient
@SpringBootApplication
客户端
@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
远程仓库
4.刷新
#刷新所有服务(new)
http://localhost:8848/actuator/busrefresh
(old)
http://localhost:8848/actuator/bus-refresh
#刷新指定服务
http://localhost:8848/actuator/busrefresh/服务ID
(old)
http://localhost:8848/actuator/bus-refresh/服务ID
错误1
错误
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值