1. 配置管理

 
 实现配置管理热更新。
2. 配置编写

 
3. 微服务拉取Nacos配置

- 引入加载bootstrap.yaml的依赖。
 
  <!--nacos配置管理-->
  <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  </dependency>
  <!--读取bootstrap文件-->
  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-bootstrap</artifactId>
  </dependency>
 
- bootstrap.yaml
 
spring:
  application:
    name: cart-service
  profiles:
    active: dev
  cloud:
    nacos:
      server-addr: 139.224.66.95:8848
      config:
        file-extension: yaml
        shared-configs:
          - data-id: shared-jdbc.yaml
          - data-id: shared-log.yaml
          - data-id: shared-swagger.yaml
 
- application.yaml
 
server:
  port: 8084
feign:
  okhttp:
    enabled: true # 开启OKHttp功能
hm:
  db:
    host: localhost
    database: hm-cart
  swagger:
    title: "购物车管理接口"
    package: com.hmall.cart.controller
 
4. 配置热更新
当修改配置文件中的配置时,微服务无需重启即可使配置生效。
- 配置类
 
@Data
@Component
@ConfigurationProperties(prefix = "hm.cart")
public class CartProperties {
    private Integer maxItems;
}
 
- nacos配置文件

 









