spring cloud 之 config-client

来源:互联网 发布:软件风险分析 编辑:程序博客网 时间:2024/05/18 20:07

老规矩 直接上代码

1.pom.xml

<?xml version="1.0"?><project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  <modelVersion>4.0.0</modelVersion>  <parent>    <groupId>com.djl.springcloud</groupId>    <artifactId>spring-cloud-demo</artifactId>    <version>0.0.1-SNAPSHOT</version>  </parent>  <artifactId>config-client</artifactId>  <name>config-client</name>  <url>http://maven.apache.org</url>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-config-client</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-actuator</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>    </dependencies>    <repositories>        <repository>            <id>spring-milestones</id>            <name>Spring Milestones</name>            <url>http://repo.spring.io/milestone</url>            <snapshots>                <enabled>false</enabled>            </snapshots>        </repository>    </repositories>    <pluginRepositories>        <pluginRepository>            <id>spring-milestones</id>            <name>Spring Milestones</name>            <url>http://repo.spring.io/milestone</url>            <snapshots>                <enabled>false</enabled>            </snapshots>        </pluginRepository>    </pluginRepositories>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>
2./config-client/src/main/resources/bootstrap.properties


spring.application.name=foo spring.cloud.config.profile=testspring.cloud.config.label=master spring.cloud.config.uri=http://localhost:8888/

注意 这里我配置的 profile 是test 

3.client代码

package com.djl.springcloud.config.client;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication@RestControllerpublic class Application {    @Value("${name}")    String bar;    @RequestMapping("/")    String hello() {        return "Hello " + bar + "!";    }    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}

上面 bar 的是通过读取配置文件注入进来的 我们看结果

4.访问:http://localhost:8080/


5.对比git上的文件


那其他环境的呢~~ 咱们随便看个其他配置


总之每个环境的配置是不一样的,应用程序会给根据你的配置来读取~~~

原创粉丝点击