Dubbo的原理
Dubbo的使用
【注册服务】
1. 准备一个提供方工程
服务提供者接口
public interface IStuService { String queryNameById(Integer id); }
接口的实现类
public class StuServiceImpl implements IStuService { public String queryNameById(Integer id) { System.out.println("服务提供方获取到参数:" + id); return "小王八"; } }
2. 导入dubbo依赖
<!-- dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.8</version> </dependency> <!-- 操作zookeeper的依赖 --> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency>
3. 配置spring的配置文件applicationContext-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 -->
<!-- 配置应用的名称 --> <dubbo:application name="dubbo-provider"/> <!-- 配置zookeeper的地址 --> <dubbo:registry address="zookeeper://192.168.227.130:2181"/> <!-- 配置注册的服务 --> <dubbo:service interface="com.qf.service.IStuService" ref="stuService"/> <bean id="stuService" class="com.qf.service.impl.StuServiceImpl"></bean> </beans>
4. 启动spring容器,注册服务(demo)
public class Main { public static void main(String[] args) { //手动初始化Spring容器 ClassPathXmlApplicationContext application = new ClassPathXmlApplicationContext("applicationContext-dubbo.xml"); //一旦spring容器启动成功,服务就会自动注册到注册中心上 //写一个死循环 while(true){ } } }
5. 检测服务
【发现与消费服务】
1. 创建一个服务的消费者
2. 导入dubbo依赖
<!-- dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.8</version> </dependency> <!-- 操作zookeeper的依赖 --> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency>
3. 配置applicationContext-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:application name="dubbo-consumer"/> <!-- 配置服务发现的地址 --> <dubbo:registry address="zookeeper://192.168.227.130:2181"/> <!-- 指定需要发现的服务名称 --> <dubbo:reference id="stuService" interface="com.qf.service.IStuService"/> </beans>
4.定义main方法,消费服务
public class MainConsumer { public static void main(String[] args) { //初始化Spring容器 - 服务就会自动发现 ClassPathXmlApplicationContext application = new ClassPathXmlApplicationContext("applicationContext-dubbo.xml"); IStuService stu = application.getBean(IStuService.class); String name = stu.queryNameById(10); System.out.println("消费者获得的返回值:" + name); while(true){ } } }
至此,一个Dubbo项目Demo就搭建好了。