dubbo用来做分布式项目间的通信,今天做了一个简单的案例,此间除了很多问题,不过最终也成功跑通了,不BB,直接代码。
步骤一:首先创建一个maven的父工程,shop-parent
做一个简单的测试这三个子项目是必须的:
shop-user-interface是接口,打包方式是jar包
shop-user-web 是本次项目里面的消费者
shop-user-service 是本次项目里面的提供者
步骤二:引入maven依赖(提供者和消费者引入的都一样)
<!--dubbo-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
<!--zkclient-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.2</version>
</dependency>
步骤三:配置文件的配置
消费者:
server:
port: 9001
dubbo:
application:
id: user-web
name: user-web
protocol:
id: dubbo
name: dubbo
port: 20880
registry:
#修改成你自己的zookeeper的地址
#怎么安装自己百度,不难
address: zookeeper://192.168.25.128:2181
id: my-registry
提供者:
server:
port: 9002
spring:
application:
name: shop
datasource:
url:jdbc:mysql://localhost:3306/?shopuseUnicode=true&characterEncoding=UTF8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
#mybatis
mybatis:
mapper-locations: "classpath*:mapper/*.xml"
dubbo:
application:
id: user-service
name: user-service
protocol:
id: dubbo
name: dubbo
port: 20880
registry:
address: zookeeper://192.168.25.128:2181
id: my-registry
scan:
basePackages: com.yichuan.user.serviceImpl
# logging
logging:
level:
com:
yc:
shop:
mapper: debug
#pagehelper分页插件
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
步骤四:开始测试代码的编写
1、首先写一个接口:UserService
package com.yichuan.service;
import com.yichuan.shop.entity.base.Result;
public interface UserService {
//这个是我们测试用的
String haha(String name);
Result getAll();
Result login(String username, String pasword);
}
这里说一下Result类是我对返回数据的简单封装,代码如下:
package com.yichuan.shop.entity.base;
import lombok.Data;
import java.io.Serializable;
/**
* @program: shop
* @description:
* @author: Chenglei
* @create: 2018-08-13 12:59
*/
@Data
public class Result implements Serializable {
/**
* msg:信息
* successOrFail:是否成功
* data:数据
*/
private String msg;
private boolean successOrFail;
private Object data;
public Result(String msg, boolean successOrFail, Object data) {
this.msg = msg;
this.successOrFail = successOrFail;
this.data = data;
}
/**
* 返回数据成功
* @param msg
* @param data
* @return
*/
public static Result success(String msg, Object data) {
return new Result(msg, true, data);
}
/**
* 返回数据失败
* @param msg
* @param data
* @return
*/
public static Result fail(String msg, Object data) {
return new Result(msg, false, data);
}
}
2、在我们的shop-user-service的pom文件中引入shop-user-interface依赖 写一个UserServiceImpl继承UserService
package com.yichuan.user.serviceImpl;
import com.alibaba.dubbo.config.annotation.Service;
import com.yichuan.service.UserService;
import com.yichuan.shop.entity.User;
import com.yichuan.shop.entity.base.Result;
import com.yichuan.shop.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @program: shop-parent
* @description: userservice
* @author: Chenglei
* @create: 2018-08-25 15:59
*/
// 注意这里的service是alibaba那个包下面的
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
// 重写该方法
@Override
public String haha(String name) {
System.out.println("========================haha");
return "haha," + name;
}
@Override
public Result getAll() {
return Result.success("查询成功", userMapper.getAll());
}
}
注意:这里的service是alibaba那个包下面的
3、服务的调用 controller
在shop-user-web 里面写一个controller来调用shop-user-service 里面的haha接口
代码如下:
package com.yichuan.user.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.yichuan.service.UserService;
import com.yichuan.shop.entity.base.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @program: shop-parent
* @description: 用户操作控制器
* @author: Chenglei
* @create: 2018-08-25 10:14
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Reference
private UserService userService;
@RequestMapping("/hi")
// 测试框架是否搭建成功能跑通
public String hi(String name) {
return "hi," + name;
}
@RequestMapping("/haha")
//测试dubbo
public String haha(String name) {
return userService.haha(name);
}
@RequestMapping("/getAll")
public Result getAll() {
return userService.getAll();
}
}
其他一些小细节忽略 我会附上源码放在git上。这里只说下大概思路。
步骤五:测试的结果
要依次启动项目:按照servicer ---> web的顺序,就是先提供者在消费者的顺序。
接着 我们来测试haha
打开浏览器输入:http://localhost:9001/user/haha?name=%E6%9D%8E%E7%8B%97%E5%97%A8
得到如下结果:
该项目跑通 简答的整合完毕,自己也在学习中,如果说的不对请斧正。
https://gitee.com/chenglei000/shop-parent.git