【Spring Cloud】02-Config

添加pom依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</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-security</artifactId>
</dependency>

添加配置文件

server:
  port: 8888

spring:
  application:
    name: @artifactId@
  profiles:
    active: native
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

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

# 本地开发模式
---
spring:
  profiles: native
  security:
    user:
      name: admin
      password: 123456
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/config/   #开发模式读取工程目录下面配置

eureka:
  instance:
    prefer-ip-address: true   #访问路径可以显示IP地址
  client:
    service-url:
      defaultZone: http://admin:admin@springcloud-eureka:8761/eureka/

# 生产模式
---
spring:
  profiles: prod
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/756585379/springcloud-config-repo.git
          username: your_username
          password: your_password
          clone-on-start: true #开启启动时直接从git获取配置
#          search-paths: '{application}'

eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://admin:admin@springcloud-eureka:8761/eureka/,http://admin:admin@springcloud-eureka:8762/eureka/,http://admin:admin@springcloud-eureka:8763/eureka/

刷新配置
在bootstrap.yml添加如下配置,暴露/actuator/refresh 端点:

# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: '*'

待刷新的配置属性所在的类上添加了@RefreshScope注解 ,例如:

@RestController
@RefreshScope
public class ConfigClientController {
  @Value("${profile}")
  private String profile;

  @GetMapping("/profile")
  public String hello() {
    return this.profile;
  }
}

修改profile 配置后,只需向应用的/actuator/refresh 端点发送POST请求,即可刷新该属性。例如:

curl -X POST http://localhost:8082/actuator/refresh

实战项目地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容