Dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。现在已经是apache基金会的顶级开源项目。
总体架构
系统环境:ubuntu 16.04
apt-get install zookeeper service zookeeper start查看zookeeper进程
ps aux | grep zookeeper
zookeeper启动成功
pom.xml依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency>服务提供方的接口 :
HelloWorld.java
package com.zhuyun.dubbo; public interface HelloWorld { public String run(); }
接口实现类:
HelloWorldImpl.java
package com.zhuyun.dubbo.impl; import org.springframework.stereotype.Component; import com.zhuyun.dubbo.HelloWorld; @Component("helloWorld") public class HelloWorldImpl implements HelloWorld{ public String run(){ return "Hello world!"; } }
spring配置文件:
applicationContext.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:p="http://www.springframework.org/schema/p" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:component-scan base-package="com.zhuyun"></context:component-scan> <dubbo:application name="dubboProvider" /> <!-- registry address, used for consumer to discover services --> <dubbo:registry protocol="zookeeper" address="192.168.10.15:2181" /> <dubbo:consumer timeout="50000" /> <!-- which service to consume? --> <!-- 具体的实现bean --> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" host="192.168.10.200" port="20888" threads="1000" queues="1000" accepts="1000"/> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.zhuyun.dubbo.HelloWorld" ref="helloWorld" actives="1000" executes="1000" /> </beans>
Main方法:
Main.java
package com.zhuyun.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) throws Exception { ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml"); while (true) { System.in.read(); } } }消费方需要调用的接口需要跟提供者的一致(包括包名和类名):
HelloWorld.java
package com.zhuyun.dubbo; public interface HelloWorld { public String run(); }
spring配置文件:
applicationContext.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:p="http://www.springframework.org/schema/p" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"> <dubbo:application name="dubboConsumer" /> <!-- registry address, used for consumer to discover services --> <dubbo:registry protocol="zookeeper" address="192.168.10.15:2181" /> <dubbo:consumer timeout="50000" /> <!-- which service to consume? --> <dubbo:reference id="helloWorld" interface="com.zhuyun.dubbo.HelloWorld" check="false" sent="true"/> </beans>
Main方法:
Main.java
package com.zhuyun.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zhuyun.dubbo.HelloWorld; import com.zhuyun.user.dubbo.UserController; public class Main { public static void main(String[] args) { ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloWorld helloWorld = (HelloWorld) act.getBean("helloWorld"); System.out.println(helloWorld.run()); } }
provider:
consumer: