服务提供者:
此进程有两个模块,一个模块是实现向外暴露的接口,一个模块是实现此接口的实现类:
ideal中编制order-api模块,提供向外的接口
public interface IOrderServices {
DoOrderResponse doOrder(DoOrderRequest request);
}
服务提供者order-provider模块:向外接口的实现类,具体的业务实现
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;
}
}
服务提供者模块下配置文件:具体说明如下
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">
服务提供者启动类:
import com.alibaba.dubbo.container.Main;
public class App
{
public static void main( String[] args )
{
Main.main(args);
}
}
启动dubbo服务
信息: [DUBBO] Export dubbo service com.gupao.vip.mic.dubbo.order.IOrderServices to url dubbo://192.168.19.56:20880/com.gupao.vip.mic.dubbo.order.IOrderServices?anyhost=true&application=order-provider&bind.ip=192.168.19.56&bind.port=20880&dubbo=2.6.2&generic=false&interface=com.gupao.vip.mic.dubbo.order.IOrderServices&methods=doOrder&owner=mic&pid=3400&side=provider×tamp=1550021134459, dubbo version: 2.6.2, current host: 192.168.19.56
二月 13, 2019 9:25:35 上午 com.alibaba.dubbo.remoting.transport.AbstractServer info
信息: [DUBBO] Start NettyServer bind /0.0.0.0:20880, export /192.168.19.56:20880, dubbo version: 2.6.2, current host: 192.168.19.56
二月 13, 2019 9:25:35 上午 com.alibaba.dubbo.container.Main info
信息: [DUBBO] Dubbo SpringContainer started!, dubbo version: 2.6.2, current host: 192.168.19.56
[2019-02-13 09:25:35] Dubbo service server started!
运行结果中显示了向外暴露服务的地址
dubbo://192.168.19.56:20880/com.gupao.vip.mic.dubbo.order.IOrderServices
服务消费者:
dubbo配置文件:
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">
url="dubbo://192.168.19.56:20880/com.gupao.vip.mic.dubbo.order.IOrderServices"
/>
获取服务,打印结果:
public class App
{
public static void main( String[] args )
{
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("order-consumer.xml");
//获取服务提供者提供的服务
IOrderServices services= (IOrderServices)context.getBean("orderService1");
DoOrderRequest request = new DoOrderRequest();
request.setName("mic");
DoOrderResponse response = services.doOrder(request);
System.out.println(response);
}
}
结果为:
DoOrderResponse{data=null, code='1000', memo='处理成功'}