码迷,mamicode.com
首页 > 编程语言 > 详细

(二)springcloud 集中配置-spring cloud config

时间:2019-04-22 16:29:01      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:配置文件   location   ota   art   超时   trap   post   point   class   

setup a Config Server and then build a client that consumes the configuration on startup and then refreshes the configuration without restarting the client.

config server

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>

    <groupId>com.example</groupId>
    <artifactId>configuration-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

启动:

package hello;

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

// use @EnableConfigServer to standup a config server that other applications can talk to
@EnableConfigServer
@SpringBootApplication
public class ConfigServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServiceApplication.class, args);
    }
}

配置:

server:
  port: 8888
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        # 也可以指向本地库:file://${user.home}/config-repo
        git:
          uri: https://gitee.com/lostNfound/spring-cloud-config.git
          username: 18507149951
          password: xxxx
          searchPaths: /**
          default-label: master
        # 也可以使用本地配置
        # native:
          # search-locations: classpath:config/*

配置管理:EnvironmentRepository

Environment资源由三个变量确定

  • {application}映射到客户端的“spring.application.name”;
  • {profile}映射到客户端上的“spring.profiles.active”(逗号分隔列表); 和
  • {label}这是一个服务器端功能,标记“版本”的一组配置文件。

测试:

1、git仓库中创建配置文件:config-client-example-dev.yml

value: config-client-example-value

2、启动集中配置,访问:http://localhost:8888/config-client-dev.yml

/**
 * 配置访问方式:
 * /{application}/{profile}[/{label}]
 * /{application}-{profile}.yml
 * /{label}/{application}-{profile}.yml
 * /{application}-{profile}.properties
 * /{label}/{application}-{profile}.properties
 */

config client

<?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.example</groupId>
    <artifactId>configuration-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

配置(bootstrap.yml):

server:
  port: 4001

spring:
  application:
    # 指定服务名称
    name: config-client-example
  profiles:
    active: dev

  cloud:
    config:
        # 指定label
      label: master
      # 指定服务名称
      profile: ${spring.profiles.active}
      # 指定集中配置服务名称和是否开启
      discovery:
        service-id: config-server
        enabled: true

# 开放监控端点,动态刷新配置
management:
  endpoints:
    web:
      exposure:
        include: "*"

启动类:

@SpringBootApplication
public class ConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
  
    /**
     * 验证从配置中心获取配置
     */
    @RefreshScope
    @RestController
    @RequestMapping("/config")
    public class ConfigController {

        @Value("${value}")
        private String value;

        @GetMapping("/param")
        public String param() {
            return value;
        }
    }
  
}

启动验证:

启动并访问:http://localhost:4001/config/param

响应结果:config-client-example-value

修改配置文件:

value: config-client-example-value edit

向客户端发送请求,到集中配置抓取最新配置

POST /actuator/refresh HTTP/1.1
Content-Length: 0
Host: localhost:4001
Content-Type: application/json

HTTP/1.1 200
Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Mon, 22 Apr 2019 05:12:24 GMT

["config.client.version","value"]

再次访问:http://localhost:4001/config/param

响应结果:config-client-example-value edit

详细配置

spring:
    cloud:
        config:
            # 找不到配置停止启动并显示异常
            fail-fast: true
            # 配置读取超时
            request-read-timeout: 1s
            server:
                git:
                    # 指定git仓库地址
                    url: https://gitee.com/lostNfound/spring-cloud-config.git
                    # 可以使用占位符来支付每一个服务一个配置库
                    # url: https://gitee.com/lostNfound/{application}.git
                    skipSslValidation: true
                    timeout: 4
                    username: xxx
                    password: xxx

除了git,文件系统外,集中配置还支持 Vault Backend, JDBC Backend, CredHub Backend。不写了

(二)springcloud 集中配置-spring cloud config

标签:配置文件   location   ota   art   超时   trap   post   point   class   

原文地址:https://www.cnblogs.com/zuier/p/10750857.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!