spring cloud 之Spring Cloud Config 尝鲜

准备工作

  • 创建一个本地git库
  • 创建一个spring cloud server项目
  • 创建一个spring cloud client项目
  • 基本注意事项

创建本地git仓库

$ cd ~

$ mkdir springCloud

$ cd springCloud/

$ git init .

$ echo info.foo: bar >lesson-dev.properties

$ git add -A .

$ git commit -m “init”


创建一个spring cloud server项目

  • 项目结构

    这里写图片描述

  • pom文件内容
<?xml version="1.0" encoding="UTF-8"?>
<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>
    <groupId>com.lesson</groupId>
    <artifactId>configServer</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
        <relativePath />
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
              <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <build>
        <plugins>
            <plugin>
               <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
  • 启动类ConfigServerController内容
package init;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.stereotype.Controller;

/**
 * Created by lesson on 2017/9/27.
 */
@SpringBootApplication
@Controller
@EnableConfigServer
public class ConfigServerController {

    public static void main(String[] args) throws Exception {
       SpringApplication.run(ConfigServerController.class, args);
    }
}
  • 配置文件 application.properties
spring.cloud.config.server.git.uri=file://${user.home}/springCloud
management.security.enabled=false
server.port=8888
  • 启动 config-server

    运行ConfigServerController main方法


创建spring cloud client

  • 项目结构图

    这里写图片描述

  • pom文件内容

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <groupId>com.lesson</groupId>
    <artifactId>configClient</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
        <relativePath />
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
             <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>    
  • HelloController内容
package init;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by lesson on 2017/9/27.
 */
@SpringBootApplication
@Controller
@RefreshScope
public class HelloController {

    @Value("${foo}")
    private String name;

    @RequestMapping("getFoo")
    @ResponseBody
    public String getFoo(){
        return name;
    }



    public static void main(String[] args) throws Exception {
       SpringApplication.run(HelloController.class, args);
    }
}
  • application.properties文件
server.port=4000
server.context-path=/


spring.application.name=lesson
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:8888/
spring.cloud.config.failFast=true
management.security.enabled=false
  • 启动HelloController

  • postman 测试url
    这里写图片描述

  • 修改git仓库配置文件内容
    这里写图片描述

  • 调用config client项目的刷新接口 (config server可以自动感知git仓库变化)
    这里写图片描述

  • 重新调用config client项目测试链接
    这里写图片描述

    可以看到响应内容已经改为我们修改后的content了


基本注意事项

  • git仓库中文件命名
    这里写图片描述
    所以 lesson-dev.properties 的命名规则
    lesson取自cloud client配置文件中的 spring.application.name
    dev 取自 cloud client 配置文件中的 spring.cloud.config.profile

  • cloud client 要求配置文件更新自动刷新类中的响应属性的类上要配上@RefreshScope注解 如下
    这里写图片描述

    原因摘自 spring cloud
    这里写图片描述

  • cloud client项目的pom文件中 要引入一下内容

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

否则 无法调用http://localhost:4000/refresh

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值