一、原理
dubbo官网:http://dubbo.io/
Dubbo offers three key functionalities, which include interface based remote call, fault tolerance & load balancing, and automatic service registration & discovery.
Dubbo有三个主要功能
1、远程调用
2、PRC:容错机制和负载均衡
3、服务目录框架:用于服务的注册和服务事件发布和订阅
源码地址:https://github.com/jxq0816/dubbo_demo
二、dubbo项目组织结构
三、编码
1、dubbo-demo
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.weeking</groupId>
<artifactId>dubbo-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>dubbo-server</module>
<module>dubbo-client</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- dubbo begin -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- dubbo end -->
<!-- 注册中心zookeeper begin -->
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.5.3-beta</version>
<type>pom</type>
</dependency>
<!--A zookeeper client, that makes life a little easier.-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.8</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2. dubbo-server
① 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>dubbo</groupId>
<artifactId>dubbo-server</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>com.weeking</groupId>
<artifactId>dubbo-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<!-- spring end -->
<!-- dubbo begin -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!-- dubbo end -->
<!-- 注册中心zookeeper begin -->
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<type>pom</type>
</dependency>
<!-- 注册中心zookeeper end -->
<!-- The Netty project is an effort to provide an asynchronous event-driven network application framework
and tools for rapid development of maintainable high performance and high scalability protocol servers and clients.
In other words, Netty is a NIO client server framework which enables quick and easy development of network applications
such as protocol servers and clients.
It greatly simplifies and streamlines network programming such as TCP and UDP socket server.
-->
<dependency>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
<version>3.2.0.Final</version>
</dependency>
<!--A zookeeper client, that makes life a little easier.-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
</dependencies>
</project>
② ServerMain
package com.jxq.main;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
/**
* Created by jiangxingqi on 2017/2/8.
*/
public class ServerMain {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationProvider.xml" });
context.start();
System.out.println("输入任意按键退出 ~ ");
System.in.read();
context.close();
}
}
③ DubboService
package com.jxq.service;
/**
* Created by jiangxingqi on 2017/2/8.
*/
public interface DubboService {
String sayHello(String name);
}
④DubboServiceImpl
package com.jxq.service.impl;
import com.jxq.service.DubboService;
/**
* Created by jiangxingqi on 2017/2/8.
*/
public class DubboServiceImpl implements DubboService {
public String sayHello(String name) {
System.out.println("init : " + name);
return "hello " + name;
}
}
⑤ applicationProvider.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-demo" />
<!-- zookeeper注册中心 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:protocol name="dubbo" port="20880" />
<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.jxq.service.impl.DubboServiceImpl" />
<!-- 向注册中心注册暴漏服务地址,注册服务 -->
<dubbo:service interface="com.jxq.service.DubboService"
ref="demoService" executes="10" />
</beans>
3. dubbo-client
①ClientMain
package com.jxq.service;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by jiangxingqi on 2017/2/8.
*/
public class ClientMain {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationConsumer.xml" });
context.start();
DubboService service = (DubboService) context.getBean("demoService");
System.out.println(service.sayHello("world"));
context.close();
}
}
②applicationConsumer.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="consumer-of-dubbo-demo" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 向注册中心订阅服务 -->
<dubbo:reference id="demoService" interface="com.jxq.service.DubboService" />
</beans>
四、联调
启动zookeep
consumer请求server所提供的sayHello接口
1、run ServerMain 启动服务提供方
2、run ClientMain 启动消费方