提供者
接口
public interface SampleService { String sayHello(String name); public List getUsers(); }
接口实现
public class SampleServiceImpl implements SampleService { public String sayHello(String name) { return "Hello " + name; } public List getUsers() { List list = new ArrayList(); User u1 = new User(); u1.setName("jack"); u1.setAge(20); u1.setSex("m"); User u2 = new User(); u2.setName("tom"); u2.setAge(21); u2.setSex("m"); User u3 = new User(); u3.setName("rose"); u3.setAge(19); u3.setSex("w"); list.add(u1); list.add(u2); list.add(u3); return list; } }
sample-provider.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 --> <bean id="sampleService" class="bhz.dubbo.sample.provider.impl.SampleServiceImpl" /> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="sample-provider" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://192.168.175.3:2181?backup=192.168.175.4:2181,192.168.175.4:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 写操作可以设置retries=0 避免重复调用SOA服务 --> <dubbo:service retries="0" interface="bhz.dubbo.sample.provider.SampleService" ref="sampleService" /> </beans>
启动测试
public class Provider { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "sample-provider.xml" }); context.start(); System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟 } }
测试结果
消费者
接口
public interface SampleService { String sayHello(String name); public List getUsers(); }
sample-consumer.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="sample-consumer" /> <dubbo:registry address="zookeeper://192.168.175.3:2181?backup=192.168.175.4:2181,192.168.175.4:2181" /> <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService 检查级联依赖关系 默认为true 当有依赖服务的时候,需要根据需求进行设置 --> <dubbo:reference id="sampleService" check="false" interface="bhz.dubbo.sample.provider.SampleService" /> </beans>
测试
public class Consumer { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "sample-consumer.xml" }); context.start(); SampleService sampleService = (SampleService) context.getBean("sampleService"); String hello = sampleService.sayHello("tom"); System.out.println(hello); System.in.read(); } }
测试结果
Xml文件说明
生产者:
注册中心Zookeeper配置:
单机:
<dubbo:registry address="zookeeper://192.168.175.3:2181" />
集群:
<dubbo:registry address="zookeeper://192.168.175.3:2181?backup=192.168.175.4:2181,192.168.175.5:2181" />
或
<dubbo:registry protocol="zookeeper" address="192.168.175.3:2181,192.168.175.4:2181,192.168.175.5:2181" />
同一Zookeeper,分成多组注册中心:
<dubbo:registry id="chinaRegistry" protocol="zookeeper" address="192.168.175.3:2181" group="china" />
<dubbo:registry id="intlRegistry" protocol="zookeeper" address="192.168.175.3:2181" group="intl" />
时间: 2024-10-29 19:05:32