4、dubbo+zookeeper的helloWorld测试
需求:
打开IDEA,创建一个空工程
在该项目中新建模块,创建一个简单的maven工程(服务接口模块)
编写接口和bean
UserAddress.java
/** * 用户地址 * @author lfy * */ public class UserAddress implements Serializable { private Integer id; private String userAddress; //用户地址 private String userId; //用户id private String consignee; //收货人 private String phoneNum; //电话号码 private String isDefault; //是否为默认地址 Y-是 N-否 ...... }
OrderService.java
public interface OrderService { /** * 初始化订单 * @param userId */ public List<UserAddress> initOrder(String userId); }
UserService.java
/** * 用户服务 * @author lfy * */ public interface UserService { /** * 按照用户id返回所有的收货地址 * @param userId * @return */ public List<UserAddress> getUserAddressList(String userId); }
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.atguigu.gmall</groupId> <artifactId>gmall-interface</artifactId> <version>1.0-SNAPSHOT</version> </project>
再新建模块,创建一个简单的maven工程(服务提供者)
在该模块中配置pom.xml文件、UserService接口的实现类UserServiceImpl、spring主配置文件、主程序类,路径如如图
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.atguigu.gmall</groupId> <artifactId>user-service-provider</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!--引入需要远程调用方法的接口和bean--> <dependency> <groupId>com.atguigu.gmall</groupId> <artifactId>gmall-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!--引入dubbo--> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency> <!--注册中心使用的是zookeeper,引入操作zookeeper的客户端 dubbo2.6以前的版本引入zkclient操作zookeeper dubbo2.6之后的版本引入curator操作zookeeper--> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.12.0</version> </dependency> </dependencies> <build> <resources> <!--IDEA默认不把xml等其它文件加入到target目录下--> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> <include>**/*.tld</include> <include>**/*.xls</include> <include>**/*.xlsx</include> </includes> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.tld</include> <include>**/*.xls</include> <include>**/*.xlsx</include> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>
UserServiceImpl.java
public class UserServiceImpl implements UserService { @Override public List<UserAddress> getUserAddressList(String userId) { //这里只是为了测试dubbo,所以就不连接数据库了 UserAddress address1 = new UserAddress(1, "北京市昌平区宏福科技园综合楼3层", "1", "李老师", "010-56253825", "Y"); UserAddress address2 = new UserAddress(2, "深圳市宝安区西部硅谷大厦B座3层(深圳分校)", "1", "王老师", "010-56253825", "N"); return Arrays.asList(address1,address2); } }
provider.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <!--1、指定服务/应用的名字(同样的服务名字相同,不要和别的服务同名)--> <dubbo:application name="user-service-provider"></dubbo:application> <!--2、指定注册中心的位置--> <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> <!--或者是用下面的这种方式指定注册中心--> <!--<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>--> <!--3、指定通信规则(通信协议、通信端口)--> <dubbo:protocol name="dubbo" port="20080"></dubbo:protocol> <!--4、暴露服务 ref:指向服务的真正的实现对象--> <dubbo:service interface="com.atguigu.gmall.service.UserService" ref="userServiceImpl"></dubbo:service> <!--服务的实现--> <bean name="userServiceImpl" class="com.atguigu.gmall.service.impl.UserServiceImpl"></bean> </beans>
MainApplication.java
public class MainApplication { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:provider.xml"); applicationContext.start(); //为了该程序不终止,在这写一个读取方法使阻塞,监控中心就能看到服务提供者是开启状态 System.in.read(); } }
运行主程序之前确保zookeeper服务端是运行状态
运行主程序,运行状态期间,配置的服务会注册到zookeeper中
想要看到我们注册的服务此时就要用到监控中心了
开启监控中心服务
进入网页的监控中心可以看到有一个服务
如下图点击可以查看提供者的信息
再创建一个模块,也是建一个简单的maven工程(服务消费者)
在该模块中配置pom.xml文件、OrderService接口的实现类OrderServiceImpl.java、spring主配置文件、主程序类,路径如如图
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.atguigu.gmall</groupId> <artifactId>order-service-consumer</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.atguigu.gmall</groupId> <artifactId>gmall-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!--引入dubbo--> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency> <!--注册中心使用的是zookeeper,引入操作zookeeper的客户端 dubbo2.6以前的版本引入zkclient操作zookeeper dubbo2.6之后的版本引入curator操作zookeeper--> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.12.0</version> </dependency> </dependencies> <build> <resources> <!--IDEA默认不把xml等其它文件加入到target目录下--> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> <include>**/*.tld</include> <include>**/*.xls</include> <include>**/*.xlsx</include> </includes> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.tld</include> <include>**/*.xls</include> <include>**/*.xlsx</include> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>
OrderServiceImpl.java
/** * 1、将服务提供者注册到注册中心(暴露服务) * 1)、导入dubbo依赖(2.6.2)\操作zookeeper的客户端(curator) * 2)、配置服务提供者 * * 2、让服务消费者去注册中心订阅服务提供者的服务地址 * */ @Service public class OrderServiceImpl implements OrderService { @Autowired UserService userService; @Override public List<UserAddress> initOrder(String userId) { System.out.println("用户id:"+userId); //1、查询用户的收货地址 List<UserAddress> addressList = userService.getUserAddressList(userId); for (UserAddress userAddress : addressList) { System.out.println(userAddress.getUserAddress()); } return addressList; } }
consumer.xml(spring主配置类)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--spring的注解包扫描配置--> <context:component-scan base-package="com.atguigu.gmall.service"></context:component-scan> <!--1、指定服务/应用的名字(同样的服务名字相同,不要和别的服务同名)--> <dubbo:application name="order-service-consumer"/> <!--2、指定注册中心的位置--> <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> <!--3、声明需要调用远程服务的接口;生成远程服务代理--> <dubbo:reference id="userService" interface="com.atguigu.gmall.service.UserService"/> </beans>
MainApplication.java(主程序类)
public class MainApplication { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml"); //通过接口获取实现类 OrderService orderService = applicationContext.getBean(OrderService.class); //远程调用 orderService.initOrder("1"); System.out.println("调用完成"); //为了该程序不终止,在这写一个读取方法使阻塞,监控中心就能看到服务消费者是开启状态 System.in.read(); } }
运行主程序类(想要调用远程服务必须保证服务提供者是运行状态)
控制台打印
因为程序还没有停止,所以监控中心可以看到我们有两个应用,一个提供者一个消费者
看到以上这些说明远程调用成功