更新日志:
2018/3/3 21:05:43 新建
2018/3/11 7:11:59 新增注册到Eureka、从Eureka注销、新增Feign,更新配置文件,更新代码
部门项目的技术框架从 ZooKeeper+Dubbo 转型为Spring Cloud 微服务,转型顺利、开发方便、使用良好,于是完全废弃了ZooKeeper+Dubbo,而Web端后台管理界面的项目由于种种原因不希望大规模重构为Spring Boot项目,继续保持原有的SSM框架,并使用http调用微服务接口。为避免将微服务地址写死,这就需要Web项目连接到Spring Cloud Eureka 上通过服务名获取微服务真实地址。
项目依赖
<!-- eureka 服务发现 -->
<dependency>
<groupId>com.netflix.eureka</groupId>
<artifactId>eureka-client</artifactId>
<version>1.7.0</version>
</dependency>
<!-- Ribbon 负载均衡 -->
<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-core</artifactId>
<version>${netflix.ribbon.version}</version>
</dependency>
<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-loadbalancer</artifactId>
<version>${netflix.ribbon.version}</version>
<exclusions>
<exclusion>
<groupId>io.reactivex</groupId>
<artifactId>rxjava</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-eureka</artifactId>
<version>${netflix.ribbon.version}</version>
</dependency>
<!-- Feign 包装http请求 -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-hystrix</artifactId>
<version>${netflix.feign.version}</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-ribbon</artifactId>
<version>${netflix.feign.version}</version>
</dependency>
<!-- <dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-gson</artifactId>
<version>${netflix.feign.version}</version>
</dependency> -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-slf4j</artifactId>
<version>${netflix.feign.version}</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-jackson</artifactId>
<version>${netflix.feign.version}</version>
</dependency>
<dependency>
<groupId>io.reactivex</groupId>
<artifactId>rxjava</artifactId>
<version>1.1.1</version>
</dependency>
这里使用netflix项目下的eureka、ribbon、Feign、Hystrix。
ribbon-开头的项目都是同一个版本号,所以就抽取出${netflix.ribbon.version}
统一管理。feign-开头的项目也都是同一个的版本号,抽取${netflix.feign.version}统一管理。
需要注意:
1. Maven依赖jar包冲突问题:rxjava
项目在ribbon-loadbalancer
和feign-hystrix
依赖的hystrix-core
中都有使用。当前最新版2.2.4的ribbon-loadbalancer
使用rxjava:1.0.9
。而feign-hystrix
依赖的hystrix-core
使用rxjava:1.1.1
。因为依赖冲突,ribbon-loadbalancer
中的rxjava:1.0.9
代替掉了hystrix-core
中的rxjava:1.1.1
。这样当程序运行时会疯狂报找不到类Error,找不到rx/Single
,这个类在2.0.9中并没有,2.1.1中有hystrix-core
用到了,但是由于依赖冲突使用2.0.9的rxjava没有该类,所以报错。
解决办法:ribbon-loadbalancer
使用 exclusion
排除依赖 rxjava
即可。
2. feign-core在中央仓库有两个groupId: com.netflix.feign
和 io.github.openfeign
。groupIdcom.netflix.feign
在2016年7月提交到8.18.0后就没有再提交,而groupIdio.github.openfeign
已经在2018年三月份提交到9.6.0。
配置文件
Ribbon配置
# ribbon.properties
# xxx-service对应的微服务名
xxx-service.ribbon.DeploymentContextBasedVipAddresses=xxx-service
# 固定写法,xxx-service使用的ribbon负载均衡器
xxx-service.ribbon.NIWSServerListClassName=com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList
# 每分钟更新xxx-service对应服务的可用地址列表
xxx-service.ribbon.ServerListRefreshInterval=60000
Eureka配置
Eureka默认在classpath中寻找eureka-client.properties配置文件
# 控制是否注册自身到eureka中,本项目虽然不对外提供服务,但需要Eureka监控,在Eureka列表上显示
eureka.registration.enabled=true
# eureka相关配置
# 默认为true,以实现更好的基于区域的负载平衡。
eureka.preferSameZone=true
# 是否要使用基于DNS的查找来确定其他eureka服务器
eureka.shouldUseDns=false
# 由于shouldUseDns