SpringCloud Netflix (六):Config 配置中心

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

SpringCloud Netflix (六):Config 配置中心

小高飞   2020-06-02 我要评论

------------恢复内容开始------------

 

SpringCloud Config 配置中心

Config 配置中心

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

 

Config配置中心实例

Config-Server

1.新建一个配置中心服务 springcloud-config-6001,并添加依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
        <version>2.1.0.RELEASE</version>
    <https://img.qb5200.com/download-x/dependency>
    <!--配置中心服务需要添加进注册中心,所以要加eureka-client依赖-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>1.4.6.RELEASE</version>
    <https://img.qb5200.com/download-x/dependency>
<https://img.qb5200.com/download-x/dependencies>

2.创建配置文件 application.yml

server:
  port: 6001

spring:
  application:
    name: springcloud-config
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/little_gofy/config-server.git #git仓库的html地址
          #如果配置文件放在子目类下,则需在search-paths配置添加子目录路径
          search-paths:
          #如果仓库为私有,则需在username和password配置添加用户名和密码
          username:
          password: 

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/

3.在启动类上添加注解@EnableConfigServer,允许配置服务

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

4.在git仓库创建配置文件application.yml,随便添加点配置

spring:
  profiles: dev
  application:
    name: config-dev
my:
  name: gofy-dev
    
---
spring:
  profiles: test
  application:
    name: config-test
my:
  name: gofy-test

开启注册中心服务和配置中心服务, 访问 localhost:6001/application-dev.yml 和 localhost:6001/application-test.yml, 返回git仓库配置文件对应模式的配置.

HTTP服务具有以下形式的资源:

# application:配置文件名, profile:配置的模式, label:配置文件所在分支
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

如果报以下异常, 把 spring-cloud-config-server 降到 2.1.0.RELEASE 即可

NoClassDefFoundError: org/springframework/cloud/config/environment/PropertyValueDescriptor

Config-Client

1.创建一个客户端 springcloud-config-client, 并添加依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
        <version>2.0.0.RELEASE</version>
    <https://img.qb5200.com/download-x/dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>1.4.6.RELEASE</version>
    <https://img.qb5200.com/download-x/dependency>
<https://img.qb5200.com/download-x/dependencies>

2.创建 bootstrap.yml

spring:
  cloud:
    config:
      discovery:
        service-id: springcloud-config #指定配置中心,对应配置中心服务名
        enabled: true #开启配置信息发现
      label: master #配置文件所在分支, 默认为master
      profile: dev #配置的模式, 多个用逗号分隔

eureka:
  client:
    service-url:
      defaultZone: http://server1:7001/eureka/
    register-with-eureka: false

再创建一个 application.yml

server:
  port: 80

spring:
  application:
    name: config-client

注意, 不要把bootstrap.yml的配置写在application.yml里, 因为bootstrap.yml的相关配置会先于application.yml,而bootstrap.yml的加载也是先于application.yml。需要注意的是eureka.client.serviceUrl.defaultZone要配置在bootstrap.yml,不然客户端是无法获取配置中心参数的,会启动失败!

3.创建Controller调用配置中心的配置

@RestController
public class ConfigController {
    @Value("${my.name}") //注入配置中心里的配置文件的my.name值
    private String myName;

    @RequestMapping("/my")
    public String getMyName(){
        return myName;
    }
}

4.创建启动类

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

启动注册中心、配置中心服务和客户端, 访问 localhost/my , 返回的是dev模式配置的值gofy-dev .

当我们把bootstrap.yml里的profile值改为test, 则返回的是test模式配置的值gofy-test .

如果报以下异常, 是因为配置中心服务还没有注册到注册中心, 等一会再启动客户端就行了. 如果还报错, 请检查配置是否和上面的配置一致.

java.lang.IllegalStateException: No instances found of configserver (springcloud-config)

 

修改Config-Server本地仓库位置

使用基于VCS的后端(git,svn),文件被检出并克隆到本地文件系统。默认情况下,它们以config-repo-为前缀放在系统临时目录中。例如,在Linux上,它可能是/tmp/config-repo-,在Window上,它是/Temp/config-repo-。一些操作系统通常会清除临时目录。这可能导致意外行为,例如缺少属性。

为避免此问题,请通过将spring.cloud.config.server.git.basedirspring.cloud.config.server.svn.basedir设置为不在系统临时结构中的目录来更改Config Server使用的目录。

spring:
  cloud:
    config:
      server:
        git:
          basedir: E:/local-config-repo

 

我的个人博客站

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们