美文网首页
【Spring Cloud】02-Config

【Spring Cloud】02-Config

作者: Y了个J | 来源:发表于2019-08-27 21:43 被阅读0次

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

实战项目地址

相关文章

网友评论

      本文标题:【Spring Cloud】02-Config

      本文链接:https://www.haomeiwen.com/subject/zwwyectx.html