# spring boot / cloud (十七) 快速搭建注册中心和配置中心 本文将使用spring cloud的eureka和config server来搭建. 然后搭建的模式,有很多种,本文主要聊的是将注册中心和配置中心整合成一个服务的方式. 对于其他方式,如果有同学感兴趣,还请自行百度,谢谢. ## 为什么将注册中心和配置中心整合在一起? 其实整合在一起和分开,在使用层面上并没有太大的区别,主要就是节省资源,启动一个服务就够了 ## 开始搭建 ### 添加pom依赖 ``` xml org.springframework.cloud spring-cloud-starter-eureka-server org.springframework.cloud spring-cloud-config-server org.itkk.udf udf-starter-amqp-rabbitmq ``` 以上为主要的依赖,引入了eureka server和config server以及amqp amqp主要是为了支持config server的在runtime刷新各个子服务的配置文件的这个特性 ### pom关键配置 ``` xml ${project.basedir}/src/main/resources/ true ``` 以上配置可以使我们在待会的配置文件中引用maven的上下文属性,通过${xxx}的方式 ``` xml org.springframework.boot spring-boot-maven-plugin ${springboot.version} repackage ``` 添加打包插件,会将这个应用打包成一个可执行的jar包(内置容器,可直接启动应用) ### 编写启动类 ``` java @SpringCloudApplication @EnableEurekaServer @EnableConfigServer public class UdfEurekaConfigServerDemoApplication extends BaseApplication { /** * 描述 : spring boot的入口 * * @param args 参数 */ public static void main(String[] args) { SpringApplication.run(UdfEurekaConfigServerDemoApplication.class, args); } } ``` 标记@SpringCloudApplication,@EnableEurekaServer,@EnableConfigServer开启注册中心和配置中心 同时这里继承了BaseApplication,这个类是前面博客中udf-starter-core中的一个类,此类做了一些简单的封装,详情大家可查阅 ## bootstrap.properties主要配置文件详解 ``` properties info.build.group=${project.groupId} info.build.artifact=${project.artifactId} info.build.version=${project.version} info.build.name=${project.name} info.build.time=${project.build.date} info.build.encoding=${project.build.sourceEncoding} info.build.java.source=${java.version} info.build.java.target=${java.version} ``` 以上配置,主要定义了应用的一些基础信息,并且这些基础信息都是通过${xxx}的方式直接从maven变量中获取的 这些信息在应用启动了之后,可以通过**http://localhost:port/info**访问 注意,这里的${project.build.date}是UTC时间,不是北京时间,所以查看的时候,要+8小时 ``` properties spring.application.name=${project.name} server.port=${server.port} management.security.enabled=false spring.profiles.active=${profiles.activation} ``` 以上配置主要设定了应用名称(使用项目的名称),应用端口(从pom中获取),以及profiles ## bootstrap-xxx.properties主要配置文件详解 ``` properties security.user.name=admin security.user.password=123456 ``` 设置应用的basic认证用户名和密码 ``` properties eureka.instance.preferIpAddress=true eureka.client.enabled=true eureka.client.serviceUrl.defaultZone=http://admin:123456@127.0.0.1:${server.port}/eureka/ eureka.client.register-with-eureka=true eureka.client.fetch-registry=true ``` 配置eureka实例和客户端信息,注意的是,因为这里是讲config server整合到注册中心上了,所以这里必须要将自身也注册到eureka上,否则,其他应用无法找到config server ``` properties spring.cloud.config.server.prefix=config-server spring.cloud.config.server.git.uri=https://git.oschina.net/wangkang/udf-sample.git spring.cloud.config.server.git.searchPaths=udf-config-hub/{application} spring.cloud.config.server.git.defaultLabel=master spring.cloud.config.server.git.forcePull=true ``` 这里配置了config server的主要信息 , 首先prefix是为了配置中心的上下文根和注册中心区分开来 然后在searchPaths中,spring cloud为我们预留了{application(应用名)}/{profile(环境)}/{label(分支)}这三个变量 大家可以根据这3个变量,灵活的组合远端git仓库中目录的结构,如下 : ``` properties usf-server-a -> usf-server-a-dev.properties -> usf-server-a-qa.properties usf-server-b -> usf-server-b-dev.properties -> usf-server-b-qa.properties ``` 在这里的话,我只使用了{application} ``` properties spring.rabbitmq.host=itkk.org spring.rabbitmq.port=5672 spring.rabbitmq.username=dev_udf-sample spring.rabbitmq.password=1qazxsw2 spring.rabbitmq.virtual-host=/dev_udf-sample spring.rabbitmq.template.retry.enabled=true ``` 以上配置了消息中间件rabbitmq的信息 ## *.properties和*.yml的区别 ? 本质上没有什么区别 , yml格式是属性结构的 , 可能对于某些同学来说 , 感觉更易于阅读 . 不过我不喜欢(哈哈) , 习惯用properties了 , 所以本文全部使用的是*.properties文件进行配置的 , 此格式的文件等价于*.yml , 如有偏好yml的同学 , 可自行转换 ## application.properties和bootstrap.properties的区别 ? 首先共同点 , 他们都可以用来配置参数 . 但是bootstrap的加载优先级高于application 在spring cloud应用中,官方也推荐使用bootstrap来存放配置 所以本文以及后续的配置项都会存放在bootstrap.properties中 ## 如何区分profiles,来加载不同环境的配置文件 ? 在spring boot的中是通过**spring.profiles.active**属性来设置环境的,默认为dev 然后spring boot预先约定配置文件通过"-"分隔,"-"之后,".properties"之前,之间的部分就是**profiles**,例如 : ``` java bootstrap.properties bootstrap-dev.properties bootstrap-qa.properties bootstrap-xxxx.properties ``` 注意,没有带**profiles**的配置文件,称为默认配置,不管什么环境下,都是必然会被加载的. 然后在启动应用的时候,指定profiles即可,如下 : ``` java java -server -jar demo-1.0.jar --spring.profiles.active=qa ``` ## 如何动态刷新配置 ? 可在任何config client或者config service上执请求如下的HTTP服务 : ``` java POST http://localhost:port/bus/refresh ``` 那么,config server就会通过消息中间件rabbitmq将配置文件更新的事件通知到各个微服务中了. ## **结束** 今天跟大家分享了整个注册中心和配置中心的样例和主要的配置,相关完整的项目大家可在udf-sample中的udf-eureka-config-server-demo中查阅 欢迎大家的意见跟建议 ## **代码仓库** (博客配套代码) - [udf-starter : 基础项目,脚手架,框架](https://gitee.com/wangkang/udf) - [udf-sample : 集成样例](https://gitee.com/wangkang/udf-sample) --------- 想获得最快更新,请关注公众号 ![想获得最快更新,请关注公众号](https://static.oschina.net/uploads/img/201705/24155414_Pukg.jpg "想获得最快更新,请关注公众号")