放上一张Dubbo的架构图
刚刚学过SpringCloud,Dubbo虽然没有Spring家族那么强悍,但始终不能违背Dubbo是一款高性能优秀的RPC框架。
click官网地址了解更多
自己也是初学者,想自己动手做一个简单的demo,于是上网查资料,发现针对那种入门的案例,网上主要有两个比较经典的,一个是尚硅谷雷神版,还有一个是官网推荐的入门 案例。
雷神的Dubbo视频教程 大神讲解
视频中用的eclipse开发,习惯IDEA开发的我们不免手痒想要使用IDEA整合一波。
1. Zookeeper-3.4.14
2. dubbo-admin 可视化管理界面
安装过程参照雷神视频,没什么技术含量
这个模块的主要功能在于分包解耦
UserAddress.java:
public class UserAddress implements Serializable {
private Integer id;
private String userAddress;
private String userId;
private String consignee;
private String phoneNum;
private String 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;
}
public UserAddress(Integer id, String userAddress, String userId, String consignee, String phoneNum, String isDefault) {
this.id = id;
this.userAddress = userAddress;
this.userId = userId;
this.consignee = consignee;
this.phoneNum = phoneNum;
this.isDefault = isDefault;
}
@Override
public String toString() {
return "UserAddress{" +
"id=" + id +
", userAddress='" + userAddress + '\'' +
", userId='" + userId + '\'' +
", consignee='" + consignee + '\'' +
", phoneNum='" + phoneNum + '\'' +
", isDefault='" + isDefault + '\'' +
'}';
}
}
OrderService.java
public interface OrderService {
//初始化订单模块
public List<UserAddress> initOrder(String userId);
}
UserService.java
public interface UserService {
//消费订单模块
public List<UserAddress> getUserAddressList(String userId);
}
UserServiceImpl.java
@Component
@Service //用的是com.alibaba.dubbo.config.annotation.Service,目的是为了在注册中心暴露服务
public class UserServiceImpl implements UserService {
@Override
public List<UserAddress> getUserAddressList(String userId) {
UserAddress userAddress1 = new UserAddress(1,"福建省莆田市","37","刘老师","10086","1");
UserAddress userAddress2 = new UserAddress(2,"福建省福州市","33","黄老师","1001","1");
List<UserAddress> addresses = new ArrayList<>();
addresses.add(userAddress1);
addresses.add(userAddress2);
return addresses;
}
}
UserServiceProviderApplication.java
@EnableDubbo //开启Dubbo
@SpringBootApplication
public class UserServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceProviderApplication.class, args);
}
}
application.properties
#应用端口
server.port=8080
#dubbo服务地址
dubbo.application.name=user-service-provider
#zk注册中心协议和地址
dubbo.registry.protocol=zookeeper
dubbo.registry.address=127.0.0.1:2181
#通信规则(通信协议和端口)
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
#连接监控中心(这里面我没有启动监控中心)
dubbo.monitor.protocol=registry
pom.xml 新增依赖
<dependency>
<groupId>com.liuzeyugroupId>
<artifactId>gmall-interfaceartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
<dependency>
<groupId>com.alibaba.bootgroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>0.2.0version>
dependency>
@RequestMapping("order")
@Controller
public class OrderController {
@Autowired
private OrderService orderService;
@ResponseBody //以json格式返回
@RequestMapping("/initOrder")
public List<UserAddress> initOrder(@RequestParam("uid") String userId){
List<UserAddress> userAddresses = orderService.initOrder(userId);
return userAddresses;
}
}
OrderServiceImpl.java
@Service //暴露服务
@Component
public class OrderServiceImpl implements OrderService {
//远程调用服务
@Reference(url = "dubbo://127.0.0.1:20880")
private UserService userService;
@Override
public List<UserAddress> initOrder(String userId) {
return userService.getUserAddressList(userId);
}
}
@Reference
这边的URL指向需要注意,因为我们的通信协议使用dubbo,地址和端口也必须指明,否则会发现UserService 注入为null的现象!
OrderServiceConsumerApplication.java
@EnableDubbo
@SpringBootApplication
public class OrderServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceConsumerApplication.class, args);
}
}
application.properties
#dubbo服务名称
dubbo.application.name=order-service-consumer
#zk地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
#监控中心配置
dubbo.monitor.protocol=registry
#这个需要注意,每一个服务的dubbo端口不能冲突
dubbo.protocol.port=20881
server.port=8081
pom.xml
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.liuzeyugroupId>
<artifactId>gmall-interfaceartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
<dependency>
<groupId>com.alibaba.bootgroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>0.2.0version>
dependency>
DEMO的测试流程应该这样
项目启动
接口调式
访问:http://localhost:8081/order/initOrder?uid=33
因为SpringBoot屏蔽了大量的XML配置,但是dubbo官方又建议我们使用XML配置,所以整合的最后附上XML配置
consumer.xml
<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">
<dubbo:application name="order-service-consumer" owner="lzy" organization="hgfzp"/>
<dubbo:registry address="zookeeper://localhost:2181"/>
<dubbo:protocol name="dubbo" port="20881" />
<dubbo:service interface="com.liuzeyu.service.OrderService" ref="orderService" protocol="dubbo" />
<bean id="orderService" class="com.liuzeyu.service.impl.OrderServiceImpl"/>
<dubbo:monitor protocol="registry"/>
beans>
provider.xml
<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">
<dubbo:application name="user-service-provider" owner="lzy" organization="hgfzp"/>
<dubbo:registry address="zookeeper://localhost:2181"/>
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:service interface="com.liuzeyu.service.UserService" ref="userService" protocol="dubbo" />
<bean id="userService" class="com.liuzeyu.service.impl.UserServiceImpl"/>
<dubbo:monitor protocol="registry"/>
beans>
如需要启动测试记住需要注释掉之前加在接口实现类上面的服务暴露配置
OrderServiceImpl:
UserServiceImpl:
通过配置自定义配置类注入配置信息
OrderConfig.java 服务提供者使用
@Configuration
public class OrderConfig {
@Bean
public ApplicationConfig applicationConfig(){
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("user-service-provider");
applicationConfig.setOwner("lzy");
applicationConfig.setOrganization("hgfzp");
return applicationConfig;
}
@Bean
public RegistryConfig registryConfig(){
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setProtocol("zookeeper");
registryConfig.setAddress("127.0.0.1:2181");
return registryConfig;
}
@Bean
public ProtocolConfig protocolConfig(){
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setName("dubbo");
protocolConfig.setPort(20880);
return protocolConfig;
}
@Bean
public ServiceConfig<UserService> serviceConfig(UserService userService){
ServiceConfig<UserService> serviceConfig = new ServiceConfig<UserService>();
serviceConfig.setInterface(UserService.class);
serviceConfig.setRef(userService);
//配置每一个Method信息
List<MethodConfig> methodConfigs = new ArrayList<>();
MethodConfig methodConfig = new MethodConfig();
methodConfig.setId("userService");
methodConfig.setName("getUserAddressList");
methodConfigs.add(methodConfig);
serviceConfig.setMethods(methodConfigs);
return serviceConfig;
}
}
ConsumerConfig.java 服务提供者使用
@Configuration
public class ConsumerConfig {
@Bean
public ApplicationConfig applicationConfig(){
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("order-service-consumer");
applicationConfig.setOwner("lzy");
applicationConfig.setOrganization("hgfzp");
return applicationConfig;
}
@Bean
public RegistryConfig registryConfig(){
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setProtocol("zookeeper");
registryConfig.setAddress("127.0.0.1:2181");
return registryConfig;
}
@Bean
public ProtocolConfig protocolConfig(){
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setName("dubbo");
protocolConfig.setPort(20881);
return protocolConfig;
}
@Bean
public ServiceConfig<OrderService> serviceConfig(OrderService orderService){
ServiceConfig<OrderService> serviceConfig = new ServiceConfig<OrderService>();
serviceConfig.setInterface(OrderService.class);
serviceConfig.setRef(orderService);
//配置每一个Method信息
List<MethodConfig> methodConfigs = new ArrayList<>();
MethodConfig methodConfig = new MethodConfig();
methodConfig.setId("orderService");
methodConfig.setName("initOrder");
methodConfigs.add(methodConfig);
serviceConfig.setMethods(methodConfigs);
return serviceConfig;
}
}
使用配置类需要注意两点