创建服务提供者
1.创建服务接口并编写实现类
2.导入jar包,这里使用maven导入
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.5</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.0.23.Final</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.11</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
3.编写服务配置文件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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="provider" />
<!--以下是三种提供服务的方式-->
<!-- ------------------------------------------------------------------------------------------------------------------ -->
<!-- 使用multicast广播注册中心暴露服务地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<!-- 本地模式,即服务方与消费方在同一台电脑上 -->
<!-- <dubbo:registry address="N/A" /> -->
<!-- zookeeper注册服务,推荐使用 -->
<!-- <dubbo:registry address="zookeeper://192.168.127.99:2181?backup=192.168.127.99:2182,192.168.127.99:2183" /> -->
<dubbo:registry protocol="zookeeper" address="192.168.127.99:2181,192.168.127.99:2182,192.168.127.99:2183" />
<!-- ------------------------------------------------------------------------------------------------------------------ -->
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="cn.demoService.DemoService"
ref="demoService" />
<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="cn.demoService.impl.DemoServiceImpl" />
</beans>
项目结构如下
创建测试服务类
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("provider.xml");
try {
System.in.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
消费方
1.创建与服务方相同的接口,包名也应该相同
2.创建消费方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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="providerss" />
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<!-- <dubbo:registry address="zookeeper://192.168.127.99:2181?backup=192.168.127.99:2182,192.168.127.99:2183" /> -->
<!--zookeeper集群-->
<dubbo:registry protocol="zookeeper" address="192.168.127.99:2181,192.168.127.99:2182,192.168.127.99:2183" />
<!--zookeeper不集群-->
<!-- <dubbo:registry address="zookeeper://192.168.127.99:2181" /> -->
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<!--使用本地-->
<!-- <dubbo:reference id="demoService" interface="cn.customer.DemoService" -->
<!-- url="127.0.0.1:20880" /> -->
<!--使用zookeeper或广播提供服务-->
<dubbo:reference id="demoService1" check="false"
interface="cn.demoService.DemoService" />
</beans>
项目路径
创建测试消费类
public class TestMain {
public static void main(String[] args) {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("customer.xml");
ac.start();
DemoService demoService = (DemoService) ac.getBean("demoService1");
String hello = demoService.getUser("world"); // 执行远程方法
System.out.println(hello); // 显示调用结果
}
}