Spring Boot 2.1.0 已经发布,现在 Spring Cloud 格林威治版本也发布了,现在为项目网关zuul做一次整体框架升级到最新稳定版 基于Spring Boot 2.0.x 的 Finchley 版本。
升级前 => 升级后
Spring Boot 1.5.14 => Spring Boot 2.0.7
Spring Cloud Edgware SR4 => Spring Cloud Finchley. SR2
1.pom文件依赖变更:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.7.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.注册中心Eureka client客户端依赖更新
升级前:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
升级后:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
3.注册中心里面的客户端实例IP显示不正确,
启动出现异常2019-01-17 09:22:22 353 com.netflix.discovery.DiscoveryClient | DiscoveryClient_ZUUL/xx-PC.xxcom:${spring.cloud.client.ipAddress}:7093 - was unable to refresh its cache! status = Retry limit reached; giving up on completing the request.
因为 Spring Cloud 获取服务客户端 IP 地址配置变更了。
升级前:
${spring.cloud.client.ipAddress}
升级后:
${spring.cloud.client.ip-address}
4.zuul依赖变更
升级前:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
升级后:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
5.feign依赖变更
升级后:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
6.熔断器方法变更
升级前:
@Component
public class HttpFallbackProvider implements FallbackProvider{
public ClientHttpResponse fallbackResponse(Throwable cause) {
}
}
升级后:
@Component
public class HttpFallbackProvider implements FallbackProvider{
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
}
}
6.异常处理方面的变更
升级前:
import org.springframework.boot.autoconfigure.web.DefaultErrorAttributes;
import org.springframework.web.context.request.RequestAttributes;
@Component
public class ZuulErrorAttribute extends DefaultErrorAttributes{
@Override
public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace) {
Map<String, Object> result = super.getErrorAttributes(requestAttributes, includeStackTrace);
log.error("--ZuulErrorAttribute--result:{}",result);
}
}
升级后:
RequestAttributes 变更为WebRequest,DefaultErrorAttributes引入变更。
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.web.context.request.WebRequest;
@Component
public class ZuulErrorAttribute extends DefaultErrorAttributes{
public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
Map<String, Object> result = super.getErrorAttributes(webRequest, includeStackTrace);
log.error("--ZuulErrorAttribute--result:{}",result);
}
}