• spring cloud config


    服务端

    创建spring boot 工程,命名为config-server,在pom中添加引用

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.9.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-server</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    View Code

    在程序入口类添加@EnableConfigServer注解

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.config.server.EnableConfigServer;
    
    @EnableConfigServer
    @SpringBootApplication
    public class ConfigServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigServerApplication.class, args);
        }
    }
    View Code

    在application.properties中添加配置

    spring.application.name=config-server
    server.port=7001
    
    spring.cloud.config.server.git.uri=https://github.com/m13810859213/SpringCloud-Learning
    spring.cloud.config.server.git.search-paths=config
    spring.cloud.config.server.git.username=xxxx
    spring.cloud.config.server.git.password=xxxx
    View Code

    在github新建项目https://github.com/m13810859213/SpringCloud-Learning

    目录下添加一个config目录,在其中添加几个配置文件

    运行config-server,打开http://localhost:7001/configClient-dev.properties

    可以看到已经连接到了github上并读取了配置,这里的URL和配置文件的映射关系如下:

    • /{appliaction}/{profile}[/{label}]
    • /{application}-{profile}.yml
    • /{label}/{application}-{profile}.yml
    • /{application}-{profile}.properties
    • /{label}/{application}-{profile}.properties

    {label}对应Git上的不同分支,不指定默认为master

    客户端

    创建config-client的spring boot应用,在pom中添加引用

    <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    View Code

    添加一个bootstrap.properties配置文件

    spring.application.name=configClient
    spring.cloud.config.profile=dev
    spring.cloud.config.label=master
    spring.cloud.config.uri=http://localhost:7001
    
    server.port=7002
    View Code

    这里的配置与Git中的文件相对应

    创建一个TestController

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.core.env.Environment;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RefreshScope
    @RestController
    public class TestController {
        @Autowired
        private Environment env;
        @RequestMapping(value = "/from",method = RequestMethod.GET)
        public String from(){
            return env.getProperty("from","undefiend");
        }
    }
    View Code

    运行程序,打开http://localhost:7002/from

  • 相关阅读:
    教程-Delphi中比较两个对象是否一致及地址是否相同
    Android问题-“signaturs do not match the previously installed version”
    Android实例-红外线操作(XE10.2+小米5)
    JAVA-JAVA WEB开发工具下载与安装
    问题-DelphiXE10.2怎么安装文本转语音(TTS)语音转文本(SR)控件(XE10.2+WIN764)
    Android实例-ImageList与Image的应用
    多国语言解决方案gnu.gettext + poedit
    Unity-Animator在Editor状态下的单个/批量预览工具
    Lambda编写斐波那契数列
    Unity-Animator深入系列---API详解
  • 原文地址:https://www.cnblogs.com/uptothesky/p/8286832.html
Copyright © 2020-2023  润新知