DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架,天天为2,000+个服务提供3,000,000,000+次访问量支持,并被普遍应用于阿里巴巴集团的各成员站点。java
http://www.oschina.net/p/dubbospring
1.导入依赖包(dubbo zookeeper zkclient) apache
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.7</version> </dependency>
2.写提供者(服务端)的方法 tomcat
接口:springboot
public interface Hello { public String getString(String name); }
实现类: app
public class HelloImpl implements Hello { @Override public String getString(String name) { return name; } }
3.提供者的spring配置 框架
<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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- dubbo程序名字 -->--> <dubbo:application name="dubbo"/> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry protocol="zookeeper" address="192.168.1.1:4181" username="用户名" password="密码"/> <!-- 用dubbo协议在20880端口暴露服务 --> <bean id="helloimpl" class="com.example.HelloImpl"/> <!--建立实现类的bean--> <dubbo:service interface="com.example.Hello" ref="helloimpl" id="hello"/> <!--将接口暴露给服务中心--> </beans>
4.写消费者(客户端)接口 分布式
这里能够把服务端的接口复制过来就完事,若是你须要提供给其余们人使用,最好不是复制接口,而是打成jar接口包。ide
5.导入和服务端同样的依赖,配置spring配置 性能
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!--名字能够和服务端不一样--> <dubbo:application name="dubbo"/> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry protocol="zookeeper" address="192.168.1.1:4181" username="用户名" password="密码"/> <!-- 用dubbo协议在20880端口暴露服务 --> < dubbo : reference interface = "com.example.Hello" id = "hello" /> <!--复制过来的接口或者jar里的接口 必须和服务端的接口一致 --> </beans>
6.客户端test
public class DemoApplicationTests { @Autowired Hello h; @Test public void test() { String hh = h.getString("dubbo 远程调用成功"); System.out.println(hh); } }
最后说一下不用tomcat启动的方式
springboot集成dubbo
@SpringBootApplication @ImportResource("classpath:/dubbo-provider.xml")//启动加在dubbo配置文件 public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); try { System.out.println("启动成功,按任意键关闭"); System.in.read(); } catch (IOException e) { e.printStackTrace(); } } }
@SpringBootApplication public class DemoApplication { private static final Logger log = LoggerFactory.getLogger(DemoApplication.class); public static void main(String[] args) { try { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:/applicationContext*.xml"); context.start(); } catch (Exception e) { log.error("== DubboProvider context start error:", e); } synchronized (DemoApplication.class) { while (true) { try { DemoApplication.class.wait(); } catch (InterruptedException e) { log.error("== synchronized error:", e); } } } } }