package com.dubbo.api.po;
import org.springframework.util.ObjectUtils;
public class BaseResult {
private static final long serialVersionUID = 1L;
private Integer code;
private String msg;
private Object data;
private Long count;
public BaseResult() {
}
public BaseResult(Integer code, String msg, Object data, Long count) {
this.code = code;
this.msg = msg;
this.data = data;
this.count = count;
}
public void markSuccess(String msg, Object data, Long count) {
this.code = 200;
this.msg = msg;
this.data = data;
this.count = count;
}
public void markSysError(String msg) {
this.code = 500;
this.msg = msg;
}
public void markWarning(String msg) {
this.code = 0;
this.msg = msg;
}
public boolean checkSuccess() {
return ObjectUtils.isEmpty(this.getCode()) && 200 == this.getCode();
}
public boolean checkSuccessWData() {
return this.checkSuccess() && ObjectUtils.isEmpty(this.getData());
}
public Integer getCode() {
return this.code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return this.msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return this.data;
}
public void setData(Object data) {
this.data = data;
}
public Long getCount() {
return this.count;
}
public void setCount(Long count) {
this.count = count;
}
public String toString() {
return "BaseResult [code=" + this.code + ", msg=" + this.msg + ", data=" + this.data + ", count=" + this.count + "]";
}
}
Dubbo角色:
Provider:暴露服务的服务提供者
Container:服务运行的容器
Consumer:调用远程服务的消费者
Registry:服务注册和发现的注册中心
Minitor:统计服务调用次数和时间的监控中心
调用过程:
下面根据我的理解说明一下
0:服务器容器负责启动、加载、运行服务提供者
1:服务提供者在启动后就可以向注册中心暴露服务
2:服务消费者在启动后就可以向注册中心订阅想要的服务
3:注册中心向服务消费者返回服务调用列表
4:服务消费者基于软负载均衡算法调用服务提供者的服务,这个服务提供者有可能是一个服务提供者列表,调用那个服务提供者就是根据负载均衡来调用了
5:服务提供者和服务消费者定时将保存在内存中的服务调用次数和服务调用时间推送给监控中心
大致情况了解之后,我们就继续写示例,继续创建一个全新的springBoot项目
<properties>
<java.version>11java.version>
<dubbo.version>2.7.5dubbo.version>
<curator.version>2.12.0curator.version>
properties>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-dependencies-zookeeperartifactId>
<version>${dubbo.version}version>
<type>pomtype>
<exclusions>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>${curator.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>${curator.version}version>
dependency>
<dependency>
<groupId>com.dubbogroupId>
<artifactId>apiartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
package com.dubbo.provider.po;
import java.io.Serializable;
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();
}
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;
}
}
UserService
package com.dubbo.provider.service;
import com.dubbo.provider.po.UserAddress;
import java.util.List;
public interface UserService {
/**
* 按照用户id返回所有的收货地址
* @param userId
* @return
*/
public List<UserAddress> getUserAddressList(String userId);
public String getMsg();
}
OrderService
package com.dubbo.provider.service;
import com.dubbo.provider.po.UserAddress;
import java.util.List;
public interface OrderService {
/**
* 初始化订单
* @param userId
*/
public List<UserAddress> initOrder(String userId);
}
UserServiceImpl
package com.dubbo.provider.service.impl;
import com.dubbo.provider.po.UserAddress;
import com.dubbo.provider.service.UserService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
@Service//暴露服务
@Component
public class UserServiceImpl implements UserService {
@Override
public List<UserAddress> getUserAddressList(String userId) {
UserAddress address1 = new UserAddress(1, "北京市昌平区", "1", "李老师", "010-56253825", "Y");
UserAddress address2 = new UserAddress(2, "深圳市宝安区", "1", "王老师", "010-56253825", "N");
return Arrays.asList(address1,address2);
}
@Override
public String getMsg() {
return "这是提供者推送的信息";
}
}
OrderServiceImpl
package com.dubbo.provider.service.impl;
import com.dubbo.provider.po.UserAddress;
import com.dubbo.provider.service.OrderService;
import com.dubbo.provider.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
UserService userService;
@Override
public List<UserAddress> initOrder(String userId) {
//1、查询用户的收货地址
List<UserAddress> addressList = userService.getUserAddressList(userId);
return addressList;
}
}
server.port=7010
dubbo.application.name=user-service-provider
dubbo.registry.address=127.0.0.1:2181
dubbo.registry.protocol=zookeeper
dubbo.protocol.name=dubbo
dubbo.protocol.port=20882
dubbo.monitor.protocol=registry
#dubbo.scan.base-packages=com.example.springboot.dubbo
@EnableDubbo(scanBasePackages="com.dubbo.provider.service.impl")
package com.dubbo.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
@EnableDubbo(scanBasePackages="com.dubbo.provider.service.impl")
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
<properties>
<java.version>11java.version>
<spring-boot.version>2.2.1.RELEASEspring-boot.version>
<dubbo.version>2.7.5dubbo.version>
<curator.version>2.12.0curator.version>
properties>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-dependencies-zookeeperartifactId>
<version>${dubbo.version}version>
<type>pomtype>
<exclusions>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>${curator.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>${curator.version}version>
dependency>
<dependency>
<groupId>com.dubbogroupId>
<artifactId>providerartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
package com.dubbo.consumer.consumer.controller;
import com.dubbo.api.po.BaseResult;
import com.dubbo.provider.po.UserAddress;
import com.dubbo.provider.service.OrderService;
import com.dubbo.provider.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class OrderController {
@Reference
OrderService orderService;
@Reference
UserService userService;
@ResponseBody
@RequestMapping(value = "/initOrder",method = RequestMethod.POST)
public List<UserAddress> initOrder(@RequestParam("uid")String userId) {
return orderService.initOrder(userId);
}
@ResponseBody
@RequestMapping(value = "/getMsg",method = RequestMethod.POST)
public BaseResult getMsg(@RequestParam("uid")String userId) {
BaseResult baseResult=new BaseResult();
String re=userService.getMsg();
baseResult.markSuccess("获取成功",re,null);
return baseResult;
}
}
注意:我们引用服务者暴露的服务时只需将@Service改为@Reference即可
server.port=7000
dubbo.application.name=order-service-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.monitor.protocol=registry
在运行此项目之前务必却保zookeeper以及provider已经启动
同样也在dubbo-admin检验是否注册成功
若遇到打包失败的问题,尝试下面的这种方法:
在pom文件中添加maven-surefire-plugin插件
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-surefire-pluginartifactId>
<version>2.22.1version>
<configuration>
<skipTests>trueskipTests>
configuration>
plugin>
至此一个简单的springBoot集成dubbo的项目至此结束