一: 新建一个java工程p1,里面创建一个接口ISomeService。 意思是某服务
1 接口
package com.way;
public interface ISomeService {
String doSomething();
}
2 pom文件
<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.way.app</groupId>
<artifactId>p1</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
二:新建java工程p2, 里面有一个实现类(对p1中的那个接口)
1 server类
package com.way;
public class SomeService implements ISomeService{
public String doSomething() {
System.out.println("is doing something");
return "something";
}
}
2 spring 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
">
<!-- 服务的实现类 -->
<bean id="someService" class="com.way.SomeService"/>
<!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->
<dubbo:application name="dubbo_provider"></dubbo:application>
<!-- 使用zookeeper注册中心暴露服务地址
属性:
protocol: zookeeper表示注册中心用zk。
address:注册中心的地址,如果是集群,那么用逗号隔开
client:于注册中心的连接,用何种方式。
check:当注册中心不存在时,是否报错。
subscribe:如果为false,表示不订阅 (注册中心的内容),只注册
(因为是 服务提供者,默认是不订阅的(消费者才订阅))
timeout:延时(与注册中心的通信容许的最大时间 ,单位毫秒)
-->
<!--<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" client="zkclient" /> -->
<dubbo:registry id="registry1" protocol="zookeeper" address="127.0.0.1:2181,127.0.0.1:2183,127.0.0.1:2183" client="zkclient"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 要暴露的服务接口
interface:完整的接口包名+类名
registry:指明用哪个注册中心,比如对应上面的registry1
ps:可以有多个注册中心
cluster="failsafe" 默认是failover 意思是失败后,尝试其他服务
-->
<dubbo:service interface="com.way.ISomeService" ref="someService" cluster="failover" loadbalance="roundrobin"/>
</beans>
3 pom文件
<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.way.app</groupId>
<artifactId>p2</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- Spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.way.app</groupId>
<artifactId>p1</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- dubbo zk -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.4.9</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
</project>
4 启动此服务
package com.way;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PrividerTestMain {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"spring-bean.xml"});
context.start();
System.out.println("提供者正在Press any key to exit.");
System.in.read();
}
}
三 新建java工程p3,它是消费者。使用p2提供的服务。
1 消费类
package com.way;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ConsumerThd implements Runnable {
/*
* @see java.lang.Runnable#run()
*/
public void run() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"spring-bean.xml"});
context.start();
ISomeService someService = (ISomeService)context.getBean("someService2");
String result = someService.doSomething();
System.out.println("消费者获得的结果:"+result);
try {
Thread.sleep(1000*20);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() );
}
public static void main(String[] args) {
new Thread(new ConsumerThd()).start();
/**
* 输出结果:
* Thread-0 Finished:nihao
*/
}
}
2 spring 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:application>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181,127.0.0.1:2183,127.0.0.1:2183" check="false" client="zkclient"></dubbo:registry>
<!-- 要引用的服务 这个someService2 取名加个2是为了区别 提供者定义的那个someService.
这个在消费者使用,就是一个以往的spring bean id
-->
<dubbo:reference interface="com.way.ISomeService" id="someService2" ></dubbo:reference>
</beans>
3 pom文件
<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.way.app</groupId>
<artifactId>p3</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- Spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.way.app</groupId>
<artifactId>p1</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- dubbo zk -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.4.9</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
</project>
四 测试
1 运行PrividerTestMain
2 运行ConsumerThd
发现p2的后台打印出了“is doing something”
p3的后台打印出“消费者获得的结果:something”
成功。
五 集群与负载均衡 说明
在提供者中,默认是有属性cluster=”failover”的,即 什么不配置的话,也是有集群的,策略是failover,意思是访问一个节点的服务,失败后,尝试其他节点的同一服务。
负载均衡,使用属性 loadbalance=”roundrobin”,值可以是轮询(roundrobin),随机(random)等,
以轮询举例,假如有3个节点作为服务提供者,第一次调用时访问A节点,那么下一次使用另一个B节点。
六 其他
1 本例的消费者,是用java的main方法,正常使用时,完全可以在spring mvc 等mvc框架的logic类中,获得远程service,实现RPC调用。
2 dubbo不适用大文件传输。
参考:http://blog.csdn.net/congcong68/article/details/41113239
官网: http://dubbo.io/User+Guide-zh.htm