前言
记录一次学习SpringCloud的过程,不断积累经验,手撸方能熟能生巧。
新手上路难免有误,人非圣贤,欢迎各位指出不足之处,虚心听取各位的建议与意见。
项目源码:https://github.com/Ahua0918/hwa
一、简介
在分布式系统中,由于服务数量巨多,在业务上有时候需要修改一些配置信息,例如版本信息,倘若没有配置服务,那么需要挨个修改微服务,挨个重新部署微服务,这样就比较麻烦,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件,这样可以大大节约了开发,上线和重新部署的时间了。在SpringCloud中,有分布式配置中心组件SpringCloud Config,它支持配置服务的内存中(即本地),也支持放在远程仓库中。在SpringCloud Config组件中,分两个角色,一是ConfigServer,二是ConfigClient。
二、构建ConfigServer
同样基于Maven父项目创建子项目,右击项目--Module--选择Maven--选择对应JDK版本--点击Next--输入模块名hwa-config-server即可
2.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">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>hwa</artifactId>
<groupId>com.lee</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>hwa-config-server</artifactId>
<packaging>jar</packaging>
<description>SuperConfigServer</description>
<dependencies>
<!-- ConfigServer jar包依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
</project>
2.2 编写ConfigServer启动类
package com.lee.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer //通过注解@EnableConfigServer开启配置服务器功能
@EnableDiscoveryClient
public class HwaConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(HwaConfigServerApplication.class, args);
}
}
2.3 编写配置文件application.yml
uri:配置git仓库地址 仓库名字一定要与git仓库名字一致
search-paths: 配置仓库路径
label:配置仓库的分支
如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写,本例子是公开仓库,放心使用
#注册中心端口
server:
port: 8886
spring:
application:
#服务名称
name: hwa-config-server
cloud:
config:
server:
git:
uri: https://github.com/Ahua0918/SpringCloudConfig/
search-paths: respo
label: master
#主机名
eureka:
client:
#eureka服务地址
#注明自己的服务注册中心的地址,与Eureka Server中的配置application.yml相对应
service-url:
defaultZone: http://localhost:1116/eureka/
远程仓库https://github.com/Ahua0918/SpringCloudConfig/中有个文件hwa-eureka-feign-dev.properties文件中有一个属性
hwa = hwa springcloud version 1.1
2.4 启动并访问
先启动EurekaApplication,再启动HwaConfigServerApplication,然后访问http://localhost:8886/version/dev,
如下图所示,证明配置服务中心可以从远程程序获取配置信息
三、构建ConfigClient
接下来把视图微服务-Feign改造成配置服务端,使其可以从配置服务器上获取版本信息
3.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">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>hwa</artifactId>
<groupId>com.lee</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>hwa-eureka-feign</artifactId>
<packaging>jar</packaging>
<description>SuperFeign</description>
<dependencies>
<!-- Feign客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- Config客户端依赖 用于访问配置服务器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
</project>
3.2 新增Bootstrap.yml
作为配置客户端,需要在Bootstrap.yml里面配置config-server的信息,而不是像以前那样在application.yml里进行配置
label:指明远程仓库的分支
profile:dev开发环境配置文件 test测试环境 pro正式环境
service-id:指向配置服务器
spring:
cloud:
config:
label: master
profile: dev
discovery:
enabled: true
service-id: hwa-config-server
#主机名
eureka:
client:
#eureka服务地址
#注明自己的服务注册中心的地址,与Eureka Server中的配置application.yml相对应
service-url:
defaultZone: http://localhost:1116/eureka/
3.3 Application.yml
Application.yml没有发生什么变化,就是把eureka地址信息移到了bootstrap.yml里
#注册中心端口
server:
port: 8884
spring:
application:
#服务名称
name: hwa-eureka-feign
3.4 修改HwaFeignApplication启动类
写一个Api接口 “/hiConfigClient”,返回从配置中心读取的hwa变量的值
package com.lee.feign;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients //通过注解@EnableFeignClients配置Feign客户端功能
@RestController
public class HwaFeignApplication {
public static void main(String[] args) {
SpringApplication.run(HwaFeignApplication.class, args);
}
@Value("${hwa}")
String hwa;
@RequestMapping(value = "/hiConfigClient")
public String hi(){
return hwa;
}
}
3.5 启动并访问
先启动EurekaApplication,接着启动HwaConfigServerApplication,再启动HwaFeignApplication,然后访问http://localhost:8884/hiConfigClient,如下图所示,说明config-client从config-server获取hwa的属性,而config-server是从git仓库读取的
3.6 缺点
修改版本信息:hwa = hwa springcloud version 1.2,然后刷新http://localhost:8884/hiConfigClient之后发现仍然是1.1,必须重启HwaConfigServerApplication和HwaFeignApplication才能生效,但这样生产效率有点低,所以又学习了RabbitMQ动态刷新实现