场景
Dubbo简介与基本概念:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103555224
Dubbo环境搭建-ZooKeeper注册中心:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103555470
Dubbo环境搭建-管理控制台dubbo-admin实现服务监控:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103624846
在上面搭建好ZooKeeper注册中心和管理控制台dubbo-admin搭建后,实现HelloWorld程序完成基本入门。
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
为了解决依赖问题与实现微服务的概念,下面需要新建三个Maven Project
gmall-interface 用来作为公共依赖,存放公共接口和公共Bean等。
uesr-service-provider 搭建服务提供者
order-service-consumer 搭建服务消费者
新建公共接口程序
打开Eclipse-新建maven Project
输入相关坐标信息,后面两个项目过程也是如此。
然后在项目下新建包com.badao.gmall
然后再新建bean包和service包
bean中新建UserAddress实体类
package com.badao.gmall.bean;
import java.io.Serializable;
/**
* 用户地址
* @author badao
*
*/
public class UserAddress implements Serializable {
private Integer id;
private String userAddress; //用户地址
private String userId; //用户id
private String consignee; //收货人
private String phoneNum; //电话号码
private String isDefault; //是否为默认地址 Y-是 N-否
public UserAddress() {
super();
// TODO Auto-generated constructor stub
}
public UserAddress(Integer id, String userAddress, String userId, String consignee, String phoneNum,
String isDefault) {
super();
this.id = id;
this.userAddress = userAddress;
this.userId = userId;
this.consignee = consignee;
this.phoneNum = phoneNum;
this.isDefault = isDefault;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserAddress() {
return userAddress;
}
public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getConsignee() {
return consignee;
}
public void setConsignee(String consignee) {
this.consignee = consignee;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public String getIsDefault() {
return isDefault;
}
public void setIsDefault(String isDefault) {
this.isDefault = isDefault;
}
}
然后在service包下新建两个公共接口类
OrderService.java
package com.badao.gmall.service;
import java.util.List;
import com.badao.gmall.bean.UserAddress;
public interface OrderService {
/**
* 初始化订单
* @param userId
*/
public List<UserAddress> initOrder(String userId);
}
该接口声明一个初始化订单的方法。
UserService.java
package com.badao.gmall.service;
import java.util.List;
import com.badao.gmall.bean.UserAddress;
/**
* 用户服务
* @author badao
*
*/
public interface UserService {
/**
* 按照用户id返回所有的收货地址
* @param userId
* @return
*/
public List<UserAddress> getUserAddressList(String userId);
}
该接口提供获取所有收获地址的方法的声明
新建服务提供者程序
参照上面流程,新建user-service-provider
然后在pom.xml中引入上面公共接口的依赖
<dependency>
<groupId>com.badao.gmall</groupId>
<artifactId>gmall-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
然后还要引入dubbo以及zookeeper的依赖
<!-- 引入dubbo -->
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!-- 注册中心使用的是zookeeper,引入操作zookeeper的客户端端 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
完整pom.xml代码
<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.badao.gmall</groupId>
<artifactId>user-service-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.badao.gmall</groupId>
<artifactId>gmall-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 引入dubbo -->
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!-- 注册中心使用的是zookeeper,引入操作zookeeper的客户端端 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
</dependencies>
</project>
在项目下新建service.impl包,包下新建实现类
package com.badao.gmall.service.impl;
import java.util.Arrays;
import java.util.List;
import com.badao.gmall.bean.UserAddress;
import com.badao.gmall.service.UserService;
public class UserServiceImpl implements UserService {
public List<UserAddress> getUserAddressList(String userId) {
// TODO Auto-generated method stub
UserAddress address1 = new UserAddress(1, "霸道流氓气质", "1", "李老师", "123456789", "Y");
UserAddress address2 = new UserAddress(2, "公众号:霸道的程序猿)", "1", "王老师", "987654321", "N");
return Arrays.asList(address1,address2);
}
}
然后按照其官方指示,新建配置文件并配置服务提供者
在src/main/resource下新建配置文件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://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) -->
<dubbo:application name="user-service-provider"></dubbo:application>
<!-- 2、指定注册中心的位置 两种方式 -->
<!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
<!-- 3、指定通信规则(通信协议?通信端口) -->
<dubbo:protocol name="dubbo" port="20882"></dubbo:protocol>
<!-- 4、暴露服务 ref:指向服务的真正的实现对象 -->
<dubbo:service interface="com.badao.gmall.service.UserService"
ref="userServiceImpl" timeout="1000" version="1.0.0">
<dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method>
</dubbo:service>
<!--统一设置服务提供方的规则 -->
<dubbo:provider timeout="1000"></dubbo:provider>
<bean id="userServiceImpl" class="com.badao.gmall.service.impl.UserServiceImpl"></bean>
</beans>
然后在com.badao.gmall包下新建MainApplication.java
读取配置文件并暴露服务。
package com.badao.gmall;
import java.io.IOException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApplication {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml");
ioc.start();
System.in.read();
}
}
搭建服务消费者程序
参照上面流程新建order-service-consumer服务消费者程序
在pom.xml中添加公共接口依赖以及dubbo和zookeeper相关依赖
<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.badao.gmall</groupId>
<artifactId>user-service-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.badao.gmall</groupId>
<artifactId>gmall-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 引入dubbo -->
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!-- 注册中心使用的是zookeeper,引入操作zookeeper的客户端端 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
</dependencies>
</project>
然后新建接口实现类OrderServiceImpl
package com.badao.gmall.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.badao.gmall.bean.UserAddress;
import com.badao.gmall.service.OrderService;
import com.badao.gmall.service.UserService;
/**
* 1、将服务提供者注册到注册中心(暴露服务)
* 1)、导入dubbo依赖(2.6.2)\操作zookeeper的客户端(curator)
* 2)、配置服务提供者
*
* 2、让服务消费者去注册中心订阅服务提供者的服务地址
* @author badao
*
*/
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
UserService userService;
public List<UserAddress> initOrder(String userId) {
// TODO Auto-generated method stub
System.out.println("用户id:"+userId);
//1、查询用户的收货地址
List<UserAddress> addressList = userService.getUserAddressList(userId);
for (UserAddress userAddress : addressList) {
System.out.println(userAddress.getUserAddress());
}
return addressList;
}
}
按照官方指示,同样新建服务消费者配置文件consumer.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<context:component-scan base-package="com.badao.gmall.service.impl"></context:component-scan>
<dubbo:application name="order-service-consumer"></dubbo:application>
<dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
<!--声明需要调用的远程服务的接口;生成远程服务代理 -->
<!--
1)、精确优先 (方法级优先,接口级次之,全局配置再次之)
2)、消费者设置优先(如果级别一样,则消费方优先,提供方次之)
-->
<dubbo:reference interface="com.badao.gmall.service.UserService"
id="userService" timeout="5000" retries="3" version="*">
</dubbo:reference>
</beans>
然后新建MainApplication,加载Spring配置,并调用远程服务
package com.badao.gmall;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.badao.gmall.service.OrderService;
public class MainApplication {
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml");
OrderService orderService = applicationContext.getBean(OrderService.class);
orderService.initOrder("1");
System.out.println("调用完成....");
System.in.read();
}
}
开始测试
参照官网快速搭建入门程序的指导:
http://dubbo.apache.org/zh-cn/docs/user/quick-start.html
搭建好服务提供者和服务消费者之后,参照上面博客搭建好zookeeper服务注册中心后,启动ZooKeeper服务端。
然后再参照博客搭建dubbo-admin管理控制台程序。
启动服务提供者的MainApplication使服务暴露。
此时刷新管理控制台,可见此时服务提供者已经暴露。
然后运行服务消费者的MainApplication,远程调用成功。
此时刷新管理控制台
服务提供者与服务消费者各一。
代码下载
关注公众号:
霸道的程序猿
回复:
Dubbo入门代码