目录
SpringCloud系列开篇文章就说到了,公司新项目将入坑SpringCloud全家桶,但原先线上老项目是采用Spring+Dubbo技术栈的,新项目中的部分业务还是和老项目有点关联的,重写这一部分业务逻辑也是需要点时间,本来想通过http请求进行业务交互的,总感觉不够优雅,原先也了解过SpringCloud Alibaba,趁着这个机会捣腾捣腾。
spring-boot-version: 2.2.5.RELEASE
spring-cloud.version: Hoxton.SR3
dubbo版本会分成2.6.x系列和2.7.x系列二个版本进行说明
dubbo2.6.x
<dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.1.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.5</version> </dependency>
dubbo2.7.x
<dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.6</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.6</version> </dependency>
本章节主要记录集成Dubbo,所以SpringCloud组件中只用到了Eureka服务,为了便于说明Duboo服务调用简单搞了个图说明下
因仅仅是简单演示,我这边偷下懒,通过访问老项目中/web/user/addCart接口调用新项目中DCartServiceImpl的addCart方法,其中addCart方法中又会调用老项目中UserServiceImpl的getByUserId方法,从而演示SpringCloud项目和Dubbo项目之间互相调用
项目结构图如下
cart-facade购物车对外接口定义模块
pom.xml
<?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> <groupId>com.chinawu.cloud</groupId> <artifactId>cart-facade</artifactId> <version>1.0.0-SNAPSHOT</version> </project>
DCartService.java
public interface DCartService { /** * <p > * 功能:加入购物车 * </p> * @param userId 用户id * @param goodsId 物品id * @author wuyubin * @date 2020年04月22日 * @return */ String addCart(Long userId,Long goodsId); }
cart-service购物车接口实现模块
pom.xml
<dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.1.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.5</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.12.0</version> </dependency> <!--老项目用户模块对外接口--> <dependency> <groupId>com.chinawu.old</groupId> <artifactId>user-facade</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency>
application.properties
# 服务端口 server.port=9007 # 服务应用名称 spring.application.name=cart-service # eureka服务端地址 eureka.client.serviceUrl.defaultZone=http://localhost:9003/eureka/ # dubbo扫描包路径 dubbo.scan.base-packages=com.chinawu.cloud.cart.service # dubbo协议 dubbo.protocol.name=dubbo # 随机端口 dubbo.protocol.port=-1 # zookeeper地址 dubbo.registry.address=zookeeper://127.0.0.1:2181
DCartServiceImpl.java
注意事项:
@Service为com.alibaba.dubbo.config.annotation包下的注解标签而不是Spring中的
@Reference 申明引用Dubbo服务
@Service public class DCartServiceImpl implements DCartService { Logger LOGGER = LoggerFactory.getLogger(DCartServiceImpl.class); @Reference DUserService dUserService; /** * <p > * 功能:加入购物车 * </p> * @param userId 用户id * @param goodsId 物品id * @author wuyubin * @date 2020年04月22日 * @return */ public String addCart(Long userId,Long goodsId) { DUserDTO userDTO = dUserService.getByUserId(userId); LOGGER.info("i am springcloud project dubbo method:addCart,userId:{},goodsId:{}",userId,goodsId); return userDTO.toString()+" addCart goodsId:"+goodsId+" success"; } }
CartServiceApplication.java
@SpringBootApplication @EnableDiscoveryClient //@EnableAutoConfiguration public class CartServiceApplication { public static void main(String[] args) { SpringApplication.run(CartServiceApplication.class, args); } }
xxx-user模块依托于xxx-web模块运行访问,项目结构图如下
user-facade用户模块对外接口定义类
项目结构如下
public interface DUserService { /** * <p > * 功能:根据用户id查询用户信息 * </p> * @param * @author wuyubin * @date 2020年04月23日 * @return */ DUserDTO getByUserId(Long userId); }
user-service用户模块业务模块
项目结构如下
pom.xml
<dependencies> <dependency> <groupId>com.chinawu.old</groupId> <artifactId>user-facade</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <!--新项目Dubbo接口定义类--> <dependency> <groupId>com.chinawu.cloud</groupId> <artifactId>cart-facade</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> </dependencies>
user-dubbo.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!--对外暴露提供服务接口--> <dubbo:service interface="com.chinawu.old.user.facade.interfaces.DUserService" ref="userServiceImpl" /> <!--引用其他模块服务接口--> <dubbo:reference interface="com.chinawu.cloud.cart.interfaces.DCartService" id="userServiceDCartService"></dubbo:reference> </beans>
UserServiceImpl.java
此处@Service为org.springframework.stereotype.Service
@Service("userServiceImpl") public class UserServiceImpl implements UserService,DUserService { Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class); @Autowired DCartService userServiceDCartService; /** * <p > * 功能:根据用户id获取会员名称 * </p> * @param * @author wuyubin * @date 2020年04月23日 * @return */ public String getNameById(Long userId) { return "admin"; } /** * <p > * 功能:根据用户id查询用户信息 * </p> * @param * @author wuyubin * @date 2020年04月23日 * @return */ public DUserDTO getByUserId(Long userId) { DUserDTO dto = null; String userName = getNameById(userId); if (null != userName && !"".equals(userName)) { dto = new DUserDTO(); dto.setUserId(userId); dto.setUserName(userName); dto.setCreateTime(new Date()); } LOGGER.info("i am old project dubbo method:getByUserId,userId:{}",userId); return dto; } /** * <p > * 功能:添加购物车(仅仅演示SpringCloud和Dubbo集成) * </p> * @param * @author wuyubin * @date 2020年04月23日 * @return */ public String addCart(Long userId,Long goodsId) { return userServiceDCartService.addCart(userId,goodsId); } }
user-web用户模块web请求
项目结构如下
WebUserController.java
@RestController @RequestMapping("/web/user") public class WebUserController { @Autowired UserService userServiceImpl; @RequestMapping(value = "/detail") public String getDetail(Long userId){ return userServiceImpl.getNameById(userId); } @RequestMapping(value = "/addCart") public String addCart(Long userId,Long goodsId){ return userServiceImpl.addCart(userId,goodsId); } }
最后通过访问
http://localhost:8080/xxx_web_war_exploded/web/user/addCart?userId=1&goodsId=1
如果项目中使用的Dubbo为2.7.x系列的,调整相关jar包依赖即可。
新项目中将原先dubbo-spring-boot-starter和dubbo进行如下替换
<dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.6</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.6</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.0.1</version> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.7</version> </dependency>
如果调整后项目启动报错,则需根据相应报错内容进行解决,主要是依赖包的版本问题,笔者在调整工程中就遇到如下问题
问题:org.apache.zookeeper.KeeperException$UnimplementedException: KeeperErrorCode = Unimplemented
原因:
因为项目中有依赖curator-recipes包,而curator-recipes 4.0.1包中依赖zookeeper版本是3.5.x的,排除curator-recipes包中的zookeeper,依赖自己的zookeeper版本即可
老项目模块升级比较方便,直接将原先dubbo调整如下即可
<dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.6</version> </dependency>
最后通过访问,获取到正常数据就OK啦
http://localhost:8080/xxx_web_war_exploded/web/user/addCart?userId=1&goodsId=1https://github.com/apache/dubbo-spring-boot-project