先安装zookeeper,这里不解说
1、下载dubbo-admin-2.5.4.war 解压到tomcat root下
2、接口(单独打包)
public interface ZkService {
public String hello(String name);
}
3、实现
public class ZkServiceImpl implements ZkService{
@Override
public String hello(String name) {
// TODO Auto-generated method stub
return "hello "+name;
}
}
4、pom配置
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
<dependency>
<groupId>registryService</groupId>
<artifactId>registryService</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
5、提供者
application-Producror.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="productor"/>
<!-- 用zookeeper注册服务中心暴露服务地址 -->
<dubbo:registry protocol="zookeeper" address="zookeeper://192.168.247.128:2181?backup=192.168.247.129:2181"/>
<!-- 暴露dubbo的通信端口 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 给消费者提供服务的接口 -->
<dubbo:service ref="dubboProvider" interface="com.dubbo.zkservice.ZkService"></dubbo:service>
<!-- 提供服务的实现类 -->
<bean id="dubboProvider" class="org.hadoop.dubbo.ZkServiceImpl"></bean>
</beans>
java代码
public class DubboCustomerStart {
public static void main(String[] args) throws InterruptedException{
ClassPathXmlApplicationContext cfig = new ClassPathXmlApplicationContext("classpath:application-customer.xml");
ZkService test = (ZkService) cfig.getBean("dubboProvider");
System.out.println("消费者调用服务方接口开始");
String value= test.hello("李四");
System.out.println("value="+value);
Thread.sleep(3000);
System.out.println("消费者调用服务方接口结束");
}
}
6、消费者
application-customer.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="customer"/>
<!-- 用zookeeper注册服务中心发现服务地址 -->
<dubbo:registry protocol="zookeeoer" address="zookeeper://192.168.247.128:2181?backup=192.168.247.129:2181" check="false"/>
<!-- 调用远程的接口 -->
<dubbo:reference id="dubboProvider" interface="com.dubbo.zkservice.ZkService"/>
</beans>
<!--
说明:
dubbo:reference 的一些属性的说明:
1)interface调用的服务接口
2)check 启动时检查提供者是否存在,true报错,false忽略
3)registry 从指定注册中心注册获取服务列表,在多个注册中心时使用,值为<dubbo:registry>的id属性,多个注册中心ID用逗号分隔
4)loadbalance 负载均衡策略,可选值:random,roundrobin,leastactive,分别表示:随机,轮循,最少活跃调用
-->
java 代码
public class DubboCustomerStart {
public static void main(String[] args) throws InterruptedException{
ClassPathXmlApplicationContext cfig = new ClassPathXmlApplicationContext("classpath:application-customer.xml");
ZkService test = (ZkService) cfig.getBean("dubboProvider");
System.out.println("消费者调用服务方接口开始");
String value= test.hello("李四");
System.out.println("value="+value);
Thread.sleep(3000);
System.out.println("消费者调用服务方接口结束");
}
}
查看dubbo(根据你们自己的dubbo部署情况查看)
浏览192.168.247.128:8080