您的位置:首页 > 编程语言 > Java开发

Spring Cloud构建微服务架构(一)分布式配置中心

2017-07-26 15:55 1096 查看
 

功能:

spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持。配置服务器为各应用的所有环境提供了一个中心化的外部配置。它实现了对服务端和客户端对Spring Environment和PropertySource抽象的映射,所以它除了适用于Spring构建的应用程序,也可以在任何其他语言运行的应用程序中使用。作为一个应用可以通过部署管道来进行测试或者投入生产,我们可以分别为这些环境创建配置,并且在需要迁移环境的时候获取对应环境的配置来运行配置服务器默认采用Git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。当然他也提供本地化文件系
4000
统的存储方式,下面从这两方面介绍如何使用分布式配置来存储微服务应用多环境的配置内容。

构建Config Server

通过Spring Cloud构建一个Config Server,非常简单,只需要三步:

1、pom.xml中引入spring-cloud-config-server依赖,完整依赖配置如下:

<parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>1.5.1.RELEASE</version>    <relativePath/> <!-- lookup parent from repository --></parent><dependencies>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-test</artifactId>        <scope>test</scope>    </dependency>    <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-config-server</artifactId>    </dependency></dependencies><dependencyManagement>    <dependencies>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-dependencies</artifactId>            <version>Brixton.RELEASE</version>            <type>pom</type>            <scope>import</scope>        </dependency>    </dependencies></dependencyManagement>


2、创建Spring Boot的程序主类,并添加@EnableConfigServer注解,开启Config Server

@EnableConfigServer@SpringBootApplicationpublic class Application {    public static void main(String[] args) {        new SpringApplicationBuilder(Application.class).web(true).run(args);    }}


3、application.properties中配置服务信息以及git信息,例如:

spring.application.name=serConfigserver.port=2227spring.cloud.config.server.git.uri=http://10.10.10.236/spring/cloud-config.gitspring.cloud.config.server.git.searchPaths=cloud-config-repo


spring.cloud.config.server.git.uri:配置git仓库位置,此处只需配置到工程名即可
spring.cloud.config.server.git.searchPaths:配置仓库路径下的相对搜索位置,可以配置多个
spring.cloud.config.server.git.username:访问git仓库的用户名
spring.cloud.config.server.git.password:访问git仓库的用户密码
 

说明:因为我的例子是http://10.10.10.236/spring/cloud-config工程,而此工程下cloud-config-repo文件夹下面已经存在文件,具体文件名如下图:



 

 我使用到的配置文件为lz-hr-aaa.properties和lz-hr-bbb.properties,  而这两个文件里面的内容从上到下分别是:

 




此时,配置中心的服务端已经配置完毕,我们只需要按照自己的固定的格式进行访问就行了,具体的访问路径由以下部分组成:

 http://ip:port//{application}/{profile}

说明:{application}-{profile}.properties是组成文件的名称,最后一个中横线之前的所有名字都叫{application},之后的部分都叫{profile}.  所有的配置文件最好用中横线(-)加以区分。

 

 

根据上面说的url组成规则,那么我的本地配置中心搭建完毕之后的路径应该是:http://localhost:2227/lz-hr/aaa,访问信息如下:

 


而如果我们想要看看lz-hr-bbb.properties文件,可以直接输入访问地址即可:http://localhost:2227/lz-hr/bbb,具体访问信息为:

 


 

至此,一个简单的配置中心配置完毕。

注意:

1、我们在实际开发的工程中,如果多个文件只是进行区分生产环境的话,即开发环境、测试环境等单一选项的话,各个配置里面的属性是可以一样的。

2、如果我们监控的多个文件是多选,即可以同时使用各个文件里面的属性的话,那么必须将各个文件里面的属性进行区分开。否则在下面的配置中心客户端程序中,想要具体调用某个属性,那就不可想象了。

 

构建Config Client

1、创建一个Spring Boot应用,在pom.xml中引入spring-cloud-starter-config依赖

完整依赖关系如下:

<parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>1.5.1.RELEASE</version>    <relativePath/> <!-- lookup parent from repository --></parent><properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <java.version>1.7</java.version>    </properties><dependencies>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-test</artifactId>        <scope>test</scope>    </dependency>    <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></dependencies><dependencyManagement>    <dependencies>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-dependencies</artifactId>            <version>Brixton.RELEASE</version>            <type>pom</type>            <scope>import</scope>        </dependency>    </dependencies></dependencyManagement>


 

2、创建最基本的Spring Boot启动主类

package com.didispace;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.netflix.feign.EnableFeignClients;import org.springframework.context.annotation.Bean;import org.springframework.core.env.Environment;import com.didispace.entity.Us;@EnableDiscoveryClient@SpringBootApplicationpublic class ServiceC {     public static void main(String[] args) throws Exception    {        SpringApplication.run(ServiceC.class, args);    }     }


3、创建bootstrap.properties配置,来指定config server

server.port=2223 spring.cloud.config.hostname=127.0.0.1spring.cloud.config.uri=http://${spring.cloud.config.hostname}:${config.port:2227}spring.cloud.config.name=lz-hrspring.cloud.config.profile=${config.profile:aaa},${config.profile:bbb}


此处配置了多文件,分别是lz-hr-aaa.properties和lz-hr-bbb.properties

· spring.application.name:对应前配置文件中的{application}部分

· spring.cloud.config.profile:对应前配置文件中的{profile}部分

· spring.cloud.config.label:对应前配置文件的git分支

· spring.cloud.config.uri:配置中心的地址

这里需要格外注意:上面这些属性必须配置在bootstrap.properties中,config部分内容才能被正确加载。因为config的相关配置会先于application.properties,而bootstrap.properties的加载也是先于application.properties。

 

程序调用:

package com.didispace.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.context.annotation.Configuration;import org.springframework.core.env.Environment;import com.didispace.entity.Us;import com.didispace.service.IUserService;@RefreshScope@EnableConfigurationProperties(Us.class)@RestControllerpublic class Controller {        //第一种调用方式    @Autowired    private Environment env;        //第二种调用方式    @Value("${from}")    private String from;         //第三种调用方式    @Autowired    private Us u;        @RequestMapping("/from")    public String getMsg2(){        System.out.println("-------------from2:" + env.getProperty("profiles"));                System.out.println("-------------from:" + from);                System.out.println("---------------env:" + env.getProperty("from"));                System.out.println("---------------aaa:" + u.getAaa());                System.out.println("---------------bbb:" + u.getBbb());                return "";    }}


 

下面重点说一下第三种调用方式:首先需要顶一个model类,用来存放我们从各个配置文件中读取的值,这个类必须要使用@ConfigurationProperties注解,而且各个字段名要和配置文件中的文件名一致:

Us.java文件(负责存放从git仓厘米获取到的配置文件里面的具体信息)

 

package com.didispace.entity;import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationPropertiespublic class Us {         private String aaa;        private String bbb;    public String getAaa() {        return aaa;    }    public void setAaa(String aaa) {        this.aaa = aaa;    }    public String getBbb() {        return bbb;    }    public void setBbb(String bbb) {        this.bbb = bbb;    }}


 

而上面的代码Control.Java文件中,分别需要使用@RefreshScope、@EnableConfigurationProperties(Us.class)两个注解。

@RefreshScope是热部署,即配置文件改变的话,配置中心客户端可以实时更新。

@EnableConfigurationProperties(Us.class)则是调用之前定义好了的Us.java文件,并且再通过

@Autowiredprivate Us u;


将具体实例注进到调用此实例的类中。

 

最后,看一下后台打印结果:



 

 

总结:

1、配置中心的服务端,其实就是在application.properties中添加一些配置信息就可以实现配置中心的服务端调用

2、所谓的配置中心,其实主要工作还是在配置中心客户端一侧,或者说主要工作就是在服务端一侧读取git仓中信息的过程,也就是我之前说的客户端api的3种读取配置信息。

 

比较:

  1、通过Environment env获取配置信息,好处是可以根据键值对形式,直接获取到信息;但是它一般是进行debugger调测使用,不推荐在具体项目中大量使用

  2、通过@Value("${from}")注解,如果属性过多,需要自己逐个的去注解字段,不方便复用

  3、而自己封装model,我觉得比较好用,可以根据自己的需求,动态的进行属性配置,而且做到了复用的效果,推荐使用。

 

后期我会基于配置中心模块,专门在再整理一份关于配置数据源的博文,到时候可以详细的体验出使用自己封装model的好处

 

 

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: