一、 服务提供者:
1.dubbo容器启动类
import com.alibaba.dubbo.container.Main;
public class App
{
public static void main( String[] args )
{
Main.main(args);
}
}
2.对外暴露的接口:
public interface IOrderServices {
DoOrderResponse doOrder(DoOrderRequest request);
}
3. 接口的实现类:
public class OrderServiceImpl implements IOrderServices{
@Override
public DoOrderResponse doOrder(DoOrderRequest request) {
System.out.println("曾来过:"+request);
DoOrderResponse response = new DoOrderResponse();
response.setCode("1000");
response.setMemo("处理成功");
return response;
}
}
4.配置文件:
虚拟机上启动四个zookeeper服务
<?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">
<!--name:当前项目在整个分布式架构里面的唯一名称,计算依赖关系的标签 owner表示当前application是由谁维护-->
<dubbo:application name="order-provider" owner="mic"/>
<!--监控时使用-->
<dubbo:monitor protocol="registry"/>
<!--dubbo这个服务所要暴露的服务地址所对应的注册中心,四个zookeeper服务-->
<dubbo:registry protocol="zookeeper" address="192.168.70.63:2181,192.168.70.64:2181,192.168.70.65:2181,192.168.70.66:2181"/>
<!--当前服务发布所依赖的协议:webservice Thrift Hession http-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--服务发布的配置,需要暴露的服务接口-->
<dubbo:service interface="com.gupao.vip.mic.dubbo.order.IOrderServices" ref="orderService"/>
<!--Bean bean定义-->
<bean id="orderService" class="com.gupao.vip.mic.dubbo.order.OrderServiceImpl"/>
</beans>
加入依赖的pom文件
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
二、服务消费者:
1.消费端消费服务端提供的服务(orderService1对应的服务)
public class App
{
public static void main( String[] args ) throws IOException {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("order-consumer.xml");
//获取服务提供者提供的服务 orderService1与消费端dubbo配置文件代理类id一致
IOrderServices services= (IOrderServices)context.getBean("orderService1");
DoOrderRequest request = new DoOrderRequest();
request.setName("fangxinde");
DoOrderResponse response = services.doOrder(request);
System.out.println(response);
System.in.read();
}
}
2.消费端的dubbo配置
<?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">
<!--name:当前项目在整个分布式架构里面的唯一名称,计算依赖关系的标签 owner表示当前application是由谁维护-->
<dubbo:application name="order-provider" owner="mic"/>
<!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
<dubbo:registry address="zookeeper://192.168.70.66:2181?backup=192.168.70.65:2181,192.168.70.64:2181,192.168.70.63:2181"/>
<!--生成一个远程服务的调用代理-->
<dubbo:reference id="orderService1" interface="com.gupao.vip.mic.dubbo.order.IOrderServices"/>
</beans>
3.依赖pom文件(需要引用zookeeper的jar包)
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>