springboot:2.2.2
alibaba.dubbo:2.0.0
zkclinet:0.10
JDK:1.8
1.创建父工程
Idea或Eclipse创建普通Maven工程,GAV定义随意,也可以参考我的
G:com.youzha
A:Dubbo_Soul
V:1.0-SNAPSHOT
父pom完整文件(该文件包括三个子module,在之后创建)
<?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.youzha</groupId>
<artifactId>Dubbo_Soul</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>Dubbo_Provider</module>
<module>Dubbo_Consumer</module>
<module>Dubbo_Api</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<properties>
<!--1.0版本增加-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<boot.dubbo.version>2.0.0</boot.dubbo.version>
<zkclient.version>0.10</zkclient.version>
<!--1.0版本结束-->
</properties>
<dependencyManagement>
<dependencies>
<!--1.0 开始-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${boot.dubbo.version}</version>
</dependency>
<!-- ZooKeeper client -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient.version}</version>
</dependency>
<!--1.0结束-->
</dependencies>
</dependencyManagement>
</project>
2.创建共用Dubbo_Api
2.1 pom
<?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_Soul</artifactId>
<groupId>com.youzha</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Dubbo_Api</artifactId>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
</dependencies>
</project>
2.2 创建共用资源
整体目录结构:
|── com.youzha.dubbo
|── dto 封装共用返回Result
|── entity 共用参数,返回Body
|── service 暴露Service,提供给第三方使用
3.创建提供者Dubbo_Provider
3.1 pom
<?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_Soul</artifactId>
<groupId>com.youzha</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Dubbo_Provider</artifactId>
<dependencies>
<dependency>
<groupId>com.youzha</groupId>
<artifactId>Dubbo_Api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--com.dubbo-springBoot依赖 -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.2 Service
需要注意的是,这里的@Service引自:import com.alibaba.dubbo.config.annotation.Service;,标注其为一个Dubbo的Service,而下面的@Component才是将其注入成一个Spring Bean。
import com.alibaba.dubbo.config.annotation.Service;
import com.youzha.dubbo.entity.User;
import com.youzha.dubbo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* @author youzhaxiaobo
* @version 1.0
* @date 2020/7/1 19:06
* @Desc
*/
@Slf4j
@Service
@Component
public class UserServiceImpl implements UserService {
@Override
public User findById(int id) {
if(id < 0) {
log.info("非法请求,ID:{}", id);
return null;
}
User user = new User(1, "youzha", 1L, new Date());
log.info("提供者返回数据:{}", user.toString());
return user;
}
}
3.3 Controller
这里的Controller模拟多个服务,除提供Dubbo接口外,自身还提供Http接口。
import com.youzha.dubbo.dto.ResultDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author youzhaxiaobo
* @version 1.0
* @date 2020/7/4 0004 13:52
* @Desc
*/
@Slf4j
@RestController
@RequestMapping("/provider")
public class ProviderController {
@GetMapping(value = "/hello", produces = MediaType.APPLICATION_JSON_VALUE)
public ResultDTO getUserById(@RequestParam("id") int id) {
log.info("id=" + id);
if (id > 0) {
return new ResultDTO(-1, "获取失败");
}
return new ResultDTO(200, "获取成功");
}
}
3.4 启动类Application
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author youzhaxiaobo
* @version 1.0
* @date 2020/7/1 19:15
* @Desc
*/
@EnableDubbo
@SpringBootApplication
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
3.5 配置文件
server:
port: 9091
spring:
main:
allow-bean-definition-overriding: true
dubbo:
application:
name: dubbo-consumer
id: dubbo-consumer
registry:
address: zookeeper://127.0.0.1:2181
protocol:
name: dubbo
port: 20883
scan:
base-packages: com.youzha.dubbo.service
logging:
level:
root: info
org.springframework.boot: info
org.apache.ibatis: info
org.dromara.soul.bonuspoint: info
org.dromara.soul.lottery: info
org.dromara.soul: info
4.创建消费者Dubbo_Consumer
4.1 pom
<?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_Soul</artifactId>
<groupId>com.youzha</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Dubbo_Consumer</artifactId>
<dependencies>
<dependency>
<groupId>com.youzha</groupId>
<artifactId>Dubbo_Api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--com.dubbo-springBoot依赖 -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.2 Service
4.2.1 定义Sevice
这个Service是我们自己业务所需,只是在这个Service中的某些数据我们要调用Provider服务而已,这个和Provider提供的service本身没有直接关系,也不需要保持方法名,入参的完全一致,只是引用。
public interface RemoteUserService {
/**
* ID查找用户
* @param id 用户ID
* @return USER对象
*/
User findById(int id);
}
4.2.2 定义ServiceImpl
import com.alibaba.dubbo.config.annotation.Reference;
import com.youzha.dubbo.entity.User;
import com.youzha.dubbo.service.RemoteUserService;
import com.youzha.dubbo.service.UserService;
import org.springframework.stereotype.Service;
/**
* @author youzhaxiaobo
* @version 1.0
* @date 2020/7/1 19:06
* @Desc
*/
@Service
public class UserServiceImpl implements RemoteUserService {
@Reference
private UserService userService;
@Override
public User findById(int id) {
return userService.findById(id);
}
}
说明:
- 实现是的我们自身的业务接口
- 通过dubbo的@Reference注解,注入Provider的Service【而提供者是通过dubbo的@Service注解暴露,暴露和注入都在zookeeper获取】
4.3 Controller
@Slf4j
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
@Autowired
private RemoteUserService userService;
@GetMapping(value = "/getUserById", produces = MediaType.APPLICATION_JSON_VALUE)
public ResultDTO getUserById(@RequestParam("id") int id) {
log.info("id=" + id);
User user = userService.findById(id);
log.info("消费者获取用户,信息为:{}", user);
if(null == user) {
return new ResultDTO(-1, "获取失败");
}
return new ResultDTO(200, "获取成功", user);
}
}
4.4 启动类Application
@EnableDubbo
@SpringBootApplication
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
4.5 配置文件
server:
port: 9092
spring:
main:
allow-bean-definition-overriding: true
dubbo:
application:
name: dubbo-consumer
id: dubbo-consumer
registry:
address: zookeeper://127.0.0.1:2181
protocol:
name: dubbo
port: 20883
scan:
base-packages: com.youzha.dubbo.service
logging:
level:
root: info
org.springframework.boot: info
org.apache.ibatis: info
org.dromara.soul.bonuspoint: info
org.dromara.soul.lottery: info
org.dromara.soul: info
5.验证
5.1 运行zookeeper
当然前提是需要安装zk,配置文件也可以看到我这里启动的本地zk(如果你使用的其他地址的zk,替换上图zk的地址即可),如果没有可以参考这个附件:
运行前你需要解压打开:zookeeper-3.4.14\conf\zoo.cfg,确保对应的dataDir在你本地存在,如果是Linux系统换成对应的路径即可,下图示例:Windows本地
进入:zookeeper-3.4.14\bin,
如果你和我一样是Windows环境测试,双击:zkServer.cmd,
如果你是Linux环境,启动zkServer.sh,二者效果完全一致,启动后类似下图则表示启动完成:
5.2 启动提供者
直接启动对应的启动类Application即可。
5.3 启动消费者
直接启动对应的启动类Application即可。
5.4 请求验证
postman发送地址请求:http://localhost:9092/consumer/getUserById?id=1
查看消费者日志:
查看提供者日志:
6.总结
如果你在本章节,运行并未出现上述效果,可能是哪里出错了,我们在接下来的网关接入,一样比对着可以修复,你可以百度自行解决当前问题,也可直接进入下一章节,进行网关接入,一起验证。