如何配置Spring Cloud配置服务器从文件系统读取属性?

问题描述 投票:0回答:1

我有一个多Maven项目

project:
├── pom.xml
│
├── project-config-provider
│   ├── pom.xml
│   ├── src
│   │   └── main
│   │       └── java
│   │           └── com
│   │               └── company
│   │                   └── project
│   │                       └── config_provider
│   │                           └── Application.java
│   ├── config
│   │   ├── application_config-provider--native.yaml
│   │   └── application_config-provider--aws.yaml
│   │
│   └── cloud_config
│       ├── application-config_main.yaml
│       ├── application-config_storage.yaml
│       ├── application-config_swagger.yaml
│       └── application-env_local.yaml
│
│
└── project-abc
    ├── pom.xml
    ├── src
    │   └── main
    │       └── java
    │           └── com
    │               └── company
    │                   └── project
    │                       └── abc
    │                           └── AbcApplication.java
    └── config
        └── application_abc.yaml

我正在学习如何使用 spring cloud config 集中配置。 我想使用文件系统配置它。

模块

project-config-provider
必须是Spring Cloud配置服务器, 并且模块
projcet-abc
将使用project-config-provider中的一些配置。

每个模块都有一个config文件夹:

  • project-config-provider 包含多个 yaml 文件,每个环境一个(application_config-provider--native.yaml 用于本地测试,application_config-provider--aws.yaml 用于使用 Secretmanager 和 S3 进行 aws 测试)

application_config-provider--native.yaml:

spring:
   application:
      name: config-provider
   profile:
      active: native
   cloud:
      config:
         server:
            native:
               search-location: cloud_config
            prefix: /config
            bootstrap: true

server.port: 8888
  • 所有其他模块只有一个 yaml 文件

application_abc.yaml:

spring:
   application:
      name: maps
      version: 1.0.0
   profiles:
      #inlcude configurations from cloud_config of config-provider 
      include: config_main, config_storage, config_swagger
   config:
      import: configserver:http://${CONFIG_PROVIDER_HOST:localhost}:${CONFIG_PROVIDER_PORT:8888}/config

项目-abc应用:

@SpringBootApplication
@ComponentScan({ "com.project*" })
@EntityScan("com.project*")
@EnableJpaRepositories("com.project*")
public class AbcApplication {

    public static void main(String[] args) {
        SpringApplication.run(AbcApplication.class, args);
    }

}

在project-abc pom.xml上,我添加了:

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

项目配置提供者应用程序:

@EnableConfigServer
@ComponentScan(basePackages = { "com.project" } )
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class Application {

    public static void main(final String[] args) {
        SpringApplication.run(Application.class, args);
    }
    
    @Configuration
    public static class SecurityPermitAllConfig {
        
        @Bean
        public SecurityFilterChain filterChain(final HttpSecurity httpSecurity) throws Exception {
            httpSecurity.authorizeRequests().anyRequest().permitAll().and().csrf().disable();
            return httpSecurity.build();
        }
        
    }
    
}

项目配置提供者 pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  
  <parent>
    <groupId>com.project</groupId>
    <artifactId>project</artifactId>
    <version>1.0.0</version>
    <relativePath>../</relativePath>
  </parent>
  
  <artifactId>project-config-provider</artifactId>
  <name>Project Config Provider</name>
  
  <dependencies>
     
    <!-- cloud config server -->        
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    
    <!-- custom configuration in application-*.yaml -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

  </dependencies>   
    
</project>

Spring Boot 应用程序project-config-provider 运行时使用

--spring.config.import=config/application-config-provider--native.yaml

作为程序参数,但一旦启动,它就会失败:

***************************
APPLICATION FAILED TO START
***************************

Description:

Invalid config server configuration.

Action:

If you are using the git profile, you need to set a Git URI in your configuration.  If you have set spring.cloud.config.server.bootstrap=true, you need to use a composite configuration.

我没有使用git..但我已经设置了bootstrap=true,
我还尝试在 application_config-provider--native.yaml 上使用复合:

spring:
   application:
      name: config-provider
   profile:
      active: composite
   cloud:
      config:
         server:
            composite:
             - type: native
               search-locations: cloud_config
            prefix: /config
            bootstrap: true

server.port: 8888
java spring spring-boot spring-cloud-config
1个回答
0
投票

项目配置提供者 pom.xml :

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  
  <parent>
    <groupId>com.project</groupId>
    <artifactId>project</artifactId>
    <version>1.0.0</version>
    <relativePath>../</relativePath>
  </parent>
  
  <artifactId>project-config-provider</artifactId>
  <name>Project Config Provider</name>
  
  <dependencies>
     
    <!-- spring cloud config -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</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-bootstrap</artifactId>
    </dependency>
    
    <!-- spring security -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

  </dependencies>
  
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
    
</project>

application_config-provider--native.yaml:

spring:
   application:
      name: config-provider
   profiles:
      active: native
   cloud:
      config:
         server:
            native:
               search-locations: cloud_config
            prefix: /config
            bootstrap: true

server.port: 8888

现在我可以看到以下属性:

http://localhost:8888/config/application-config_storage/env_local

© www.soinside.com 2019 - 2024. All rights reserved.