概述:
Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合)。从服务模型的角度来看,Dubbo采用的是一种非常简单的模型,要么是提供方提供服务,要么是消费方消费服务,所以基于这一点可以抽象出服务提供方(Provider)和服务消费方(Consumer)两个角色。
我们来看一下Dubbo 的RPC 调用流程,这里主要涉及到4个模块:
Registry:服务注册,我们一般会采取Zookeeper 作为我们的注册中心
Provider:服务提供者(生产者),提供具体的服务实现
Consumer:消费者,从注册中心中订阅服务
Monitor:监控中心,RPC调用次数和调用时间监控
从上图中我们可以了解到整个RPC 服务调用的过程主要为:
生产者发布服务到服务注册中心中
消费者在服务注册中心中订阅服务
消费者调用已经注册的服务
zookeeper 下载 安装
下载地址 : https://www.apache.org/dyn/closer.cgi/zookeeper/
Linux安装教程 :https://www.cnblogs.com/lsdb/p/7297731.html
Windows安装教程:https://blog.csdn.net/zsq520520/article/details/72824925
一、项目构建
我们项目主要分为了两个模块,一部分是生产者:spring-boot-dubbo ,一部分是:spring-boot-consumer。
二、Productor
2.1 pom.xml 依赖配置
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
2.2、配置文件
## Dubbo 服务提供者配置
spring.dubbo.application.name=provider --服务名称
spring.dubbo.registry.address=zookeeper://169.254.147.205:2181 -- 注册中心地址
spring.dubbo.protocol.name=dubbo -- dubbo 协议
spring.dubbo.protocol.port=20880
spring.dubbo.scan=com.jaycekon.dubbo.service --声明需要暴露的服务接口
2.3、服务提供
接口
import com.jaycekon.dubbo.domain.User;
/**
* Created by Jaycekon on 2017/9/19.
*/
public interface UserService {
User saveUser(User user);
}
实现
import com.alibaba.dubbo.config.annotation.Service;
import com.jaycekon.dubbo.domain.User;
import com.jaycekon.dubbo.service.UserService;
/**
* Created by Jaycekon on 2017/9/19.
*/
@Service
public class UserServiceImpl implements UserService {
@Override
public User saveUser(User user) {
user.setId(1);
System.out.println(user.toString());
return user;
}
}
三、Consumer
3.1、pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot Dubbo 依赖 -->
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- mvn spring-boot:run 热部署启动 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>provided</scope>
</dependency>
</dependencies>
3.2、配置文件
## 避免和 server 工程端口冲突
server.port=8081
## Dubbo 服务消费者配置
spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.scan=com.jaycekon.dubbo.service
3.3、服务实现
接口
import com.jaycekon.dubbo.domain.User;
/**
* Created by Jaycekon on 2017/9/19.
*/
public interface UserService {
User saveUser(User user);
}
接口实现
import com.alibaba.dubbo.config.annotation.Reference;
import com.jaycekon.dubbo.domain.City;
import com.jaycekon.dubbo.domain.User;
import org.springframework.stereotype.Component;
/**
* 城市 Dubbo 服务消费者
* <p>
* Created by Jaycekon on 20/09/2017.
*/
@Component
public class CityDubboConsumerService {
@Reference
CityDubboService cityDubboService;
@Reference
UserService userService;
public void printCity() {
String cityName = "广州";
City city = cityDubboService.findCityByName(cityName);
System.out.println(city.toString());
}
public User saveUser() {
User user = new User();
user.setUsername("jaycekon")
.setPassword("jaycekong824");
return userService.saveUser(user);
}
}
3.4、服务调用
import com.jaycekon.dubbo.service.CityDubboConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by Jaycekon on 2017/9/19.
*/
@RestController
public class UserController {
@Autowired
private CityDubboConsumerService service;
@RequestMapping("/save")
public Object saveUser() {
return service.saveUser();
}
}