SpringMvc项目接入SpringCloud微服务的解决方案

SpringMvc项目接入SpringCloud微服务的解决方案

在SpringBoot项目大行其道的时代,仍有很多项目是基于SpringMvc,甚至是基于struts的,这些项目的特点是年代久远,项目庞大,设计文档存在缺漏。但是对于这些项目,我们不能放任不管,甚至很多项目还在为公司创造这价值。
随着公司业务的发展,原先的单体项目已经不能满足快速发展变化的业务的需求,这时候就要进行微服务改造。

方案一:Sidecar异构接入

Sidecar异构架构
Sidecar项目可以看作老项目的影子项目,由Sidecar负责接入注册中心,并且发起远程调用和被远程调用。两个项目为一个整体对外提供服务。

优势:
  1. sidecar可以无视语言,可以用于代理古老的java项目,也可以异构代理其他语言项目,如php、python、nodejs等
  2. sidecar可以在不对老项目进行大规模改造的情况下快速接入SpringCloud微服务,渐进式的做微服务拆分
劣势
  1. 增加项目架构和维护的复杂度,多一个项目多一个环境意味着出问题的概率又多一分
Sidecar项目配置
  1. 老项目添加HealthController
/**
 * @Usage: 用于健康检查
 */
@RestController
public class HealthController {
    @RequestMapping("/openapi/health/status")
    public Map status(){
        Map<String, String> map = new HashMap<>(1);
        map.put("status","UP");
        return map;
    }
}
  1. 新建sidecar maven项目
  2. 引入依赖
<dependencies>
    <!-- sidecar 依赖-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-netflix-sidecar</artifactId>
        <version>2.1.0.RELEASE</version>
    </dependency>
    <!-- eureka 注册中心-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>2.1.0.RELEASE</version>
    </dependency>
    <!-- hystrix 熔断-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        <version>2.1.0.RELEASE</version>
    </dependency>
    <!-- feign 声明式服务调用-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>  
        <version>2.1.0.RELEASE</version>
    </dependency>
</dependencies>
  1. 项目启动类加入如下注解
@EnableFeignClients
@EnableSidecar

不用@EnableEurekaClient,因为@EnableSidecar中包含来eureka的注册逻辑
5. 配置文件增加sidecar、eureka、feign、hystrix、ribbon配置
重点讲一下sidecar配置

sidecar:
# 配置接入web的端口
port: 8080
home-page-uri: http://localhost:${sidecar.port}/
# 配置接入web的健康检查rest接口,sidecar将请求该url,用以确定接入应用是否存活.
health-uri: http://localhost:${sidecar.port}/openapi/health/status

方案二:升级SpringBoot并集成SpringCloud组件

一、升级SpringBoot 1.x版本

在不改动Spring版本的情况下引入SrpingBoot,将项目改造为SpringBoot项目
SpringBoot 1.x 引用的Spring 4.x版本,而一般SpringMvc项目也是使用Spring4.x版本,如果使用Spring更老版本或未使用spring则另当别论。

二、升级SpringBoot 2.x版本

SpringBoot 2.x版本使用的是Spring5.x版本,升级2.x意味着升级spring并且需要解决spring升级带来的问题

三、引入SpringCloud并集成SpringCloud组件
遇到的问题
集成Eureka时,Cannot create Jersey client错误

问题原因:jersey 与 fastjson 某些版本冲突导致服务无法启动
错误:

Caused by: com.sun.jersey.spi.inject.Errors$ErrorMessagesException: null

解决方案:fastjson升级到 1.2.37以上版本

https://github.com/alibaba/fastjson/releases/tag/1.2.37

日期响应为linux时间戳

升级SpringBoot后,原先的日期格式化失效,在不给字段加@JsonFormat注解的情况下便要思考通过全局配置来解决

正常配置

spring.jackson.time-zone=GMT+8
# 指定响应数据格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
# 指定不返回时间戳
spring.jackson.serialization.write-dates-as-timestamps=false

这样配置后并没有生效,继续查找原因。

Finally, if you opt out of the Spring Boot default MVC configuration by providing your own @EnableWebMvc configuration, you can take control completely and do everything manually by using getMessageConverters from WebMvcConfigurationSupport.
《Spring Boot Reference Guide》

去掉@EnableWebMvc后,配置可以生效。

结合WebMvcAutoConfiguration理解@EnableWebMvc

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值