Holy Null 's Blog
Holy Null!
Toggle navigation
Holy Null 's Blog
主页
机器学习
Flume
Nginx
Hadoop
Apache Shiro
架构理论
Docker
Spring Cloud
关于我
归档
标签
Spring Cloud 教程|第二篇 统一配置服务
2017-12-25 13:34:44
266
0
0
holynull
# 第二篇 统一配置服务 在Spring Cloud搭建的平台中,使用Config Server来统一管理配置文件。首先,需要建立一个Config Server,在注册中心Eureka Server中进行注册。由Config Server统一为整个平台的服务实例(服务提供者和服务消费者)提供配置文件读取服务。也就是说,当一个服务实例加载自己的配置文件时,不是加载本地的配置文件,而是加载Config Server上的配置文件。 所有的配置文件将在一个git服务上进行管理,Config Server会根据不同的请求来读取git服务上的配置文件,然后将配置文件数据交给服务实例。 ## 一、创建一个Config Server 我们在`spring-cloud-poc`项目下创建一个`config-server`模块。在pom.xml文件中加入如下依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> ``` ## 二、创建一个启动类 创建启动类`com.ultimatech.cfgserver.ConfigServerApplication`: ``` package com.ultimatech.cfgserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * Created by zhangleping on 2017/10/18. */ @SpringBootApplication @EnableConfigServer @EnableEurekaClient public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } } ``` 使用`@EnableConfigServer`来实现配置管理,使用`@EnableEurekaClient`来实现服务的注册和发现。Config Server也可以看做是一个服务提供者,也是在Eureka Server中注册的一个Client。 ## 三、创建一个管理配置文件的项目 创建一个项目`cloud-config`,项目仅用来管理配置文件。在项目的`resources`目录下创建一个配置文件`eclient-1-prd.yml`: ``` eureka: instance: hostname: eclient-1 serviceUrl: defaultZone: http://eureka-1:8761/eureka/,http://eureka-2:8761/eureka/ spring: datasource: url: jdbc:oracle:thin:@oracle12c:1521:xe username: workflow password: workflow driver-class-name: oracle.jdbc.OracleDriver max-idle: 10 max-wait: 10000 min-idle: 5 initial-size: 5 #=====================jpa config================================ jpa: hibernate: ddl-auto: update #打印sql语句 show-sql: true #格式化输出的json字符串 jackson: serialization: indent_output: true ``` *注意:* *这个配置文件我们将在下一篇,创建一个服务提供者时会用到。* 最后将这个文件`git push`到git服务器端。 ## 四、创建Config Server的配置文件 在模块的`resources`目录下创建一个基础配置文件,如下: ``` spring: application: name: config-server cloud: config: server: git: uri: https://gitee.com/holynyll/spring-cloud-poc.git searchPaths: cloud-config/src/main/resosurces username: rrdgitee@126.com password: rrdgitee1234 label: master server: port: 8080 eureka: client: healthcheck: enabled: true ``` 主要参数说明: |参数名|说明| |---|---| |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服务的密码| |spring.cloud.config.server.label|分支| 然后在模块的`config`文件夹中创建一个配置文件`config-server-1-prd.yml`,用来配置部署到docker环境下的Config Server实例。 ``` spring: application: name: config-server cloud: config: server: git: uri: https://gitee.com/holynyll/spring-cloud-poc.git searchPaths: cloud-config/src/main/resosurces username: rrdgitee@126.com password: rrdgitee1234 label: master server: port: 8080 eureka: instance: hostname: config-server-1 client: serviceUrl: defaultZone: http://eureka-1:8761/eureka/,http://eureka-2:8761/eureka/ ``` 在模块的根目录下运行如下命令进行编译: ``` mvn clean install ``` ## 五、部署 在`spring-cloud-poc`项目根目录下的`docker-compose.yml`文件中,增加`services`: ``` config-server-1: build: ./docker/webnode container_name: config-server-1 volumes: - "/tmp" - "./config-server/target/config-server-1.0-SNAPSHOT.jar:/app.jar" - "/config" - "./config-server/config/config-server-local-prd.yml:/config/application-prd.yml" # - "./config/log4j2.xml:/config/log4j2.xml" environment: JAVA_OPTS: -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=128m depends_on: - eureka-1 - eureka-2 ports: - "8082:8080" networks: mynet: ipv4_address: 172.19.0.8 extra_hosts: - "eureka-1:172.19.0.2" - "eureka-2:172.19.0.3" - "eclient-1:172.19.0.5" - "eclient-2:172.19.0.6" - "feign-1:172.19.0.7" - "config-server-1:172.19.0.8" - "service-2-1:172.19.0.9" - "apigateway-1:172.19.0.10" - "feign-2:172.19.0.11" - "mysql-1:172.19.0.12" ``` ## 六、启动 在`spring-cloud-poc`目录下,运行: ``` docker-compose up -d config-server-1 ``` ## 六、验证 在浏览器中访问: ``` http://localhost:8082/eclient-1-prd.yml ``` 可以看到之前在`cloud-config`中创建的`eclient-1-prd.yml`配置文件的内容。 *注意:* *此文件的内容是来至git服务器端的配置文件内容,并非本地`cloud-config`项目中文件。*
上一篇:
Spring Cloud 教程|第三篇 开发部署一个服务
下一篇:
Spring Cloud 教程|第一篇 Eureka注册中心
0
赞
266 人读过
新浪微博
微信
腾讯微博
QQ空间
人人网
提交评论
立即登录
, 发表评论.
没有帐号?
立即注册
0
条评论
More...
文档导航
没有帐号? 立即注册