1. 建立空的子项目 springcloud-config-server

2. 修改 pom.xml

增加

    <dependencies>
        <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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.xnx3.springcloud</groupId>
    <artifactId>springcloud-main</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <groupId>com.xnx3.springcloud.config.client.test</groupId>
  <artifactId>springcloud-config-client-test</artifactId>

	<!-- 以下是增加的 -->
    <dependencies>
        <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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
	<!-- 以上是增加的 -->

</project>

3. 添加启动类 com.xnx3.springcloud.config.client.test.ConfigClientApplication

package com.xnx3.springcloud.config.client.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

4. 添加类 com.xnx3.springcloud.config.client.test.config.AuthorConfig

会将 git 远程配置文件 author.properties 的配置信息自动加载到此类。使用时,直接取

package com.xnx3.springcloud.config.client.test.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "com.xnx3.author")
public class AuthorConfig {
	private String name;
	private Integer age;
	private String site;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getSite() {
		return site;
	}
	public void setSite(String site) {
		this.site = site;
	}

	@Override
	public String toString() {
		return "AuthorConfig [name=" + name + ", age=" + age + ", site=" + site
				+ "]";
	}
}

5. 添加测试的控制器 com.xnx3.springcloud.config.client.test.controller.ConfigTestController

package com.xnx3.springcloud.config.client.test.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xnx3.springcloud.config.client.test.config.AuthorConfig;

@Controller
@RequestMapping("config")
public class ConfigTestController {

    @Autowired
    private AuthorConfig authorConfig;

    @RequestMapping("/getAuthor")
    @ResponseBody
    public String getAuthorInfo(){
        return " author信息是丛git上加载下来的 :" + authorConfig.toString();
    }
}

6. 启动

。。。

05-23 17:49