一、基本使用
需求:
某个电商系统,订单服务需要调用用户服务获取某个用户的所有地址;
我们现在需要创建两个服务模块进行测试
模块 | 功能 |
订单服务web模块 | 创建订单等 |
用户服务service模块 | 查询用户地址等 |
测试预期结果:
订单服务web模块在A服务器,用户服务模块在B服务器,A可以远程调用B的功能。
二、spring整合dubbo
以下使用XML 配置的方式,更多配置方式见官方文档
2.1 spring-common模块:
公共接口层(model,service,exception…),定义公共接口,也可以导入公共依赖
UserAddress.java:用户地址实体类
public class UserAddress implements Serializable { private Integer id; private String userAddress; private String userId; private String consignee; private String phoneNum; private String isDefault; .... }
IOrderService.java:订单接口
public interface IOrderService { /** * 用户下单 * @param userId */ UserAddress placeOrder(int userId); }
IUserService.java:用户接口
public interface IUserService { /** * 根据用户id获取用户地址 * @param userId * @return */ UserAddress getUserAddById(int userId); }
pom.xml:通用的依赖,引入dubbo和zkclient
<?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"> <parent> <artifactId>dubboStudy</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spring-common</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <!--dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.10</version> </dependency> <!--zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> </dependencies> </project>
2.2 spring-user模块:
用户业务,作为服务提供者
pom.xml:引入通用模块,可以使用定义的接口
<dependency> <groupId>org.example</groupId> <artifactId>spring-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
UserServiceImpl.xml:IUserService的实现类,供远程调用
public class UserServiceImpl implements IUserService { @Override public UserAddress getUserAddById(int userId) { UserAddress userAddress = new UserAddress(); userAddress.setUserAddress("上海市宝山区"); return userAddress; } }
provider.xml:dubbo配置信息
<?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://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!--1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名)--> <dubbo:application name="spring-user"/> <!--2、指定注册中心的位置--> <!--<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>--> <dubbo:registry address="zookeeper://192.168.31.136:2181"/> <!--3、指定通信规则(通信协议? 服务端口)--> <dubbo:protocol name="dubbo" port="20880"/> <!--4、暴露服务 让别人调用 ref指向服务的真正实现对象--> <bean id="userServiceImpl" class="me.nic.service.impl.UserServiceImpl"/> <!--服务的实现--> <dubbo:service interface="me.nic.service.IUserService" ref="userServiceImpl"/> </beans>
ConsumerRun.java:启动:
public class ConsumerRun { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml"); IOrderService orderService = applicationContext.getBean(IOrderService.class); orderService.placeOrder(1); System.in.read(); } }
2.3 spring-order模块:
订单业务,作为服务消费者
pom.xml:引入通用模块,可以使用定义的接口,同1.2
OrderServiceImpl.xml:IOrderService的实现类,其中远程调用userService
@Service public class OrderServiceImpl implements IOrderService { @Autowired private IUserService userService; @Override public UserAddress placeOrder(int userId) { // 远程调用,获取用户地址 UserAddress userAddById = userService.getUserAddById(userId); // 下单 System.out.println("用户地址:" + userAddById.getUserAddress()); System.out.println("下单成功"); return null; } }
consumer.xml:dubbo配置信息
<?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:context="http://www.springframework.org/schema/context" 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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!--包扫描--> <context:component-scan base-package="me.nic.service.impl"/> <!--指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名)--> <dubbo:application name="spring-order"/> <!--指定注册中心的位置--> <dubbo:registry address="zookeeper://192.168.31.136:2181"/> <!--调用远程暴露的服务,生成远程服务代理--> <dubbo:reference id="userService" interface="me.nic.service.IUserService"/> </beans>
ProviderRun.java:启动
public class ProviderRun { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:provider.xml"); System.in.read(); } }
2.4 效果
ProviderRun启动之后阻塞,
ConsumerRun启动之后调用orderService.placeOrder(1)
,placeOrder
中远程调用spring-user
模块中的userService.getUserAddById(userId)
获取用户地址:
用户地址:上海市宝山区 下单成功
三、springboot整合dubbo
3.1 boot-common模块:
公共接口层(model,service,exception…),定义公共接口,也可以导入公共依赖
UserAddress.java:用户地址实体类
public class UserAddress implements Serializable { private Integer id; private String userAddress; private String userId; private String consignee; private String phoneNum; private String isDefault; .... }
IOrderService.java:订单接口
public interface IOrderService { /** * 用户下单 * @param userId */ UserAddress placeOrder(int userId); }
IUserService.java:用户接口
public interface IUserService { /** * 根据用户id获取用户地址 * @param userId * @return */ UserAddress getUserAddById(int userId); }
pom.xml:通用的依赖,引入dubbo的starter
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.0</version> <relativePath/> </parent> <groupId>com.example</groupId> <artifactId>boot-common</artifactId> <version>0.0.1-SNAPSHOT</version> <name>boot-common</name> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> </dependencies> </project>
3.2 boot-user模块:
用户业务,作为服务提供者
pom.xml:引入通用模块,可以使用定义的接口
<dependency> <groupId>org.example</groupId> <artifactId>spring-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
UserServiceImpl.xml:IUserService的实现类,供远程调用
@Service
暴露dubbo的服务
@Service @Component public class UserServiceImpl implements IUserService { @Override public UserAddress getUserAddById(int userId) { UserAddress userAddress = new UserAddress(); userAddress.setUserAddress("上海市宝山区"); return userAddress; } }
application.properties:dubbo配置信息
dubbo.application.name=boot-user dubbo.registry.address=192.168.31.136:2181 dubbo.registry.protocol=zookeeper dubbo.protocol.name=dubbo dubbo.protocol.port=20880
BootUserApplication.java:启动:
@EnableDubbo
: 开启基于注解的dubbo功能
@EnableDubbo @SpringBootApplication public class BootUserApplication { public static void main(String[] args) { SpringApplication.run(BootUserApplication.class, args); } }
3.3 boot-order模块:
订单业务,作为服务消费者
pom.xml:引入通用模块,可以使用定义的接口,同1.2
<dependency> <groupId>com.example</groupId> <artifactId>boot-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
OrderServiceImpl.xml:IOrderService的实现类,其中远程调用userService
@Reference
:引用远程提供者服务
@Service public class OrderServiceImpl implements IOrderService { //引用远程提供者服务 @Reference private IUserService userService; @Override public UserAddress placeOrder(int userId) { // 远程调用,获取用户地址 UserAddress userAddById = userService.getUserAddById(userId); return userAddById; } }
OrderController.java:web接口,调用OrderService:
@Controller public class OrderController { @Autowired IOrderService orderService; @RequestMapping("/initOrder") @ResponseBody public UserAddress initOrder(@RequestParam("uid")int userId) { return orderService.placeOrder(userId); } }
application.properties:dubbo配置信息
server.port=8081 dubbo.application.name=boot-order dubbo.registry.address=zookeeper://192.168.31.136:2181
BootOrderApplication.java:启动
@EnableDubbo @SpringBootApplication public class BootOrderApplication { public static void main(String[] args) { SpringApplication.run(BootOrderApplication.class, args); } }
3.4 效果
到此这篇关于springboot整合dubbo详细教程的文章就介绍到这了,更多相关springboot整合dubbo内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!
- 本文固定链接: https://zxbcw.cn/post/213171/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)