关于dubbo的介绍以及背景,此处不进行描述,如需了解,可自行百度,亦可参考以下博客:https://blog.csdn.net/noaman_wgs/article/details/70214612
下面直接开始构建项目,在这之前可以先了解一下dubbo官方的入门demo—quick start,百度搜索dubbo官网点击进入quick start或直接在浏览器地址栏输入dubbo.io进入,查看官网的quick start流程。
一、创建MAVEN项目
首先在idea中创建一个maven项目,命名为dubbo,作为父项目,然后在该项目上点击右键new——>new module,选择maven,再创建一个maven项目,命名为dubbo-core,作为子项目,用同样的方法再创建两个子项目,分别命名为dubbo-orders、dubbo-user,其中dubbo-core为抽象对外client,用于存放公共对外接口,dubbo-orders模拟电商平台模块中的订单服务,用于提供远程服务(Provider),dubbo-user模拟会员服务,用于调用远程服务(Consumer),我们今天的demo的场景就是会员服务调用订单服务通过订单号来查询订单信息,项目构建如下所示:
二、引入依赖
首先在dubbo项目的pom.xml文件中引入相关依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dubbo</groupId>
<artifactId>dubbo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>com.dubbo</name>
<modules>
<module>dubbo-core</module>
<module>dubbo-orders</module>
<module>dubbo-user</module>
</modules>
<dependencyManagement>
<dependencies>
<!-- dubbo当前最新版 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<!-- zookeeper客户端 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
其次在dubbo-core项目的pom.xml文件中进行如下配置:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo</artifactId>
<groupId>com.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-core</artifactId>
<packaging>jar</packaging>
<name>dubbo-core</name>
<url>http://maven.apache.org</url>
</project>
然后在dubbo-orders项目的pom.xml文件中引入相关依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo</artifactId>
<groupId>com.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-orders</artifactId>
<packaging>jar</packaging>
<name>dubbo-orders</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.dubbo</groupId>
<artifactId>dubbo-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
</dependencies>
</project>
dubbo-user项目的pom.xml文件与dubbo-orders项目的pom.xml文件配置相似,于是我们在dubbo项目中引入了dubbo和zookeeper,zookeeper是dubbo的注册中心,dubbo-core使用jar的方式打包作为公共接口,以便其它服务进行调用。
三、编写订单查询服务
在dubbo-core项目src-main-java目录下创建一个包,然后在此包中创建一个接口命名为OrderService,作为订单查询服务接口,再写一个query方法:
/**
* Created by TAO on 2019-07-15.
*/
public interface OrderService {
public Orders query(Integer orderId);
}
在这之前我们需要创建一个Orders实体类:
import java.io.Serializable;
/**
* Created by TAO on 2019-07-15.
*/
public class Orders implements Serializable {
private Integer orderId;
private String orderName;
private String goodsName;
private Integer price;
private String buyName;
private String adr;
//get、set、Constructor省略
}
此处实体类需要序列化,不然会报错。
下一步,在dubbo-orders项目src-main-java目录下创建一个包,然后在此包中创建一个类命名为OrderServiceImpl,并实现OrderService接口,然后重写query方法:
import com.hisun.app.pojo.Orders;
/**
* Created by TAO on 2019-07-15.
*/
public class OrderServiceImpl implements OrderService{
@Override
public Orders query(Integer orderId) {
Orders orders = new Orders();
orders.setOrderId(20190715);
if (orderId.equals(orders.getOrderId())){
orders.setAdr("广东深圳");
orders.setBuyName("悟空");
orders.setGoodsName("香蕉");
orders.setPrice(10);
}
return orders;
}
}
四、Provider和Consumer的配置
完成以上步骤后,开始配置Provider和Consumer,首先,在dubbo-orders项目src-main-resource目录下创建一个包,命名为META-INF.spring,在该目录下创建一个xml文件,可将其命名为OrderService.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">
<!-- provider's application name, used for tracing dependency relationship -->
<dubbo:application name="order-provider"/>
<!-- use multicast registry center to export service -->
<dubbo:registry address="multicast://224.5.6.7:1234"/>
<!-- use dubbo protocol to export service on port 20880 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- declare the service interface to be exported -->
<dubbo:service interface="com.***.***.service.OrderService" ref="OrderService"/>
<!-- service implementation, as same as regular local bean -->
<bean id="OrderService" class="com.***.***.service.OrderServiceImpl"/>
</beans>
以上分别配置的是dubbo的应用名称、注册中心、协议和服务,然后在dubbo-orders项目src-test-java目录下创建包,在包中创建一个类,将其命名为Provider.java,并编写如下代码:
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
public static void main(String[] args) throws Exception {
// System.setProperty("java.net.preferIPv4Stack", "true");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"META-INF/spring/OrderService.xml"});
context.start();
// System.out.println("Provider started.");
System.in.read(); // press any key to exit
}
}
这样就完成了Provider的配置了,Consumer的配置和Provider一样,在dubbo-user项目src-main-resource目录下创建一个包,同样命名为META-INF.spring,在该目录下创建一个xml文件,可将其命名为UserService.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">
<!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
don't set it same as provider -->
<dubbo:application name="user-consumer"/>
<!-- use multicast registry center to discover service -->
<dubbo:registry address="multicast://224.5.6.7:1234"/>
<!-- generate proxy for the remote service, then demoService can be used in the same way as the
local regular interface -->
<dubbo:reference id="orderService" check="false" interface="com.hisun.app.service.OrderService"/>
</beans>
然后在dubbo-user项目src-test-java目录下创建包,在包中创建一个类,将其命名为Consumer.java,并编写如下代码:
import com.hisun.app.service.OrderService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Consumer {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/UserService.xml"});
context.start();
// Obtaining a remote service proxy
OrderService orderService = (OrderService)context.getBean("orderService");
// Executing remote methods
// Display the call result
System.out.println(orderService.query(20190715));
}
}
五、运行结果
完成以上配置后,即可开始进行运行测试,首先运行Provider类,如下所示:
Provider运行成功后,再运行Consumer类,如下所示:
至此,大功告成。