二、Spring Cloud 配置中心

基于:一、SpringCloud 服务注册与发现

Spring Cloud Config 能做什么?

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

怎样定位资源:

定位资源的默认策略是克隆一个git仓库(在spring.cloud.config.server.git.uri),并使用它来初始化一个迷你SpringApplication。迷你应用程序的Environment用于枚举属性源并通过JSON端点发布。

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

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties</span></span>

其中“应用程序”作为SpringApplication中的spring.config.name注入(即常规Spring Boot应用程序中通常为“应用程序”),“配置文件”是活动配置文件(或逗号分隔列表)的属性),“label”是可选的git标签(默认为“master”)。

Spring Cloud Config服务器从git存储库中提取远程客户端的配置(必须提供):

<span style="color:rgba(0, 0, 0, 0.8)"><span style="color:rgba(0, 0, 0, 0.9)"><code class="language-yaml">spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo</code></span></span>

 一、创建配置中心服务端:

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>springcloud-example01</artifactId>
        <groupId>com.xslde</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-server</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--客户端依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--配置中心服务端依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>

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

2、创建一个启动类,并启用配置中心服务端注解 @EnableConfigServer

package com.xslde;

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

/**
 * Created by xslde on 2018/7/22
 */
@EnableConfigServer //启用配置中心服务端
@SpringBootApplication
public class ConfigApplication {

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

}

3、创建git仓库,github或码云,或者自己搭建git服务:

git仓库创建完成后,写一个eureka-client-dev.yml文件,上传到git仓库

xslde:
  name: 123456abcd
server:
  port: 7101 #启动端口号
spring:
  application:
    #配置应用名称,后期可根据应用名称调用应用的应服务
    name: eureka-client

4、在resources文件夹下创建:bootstrap.yml,将上面git仓库地址贴到下面server:git:uri后面

server:
  port: 7102 #启动端口号
spring:
  application:
    #配置应用名称,后期可根据应用名称调用应用的应服务
    name: config-server
  cloud:
    config:
      server:
        git:
        #https://gitee.com/xslde/springcloud-config.git,.git 可带上或不带
          uri: https://gitee.com/xslde/springcloud-config
        #如果gig仓库需要账户密码认证,启用下面这两项就ok
        #  username:
        #  password:
eureka:
  instance:
      hostname: localhost
  client:
    service-url:
      #向服务中心注册服务,xslde:123456是用户名称和密码
      defaultZone: http://xslde:123456@${eureka.instance.hostname}:7100/eureka/

5、启动eureka-server服务

6、启动刚刚创建好的config-server服务

7、浏览器访问:http://localhost:7100,如下图,说明服务启动成功:

 

8、config-server服务启动端口是7102,在浏览器中输入http://localhost:7102/eureka-client-dev.yml看是否能成功读取到git仓库中的文件。读取成功如下:

 二、改变eureka-client工程为配置中心客户端:

1、启动类改动:

package com.xslde;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by xslde on 2018/7/21
 */
@RestController
@EnableEurekaClient //启用服务
@SpringBootApplication
public class EurekaClientApplication {

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

    //这个值是从git仓库的配置文件中读取
    @Value("${xslde.name}")
    private String name;

    @GetMapping("/name")
    public String test(){
        return "您配置文件中name值为:"+name;
    }



}

 2、bootstrap.yml改动(去掉端口号,在git仓库中eureka-client-dev.yml中配置端口号,配置,配置中心客户端读取配置文件路径):

spring:
  cloud:
    config:
      discovery:
        enabled: true
        service-id: config-server #根据eureka注册中心里的服务名称找到配置中心服务器
        #eureka-client-dev.yml
      name: eureka-client
      profile: dev
eureka:
  instance:
      hostname: localhost
  client:
    service-url:
      #向服务中心注册服务,xslde:123456是用户名称和密码
      defaultZone: http://xslde:123456@${eureka.instance.hostname}:7100/eureka/

3、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>springcloud-example01</artifactId>
        <groupId>com.xslde</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-client</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--客户端依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--配置中心客户端依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
    </dependencies>


</project>

4、启动eureka-client项目(正常启动如下):

5、启动完成后通过浏览器访问:http://localhost:7101/name

 

到此配置中心服务端和客户端配置完成,已经可以在真实开发中使用。

项目地址:springcloud-example02 

猜你喜欢

转载自blog.csdn.net/xslde_com/article/details/81151632
今日推荐