Spring Cloud_31_SpringCloud配置中心/例子

SpringCloud配置中心/例子

  • Config的作用:对集群配置的统一管理
  • Config的作用:对集群配置的统一管理
  • 客户端连接配置服务器的时候默认连接8888端口

1、创建Config服务器

  • 创建Config服务器之前,需要先创建SVN服务器,上一章节已经创建完成
  • 现在来创建Config服务器

1.1、引入依赖

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-dependenciesartifactId>
            <version>Dalston.SR3version>
            <type>pomtype>
            <scope>importscope>
        dependency>
    dependencies>
dependencyManagement>


<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-config-serverartifactId>
dependency>


<dependency>
    <groupId>org.tmatesoft.svnkitgroupId>
    <artifactId>svnkitartifactId>
    <version>1.9.0version>
dependency>

1.2、配置SVN仓库applicaiton.yml

  • SpringCloud默认使用Git仓库,如果SVN和GIT仓库都没有配置,则会报错

  • 配置profile
    1. git:默认值,表示去Git仓库读取配置文件
    2. subversion:表示去SVN仓库读取配置文件
    3. native:将会去本地的文件系统中读取配置文件
    4. vault:去Vault中读取配置文件,Vault是一款资源控制工具,可对资源实现安全访问
server:
  port: 8888
##为了方便测试,关闭安全管理机制
management:
  security:
    enabled: false
##配置SVN仓库
spring:
  profiles:
    active: subversion
  cloud:
    config:
      server:
        svn:
          uri: https://USER-20170523PW/svn/my-aitemi/
          username: aitemi
          password: aitemi
        ##默认到SVN根目录下的trunk目录中读取配置文件
        default-label: aitemi

1.3、启动类

package com.atm.cloud;

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

@SpringBootApplication
@EnableConfigServer//开启服务器
public class ConfigServerApp {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApp.class, args);
    }
}
  • SVN目录中创建文件,注意创建的文件命名规则,本人在测试时使用first.yml无法访问,命名为first-test.yml则可以访问

  • 浏览器访问http://127.0.0.1:8888/first-test.yml

2、创建Config客户端

2.1、引入依赖

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-dependenciesartifactId>
            <version>Dalston.SR3version>
            <type>pomtype>
            <scope>importscope>
        dependency>
    dependencies>
dependencyManagement>

<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-configartifactId>
dependency>
<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
    <version>1.5.4.RELEASEversion>
dependency>

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-actuatorartifactId>
    <version>1.5.4.RELEASEversion>
dependency>

2.2、配置文件bootstrap.yml

spring:
  ## 配置了spring.application.name,和cloud.config.profile,则读取atm-config-client-dev.yml
  application:
    name: atm-config-client
  cloud:
    config:
      url: http://localhost:8888
      ## 读取的文件是 atm-config-client-dev.yml
      profile: dev
spring:
  cloud:
    config:
      url: http://localhost:8888
      ## 没有配置spring.applicaiton.name,但是配置了spring.cloud.config.name,则读取文件不变,依旧是atm-config-client-dev.yml
      profile: dev
      name: atm-config-client
spring:
  cloud:
    config:
      url: http://localhost:8888
      ## 两者均没有配置,只配置了spring.cloud.profile,默认读取application-dev.yml
      profile: dev

2.3、ReadConfigController

package com.atm.cloud;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 读取远程的配置
 */
@RestController
public class ReadConfigController {

    @Autowired
    private Environment environment;

    @GetMapping("/read")
    public String readConfig() {
        return environment.getProperty("test.user.name");
    }
}

3、目录配置读取总结

  • 读取总结(服务器配置目录,客户端配置读取哪份文件)

    1. 服务器:spring.cloud.config.server.svn.uri=https://USER-20170523PW/svn/my-aitemi/,配置你需要连接的SVN
    2. 服务器:spring.cloud.config.server.default-label=aitemi,到SVN下的指定目录中读取
    3. 客户端:spring.application.name=atm-config-client,读取的配置文件的名称,
    4. 客户端:spring.cloud.config.profile=dev
  • 读取多分配置文件:spring.cloud.config.profile=hystrix,zuul

  • 客户端配置目录:spring.cloud.config.label,可以配置客户端读取配置文件的目录,会覆盖服务器所配置的default-label
spring:
  cloud:
    config:
      label: aitemi2

4、刷新配置

  • 访问/refresh端点
## 为了方便测试,关闭安全管理
management:
  security:
    enabled: false
  • 模拟POST请求
package com.atm.cloud;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class RefreshClientMain {

    public static void main(String[] args) throws Exception {
        CloseableHttpClient client = HttpClients.createDefault();
        // 发送post请求
        HttpPost post = new HttpPost("http://localhost:8080/refresh");
        HttpResponse response = client.execute(post);
        System.out.println(EntityUtils.toString(response.getEntity()));
    }
}

5、刷新Bean

  • 在实际应用中,在刷新配置的情况下,还需要刷新对应的bean
package com.atm.cloud;

import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration
public class MyConfig {

    @Bean
    @RefreshScope
    public Person person(Environment env) {
        // 读取名字创建Person
        String name = env.getProperty("test.user.name");
        // 输出Person名字
        System.out.println("初始化Person bean:" + name);
        // 创建一个Bean
        Person p = new Person();
        p.setName(name);
        return p;
    }
}
@Autowired
private Person person;

@GetMapping("/personName")
public String readPersonName() {
    return person.getName();
}

你可能感兴趣的:(SpringCloud,SpringCloud)