SpringCloud微服务实战-SpringCloudConfig配置中心

SpringCloud微服务实战-SpringCloudConfig配置中心

  • Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。
  • 客户端和服务器上的概念都与Spring Environment和PropertySource抽象相同,因此它们非常适合Spring应用程序,但可以与任何语言运行的应用程序一起使用。
  • 当应用程序通过从开发环境到测试环境和生产环境的部署管道时,您可以管理这些环境之间的配置,并确保应用程序在迁移时需要运行所需的一切。服务器存储后端的默认实现使用git,因此它可以轻松支持配置环境的标记版本,并且可以通过各种工具来访问内容。

10.1 基本使用

  • 创建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">
    <parent>
        <artifactId>spring-cloud-microservice-study</artifactId>
        <groupId>com.clsaa.learn.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>microservice-config-server</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>

</project>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • application.yml配置 
    • uri可以是一个git地址具体规则稍后介绍

server:
  port: 8080
spring:
  cloud:
    config:
      server:
        git:
          uri: https://git.coding.net/eggyer/learn-spring-cloud-config.git
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 编写启动类(@EnableConfigServer)
package com.clsaa.springcloud;

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

/**
 * Created by eggyer on 2017/3/13.
 */
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApp {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApp.class,args);
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 测试

我们可以通过http://localhost:8080/application-application.yml获取数据

10.2 应用和数据获取映射规则

  • application:spring.application.name
  • label:git上的分支名
  • profile:文件名
获取git上的资源信息遵循如下规则
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
  • 当我们用这种url路径访问时会得到json数据
{
    "name": "foobar-dev.yml",
    "profiles": [
        "master"
    ],
    "label": "master",
    "version": "5fd6ff5dc6ffab7b8aec5e4f6828218d5028f907",
    "state": null,
    "propertySources": [
        {
            "name": "https://git.coding.net/eggyer/learn-spring-cloud-config.git/application.yml",
            "source": {
                "document": "profile:profile-default"
            }
        }
    ]
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

10.3 SpringCloudConfig-Client

  • bootstrap.x里面的配置 —>链接config server 加载远程配置 —>加载application.x里的配置,所以我们要使用bootstrap.yml
  • 本地同名配置会被远程的覆盖
  • spring官方建议我们在bootstrap中放置不更改的属性.

  • bootstrap.yml

spring:
  cloud:
    config:
     uri: http://localhost:8080
     profile: dev #对应业务名称的profile
     label: master #当配置文件在git上时为分支名
  application:
    name: foobar  #对应文件的业务名称

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • application.yml
server:
  port: 8081
 
 
  • 1
  • 2
  • APP
package com.clsaa.springcloud.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Created by eggyer on 2017/3/13.
 */
@SpringBootApplication
public class ConfigClientApp {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApp.class,args);
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • controller
package com.clsaa.springcloud.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by eggyer on 2017/3/13.
 */

@RestController
public class ConfigClientController {
    //取配置文件中的值
    @Value("${profile}")
    private String profile;

    @GetMapping("/profile")
    public String getProfile(){
        return profile;
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

10.4 git仓库配置详解

  • 分别创建git仓库special和simple,在两个仓库下加入配置文件application.yml,内容分别为profile: special;profile: simple
  • 在configserver配置文件编写

  • 使用一个微服务一个配置

server:
  port: 8080
spring:
  cloud:
    config:
      server:
        git:
          uri: https://git.coding.net/eggyer/{application}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 访问http://localhost:8080/master/simple-default.yml显示:profile: simple
  • 可以发现我们可以用仓库名放入URL中(这样我们可以让每一个微服务单独使用一个git仓库,更容易进行权限管理)
  • 个人建议一个配置环境一个文件

使用一种环境一种配置

server:
  port: 8080
spring:
  cloud:
    config:
      server:
        git:
          uri: https://git.coding.net/eggyer/{application}-{profile}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

模式匹配

  • 在special下添加两个配置文件special-dev.yml,special-test.yml
server:
  port: 8080
spring:
  cloud:
    config:
      server:
        git:
          uri: https://git.coding.net/eggyer/learn-spring-cloud-config.git
          repos:
            simple: https://git.coding.net/eggyer/simple
            special:
              pattern: special*/dev*,*special*/test*    #/用来隔开application和environment(开发环境)
              uri: https://git.coding.net/eggyer/special
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

10.5 加解密

  • The server also exposes /encrypt and /decrypt endpoints (on the assumption that these will be secured and only accessed by authorized agents). If you are editing a remote config file you can use the Config Server to encrypt values by POSTing to the /encrypt endpoint, e.g.
$ curl localhost:8888/encrypt -d mysecret
682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda
The inverse operation is also available via /decrypt (provided the server is configured with a symmetric key or a full key pair):

$ curl localhost:8888/decrypt -d 682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda
mysecret
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • TIP

  • If you are testing like this with curl, then use –data-urlencode (instead of -d) or set an explicit Content-Type: text/plain to make sure curl encodes the data correctly when there are special characters (‘+’ is particularly tricky).

10.6 用户认证

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">
    <parent>
        <artifactId>spring-cloud-microservice-study</artifactId>
        <groupId>com.clsaa.learn.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>microservice-config-server</artifactId>
    <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>

</project>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

server:
  port: 8080
spring:
  cloud:
    config:
      server:
        git:
          uri: https://git.coding.net/eggyer/learn-spring-cloud-config.git
security:
  basic:
    enabled: true
  user:
    name: user
    password: password
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

client部分

  • 在bootstrap文件中加入 
    • 在uri下可以加入username和password属性,优先级比uri中高
    • 那么为什么springcloud会提供两种方式?
    • 生产环境中configserver需要高可用(可能有多个) 
      • 上面方式中如果需要负载均衡需要部署nginx等组件
      • 节点信息被硬编码在配置文件中
      • 没有充分发挥服务发现的优势所在
    • 那么我们需要让configclient也具有服务发现的功能,让其通过eureka自动的寻找到configserver
spring:
  cloud:
    config:
     uri: http://user:password@localhost:8080
     profile: dev #对应业务名称的profile
     label: master #当配置文件在git上时为分支名
  application:
    name: foobar  #对应文件的业务名称

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

10.7 通过Eureka发现configserver

  • 添加pom文件
    <!-- 添加Eureka的依赖 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5

配置服务与注册中心联合使用

在生产环境中,我们可能会将Config Server 与 Eureka等注册中心联合使用(注意:目前Spring Cloud只支持与Eureka及Consul联合使用,不支持与Zookeeper联合使用),下面讲解如何将Config Server与 Eureka 联合使用。

准备工作

  1. 启动服务microservice-discovery-eureka ;
  2. 和上文一样,准备好几个配置文件,命名规范为项目名称-环境名称.properties ,本文使用的名称是microservice-config-client-eureka-dev.properties 。

代码示例

服务器端代码示例:

首先新建一个Maven项目,在pom.xml 中添加如下内容:

<?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>

  <artifactId>microservice-config-server-eureka</artifactId>
  <packaging>jar</packaging>

  <parent>
    <groupId>com.itmuch.cloud</groupId>
    <artifactId>spring-cloud-microservice-study</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>

  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
  </dependencies>
</project>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

启动类:ConfigServerEurekaApplication.java

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerEurekaApplication {
  public static void main(String[] args) {
    SpringApplication.run(ConfigServerEurekaApplication.class, args);
  }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

配置文件:application.yml

server:
  port: 8050
spring:
  application:
    name: microservice-config-server-eureka
  cloud:
    config:
      server:
        git:
          uri: https://github.com/eacdy/spring-cloud-study/
          search-paths: config-repo
          username: 
          password: 
eureka:
  client:
    serviceUrl:
      defaultZone: http://discovery:8761/eureka/
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
客户端示例

创建一个Maven项目,在pom.xml 中添加如下内容:

<?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>

  <artifactId>microservice-config-client-eureka</artifactId>
  <packaging>jar</packaging>

  <parent>
    <groupId>com.itmuch.cloud</groupId>
    <artifactId>spring-cloud-microservice-study</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </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.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
  </dependencies>
</project>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

启动类:ConfigClientEurekaApplication.java

@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientEurekaApplication {
  public static void main(String[] args) {
    SpringApplication.run(ConfigClientEurekaApplication.class, args);
  }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

编写测试Controller

/**
 * 这边的@RefreshScope注解不能少,否则即使调用/refresh,配置也不会刷新
 * @author eacdy
 */
@RestController
@RefreshScope
public class ConfigClientController {
  @Value("${profile}")
  private String profile;

  @GetMapping("/hello")
  public String hello() {
    return this.profile;
  }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

配置文件:application.yml

server:
  port: 8051
 
 
  • 1
  • 2

配置文件:bootstrap.yml

spring:
  application:
    name: microservice-config-client-eureka
  cloud:
    config:
      profile: dev
      label: master
      discovery:
        enabled: true                                 # 默认false,设为true表示使用注册中心中的configserver配置而不自己配置configserver的uri
        serviceId: microservice-config-server-eureka  # 指定config server在服务发现中的serviceId,默认为:configserver
eureka:
  client:
    serviceUrl:
      defaultZone: http://discovery:8761/eureka/

# 参考文档:https://github.com/spring-cloud/spring-cloud-config/blob/master/docs/src/main/asciidoc/spring-cloud-config.adoc#discovery-first-bootstrap
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

从示例代码我们发现,想要将Config Server 与 注册中心联合使用,只需要在客户端侧配置spring.cloud.config.discovery.enabled=true 和 spring.cloud.config.discovery.serviceId 两个配置项即可。Eureka的配置前文有讲到过,如有疑问,详见服务发现的相关章节。

注意:当服务发现是Eureka 及 Consul 时,Config Server支持与之联合使用;如果是Zookeeper 做服务发现,目前不支持与之联合使用。

注意点:

  • client需要添加以下依赖,否则访问/refresh将会得到404:
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
 
 
  • 1
  • 2
  • 3
  • 4
  • client的controller需要添加@RefreshScope注解,否则配置无法刷新。
  • 本文的bootstrap.yml文件中的内容不能放到application.yml中,否则config部分无法被加载,因为config部分的配置先于application.yml被加载,而bootstrap.yml中的配置会先于application.yml加载,
  • Config Server也可以支持本地存储或svn而不使用git,相对较为简单,故而本文不作赘述,有兴趣的可以自行阅读Spring Cloud的文档。

参考文档:

Config Server与注册中心联合使用:https://github.com/spring-cloud/spring-cloud-config/blob/master/docs/src/main/asciidoc/spring-cloud-config.adoc#discovery-first-bootstrap

Config Server的高可用: https://github.com/spring-cloud/spring-cloud-config/issues/87

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值