项目的结构

项目搭建过程:https://blog.csdn.net/dengjili/article/details/86612940
项目地址:https://github.com/dengjili/dubbo-demo

dubbo-demo
----dubbo-order
--------dubbo-order-api
--------dubbo-order-provider
----dubbo-user
--------dubbo-user-api
--------dubbo-user-provider
----dubbo-xxx-client

例子演示中的调用关系

在这里插入图片描述

registry注册中心

注册中心由于zookeeper提供
提供ip:192.168.1.103
端口:2181

启动

[root@localhost bin]# ps -ef | grep zookeeper
root      2906     1  1 19:51 pts/0    00:00:04 /app/jdk1.8.0_191/bin/java -Dzookeeper.log.dir=. -Dzookeeper.root.logger=INFO,CONSOLE -cp /app/zookeeper-standalone/bin/../build/classes:/app/zookeeper-standalone/bin/../build/lib/*.jar:/app/zookeeper-standalone/bin/../lib/slf4j-log4j12-1.7.25.jar:/app/zookeeper-standalone/bin/../lib/slf4j-api-1.7.25.jar:/app/zookeeper-standalone/bin/../lib/netty-3.10.6.Final.jar:/app/zookeeper-standalone/bin/../lib/log4j-1.2.17.jar:/app/zookeeper-standalone/bin/../lib/jline-0.9.94.jar:/app/zookeeper-standalone/bin/../lib/audience-annotations-0.5.0.jar:/app/zookeeper-standalone/bin/../zookeeper-3.4.13.jar:/app/zookeeper-standalone/bin/../src/java/lib/*.jar:/app/zookeeper-standalone/bin/../conf:.:/app/jdk1.8.0_191/jre/lib/rt.jar:/app/jdk1.8.0_191/lib/dt.jar:/app/jdk1.8.0_191/lib/tools.jar -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false org.apache.zookeeper.server.quorum.QuorumPeerMain /app/zookeeper-standalone/bin/../conf/zoo.cfg
root      2961  2876  0 19:58 pts/0    00:00:00 grep --color=auto zookeeper
[root@localhost bin]# sh zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /app/zookeeper-standalone/bin/../conf/zoo.cfg
Mode: standalone

provider服务中心

核心代码

接口

public interface IOrderService {
	OrderResponse<String> doOrder(OrderRequest<String> request);
}

实现

public class OrderServiceImpl implements IOrderService {
	@Override
	public OrderResponse<String> doOrder(OrderRequest<String> request) {
		System.out.println("接收到请求:" + request);
		OrderResponse<String> response = new OrderResponse<String>();
		response.setCode("0000");
		response.setDesc("成功");
		response.setData("1111111111111");
		return response;
	}
}

辅助类

public class OrderRequest<T> implements Serializable {
	/**
	 * 网络调用需要序列化
	 */
	private static final long serialVersionUID = 2279590428209541954L;
	
	private T data;
	
	public T getData() {
		return data;
	}
	public void setData(T data) {
		this.data = data;
	}
	@Override
	public String toString() {
		return "OrderRequest [data=" + data + "]";
	}
}

public class OrderResponse<T> implements Serializable {
	/**
	 * 网络调用需要序列化
	 */
	private static final long serialVersionUID = 2279590428209541954L;
	
	private String code;
	private String desc;
	private T data;
	
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
	public T getData() {
		return data;
	}
	public void setData(T data) {
		this.data = data;
	}
	@Override
	public String toString() {
		return "OrderResponse [code=" + code + ", desc=" + desc + ", data=" + data + "]";
	}
}

服务配置

特殊注意事项,配置文件应该放在META-INF/spring下面
在这里插入图片描述

<?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="order-provider-app" owner="order-test" />  
    <!-- 使用zookeeper注册中心 -->
    <dubbo:registry address="zookeeper://192.168.1.103:2181?client=zkclient" client="zkclient"/>
    <!-- rpc协议 -->
    <dubbo:protocol name="dubbo" port="20880" />  
    <!-- 对外暴露接口 -->
    <dubbo:service interface="org.dubbo.order.api.IOrderService" ref="orderService" />  
    <!-- 实例化对象 -->
    <bean id="orderService" class="org.dubbo.order.provider.OrderServiceImpl" />
</beans>
启动服务
public class App {
	public static void main(String[] args) {
		Main.main(args);
	}
}

核查zookeeper注册中心是否注册成功
在这里插入图片描述

consumer消费中心

核心代码

消费代码

public class App 
{
	public static void main( String[] args )
    {
    	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("order-consumer.xml");
        // 订单调用
    	IOrderService orderService = applicationContext.getBean(IOrderService.class);
    	
    	OrderRequest<String> request = new OrderRequest<>();
    	request.setData("get order sequence");
    	OrderResponse<String> orderResponse = orderService.doOrder(request);
    	System.out.println(orderResponse);
    }
}
服务配置
<?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="order-consumer-app" owner="client-test" />  
    <!-- 使用zookeeper注册中心 -->
    <dubbo:registry address="zookeeper://192.168.1.103:2181?client=zkclient" client="zkclient"/>
    <!-- rpc协议 -->
    <dubbo:protocol name="dubbo" port="20880" />  
    <!-- 远程对象代理 -->
   	<dubbo:reference id="orderService" interface="org.dubbo.order.api.IOrderService" />
</beans>
测试结果

客户端
在这里插入图片描述

服务端

在这里插入图片描述

测试通过

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐