简单环境搭建
搭建工具
jdk1.8+idea+maven+spring
构建多模块
dubbo_par 父模块
父模块中pom.xml引入dubbo坐标
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.6</version></dependency>
dubbo_api 服务公共接口
实体类 简单模拟数据库
public class User implements Serializable { private static final long serialVersionUID = -6267484068304517004L; private Integer userId; private String uname; }
定义服务接口
public interface IUserService { //通过id 查询用户 public User queryUserById(Integer userId); }
dubbo_provider 服务提供方
pom.xml中引入公共服务接口坐标
<!--引入公共接口坐标--> <dependency> <groupId>com.shsxt</groupId> <artifactId>dubbo_demo_api</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
配置provider.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--自动扫描--> <context:component-scan base-package="com.xpx.provider"></context:component-scan> <!--dubbo配置--> <!--配置名称--> <dubbo:application name="dubbo_provider"></dubbo:application> <!--multicast 注册中心 address="multicast://224.5.6.7:1234" 广播地址 接受广播来实现服务注册--> <dubbo:registry address="multicast://224.5.6.7:1234"></dubbo:registry> <!--配置协议 20880dubbo的默认端口--> <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol> <!--配置注册服务--> <dubbo:service interface="com.xpx.api.service.IUserService" ref="userServiceImpl"></dubbo:service> </beans>
服务接口实现
@Service public class UserServiceImpl implements IUserService { private Map<Integer,User> users ; public UserServiceImpl() { users = new HashMap<>(); users.put(1,new User(1,"dubbo")); } @Override public User queryUserById(Integer userId) { return users.get(userId); } }
测试提供服务方
public class Publisher { public static void main(String args[]) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml"); context.start(); System.in.read(); } }
dubbo_consumer 消费服务方
pom.xml中引入公共服务接口坐标
<!--引入公共接口坐标--> <dependency> <groupId>com.shsxt</groupId> <artifactId>dubbo_demo_api</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
配置consumer.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应用名称--> <dubbo:application name="dubbo_consumer"></dubbo:application> <!--注册中心 address="multicast://224.5.6.7:1234" 广播地址 接受广播来实现服务注册--> <dubbo:registry address="multicast://224.5.6.7:1234"></dubbo:registry> <!--配置订阅的服务--> <dubbo:reference interface="com.xpx.api.service.IUserService" id="userService"></dubbo:reference> <bean class="com.xpx.consumer.controller.UserController" id="userController"> <property name="userService" ref="userService"></property> </bean> </beans>
controller
public class UserController { private UserService userService; // 没有配置自动扫描用set注入 public void setUserService(UserService userService) { this.userService = userService; } public User querUserById(Integer userId){ return userService.queryUserById(userId); } }
测试
public class TestConsumer { public static void main(String args[]){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml"); UserController userController = (UserController) applicationContext.getBean("userController"); User user = userController.querUserById(1); System.out.println(user); } }
结果成功
User{id=1, uname=‘dubbo‘}
原文地址:https://www.cnblogs.com/xpx951201/p/10225253.html
时间: 2024-10-30 11:30:44