【问题标题】:How to externalise spring application configuration using config server?如何使用配置服务器外部化 Spring 应用程序配置?
【发布时间】:2017-03-23 07:06:40
【问题描述】:

我编写了自己的配置服务器来集中配置管理并使用 API 公开应用程序配置。现在我想使用 spring 和 spring-boot 应用程序的配置属性。但我无法找出正确的方法。我尝试放置我的配置服务器客户端代码来监听应用程序上下文启动事件并从配置服务器读取配置。但我无法将此配置注入其他 bean。

如何从配置服务器(使用 rest 客户端)读取应用程序配置并将读取的配置注入应用程序环境以进行进一步处理?

【问题讨论】:

    标签: spring spring-boot config


    【解决方案1】:

    为配置和您的应用程序创建一个应用程序

    @SpringBootApplication
    @EnableConfigServer
    public class ConfigApplication  {
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(ConfigApplication.class, args);
        }
    }
    

    配置服务器中的application.yml

    spring:
      cloud:
        config:
          server:
            native:
              search-locations: classpath:/shared
      profiles:
         active: native
    
    
    security:
      user:
        password: pass
    
    server:
      port: 8888
    

    并像这样在 src/main/resources/shared 和 your-service.yml 中创建共享文件夹

    spring:
        security:
            basic:
                enabled: false
    
    server:
        port: 8083
    

    配置服务器中的maven pom.xml

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.1.RELEASE</version>
        </parent>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Camden.SR5</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-server</artifactId>
            </dependency>
            <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>
                    <configuration>
                        <finalName>config</finalName>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    

    在你的服务spring boot应用中添加/src/main/resources/bootstrap.yml

    喜欢这个

    spring:
      application:
        name: your-service
      cloud:
        config:
          uri: http://localhost:8888
          fail-fast: true
          username: user
          password: pass
    

    并将此依赖项添加到您的服务中

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

    首先运行配置服务器,然后运行你的 spring boot 服务

    【讨论】:

    • 嗨,阿里,我没有使用 spring-cloud-config 服务器,但我有自己的配置服务器和配置客户端。
    • 为什么不使用云配置?你想写一个像云配置这样的项目:D
    • 很少有要求需要我编写自己的配置服务,当然我不喜欢重新发明轮子,但是是的,这让我可以更好地控制数据模型、代码和功能。
    猜你喜欢
    • 2021-12-16
    • 2021-11-10
    • 2015-05-20
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多