springcloud——服务配置(微服务从ConfigServer上读取yml或properties文件)

bootstrap.yml:是系统级的配置文件,优先级更高。application.yml:是用户级的配置文件,优先级比系统级低。SpringCloud会创建一个“Bootstrap Context”,作为Spring应用的“Application Context”的父上下文。初始化的时候,“Bootstrap Context”负则从外部源加载配置属性并解析配置。这两个上下文共享一个外部获取的‘Environment’。“Bootstrap”属性有高优先级,默认情况下,它们不会被本地配置覆盖。‘Boo
摘要由CSDN通过智能技术生成

bootstrap.yml:是系统级的配置文件,优先级更高。
application.yml:是用户级的配置文件,优先级比系统级低。
SpringCloud会创建一个“Bootstrap Context”,作为Spring应用的“Application Context”的父上下文。初始化的时候,“Bootstrap Context”负则从外部源加载配置属性并解析配置。这两个上下文共享一个外部获取的‘Environment’。
“Bootstrap”属性有高优先级,默认情况下,它们不会被本地配置覆盖。‘Bootstrap Context’和‘Application Context’有着不同的约定,所以新增了一个‘bootstrap.yml’文件,保证‘Bootstrap Context’和‘Application Context’配置的分离。
要将client模块下的application.yml文件改写为bootstrap.yml,这是很关键的,因为bootstrap.yml是比application.yml先加载的。bootstrap.yml优先级高于application.yml。

在这里插入图片描述

1、pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>com.springcloud</groupId>
        <version>1.0.0</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-client3355<
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud中,可以使用@ConfigurationProperties注解读取YAML文件中的列表类型的配置。与在Spring Boot中读取类似,但需要添加`@RefreshScope`注解,以便在配置更改时自动刷新。 假设你有一个名为 `config.yml` 的YAML文件,其中包含名为 `my-list` 的列表类型的配置,可以按照以下步骤读取它: 1. 引入Spring Cloud和YAML依赖包。在Maven项目中,可以在pom.xml中添加以下依赖项: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</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-yaml</artifactId> </dependency> ``` 2. 创建一个@ConfigurationProperties类,用于映射YAML中的配置。例如: ```java import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.stereotype.Component; import java.util.List; @Component @ConfigurationProperties(prefix = "my") @RefreshScope public class MyConfig { private List<String> list; public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } } ``` 在这个类中,我们使用`@ConfigurationProperties`注解来指示要映射的配置前缀。在这个例子中,我们使用了`my`前缀。然后,我们定义了一个`list`属性,它将映射到名为`my-list`的配置。我们还添加了`@RefreshScope`注解,以便在配置更改时自动刷新。 3. 在YAML文件中定义列表配置。例如: ```yaml my: list: - item1 - item2 - item3 ``` 在这个例子中,我们使用了`my`前缀来定义配置,然后在该前缀下定义了一个名为`list`的配置。注意,这个配置的值是一个包含三个字符串元素的列表。 4. 在应用程序中使用`MyConfig`类。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MyApp { @Autowired private MyConfig myConfig; public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } // 在任何地方使用 myConfig.getList() 获取列表配置 } ``` 在这个例子中,我们使用`@Autowired`注解将`MyConfig`类注入到`MyApp`类中。然后,我们可以在任何地方使用`myConfig.getList()`方法来获取YAML文件中的列表配置。在这个例子中,这个方法将返回一个包含三个字符串元素的列表,即`["item1", "item2", "item3"]`。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值