Dubbo
来自官网介绍 中文:Apache Dubbo 是一款高性能、轻量级的开源服务框架; 英文:Apache Dubbo是一款高性能、 基于Java的开源RPC框架
那么就得说一下RPC:
在分布式计算,远程过程调?(英语:Remote Procedure Call,缩写为 RPC)是?个计算机通信协 议。该协议允许运?于?台计算机的程序调?另?个地址空间(通常为?个开放?络的?台计算机)的?程序,?程序员就像调?本地程序?样,?需额外地为这个交互作?编程(?需关注细节)。RPC是 ?种服务器-客户端(Client/Server)模式,经典实现是?个通过发送请求-接受回应进?信息交互的 系统。 如果涉及的软件采??向对象编程,那么远程过程调?亦可称作远程调?或远程?法调?,例:Java RMI。
涉及到
- 两台计算机
本质就是不同进程之间的交互; - 像调?本地程序?样,?需额外地为这个交互作?编程
调用接口方法,就能完成接口调用, 就像controller层直接调用service层一样,调用完就结束(本地调用) - 计算机通信协议
既然是通信协议, 实现方式就会由多种;
如果实现远程?法调?,基本的就是通过?络,通过传输数据来进?调?。 方式:
- RPC over Http:基于Http协议来传输数据
- PRC over Tcp:基于Tcp协议来传输数据
对于交互传输数据,是由C/S双方协商定制, 一般情况下包含:
- 接口名
- 接口方法名
- 接口方法入参
- 接口方法参数类型
- 接口版本(版本)
Java实现:
public class Invocation implements Serializable {
private String interfaceName;
private String methodName;
private Class[] paramTypes;
private Object[] params;
}
回到开头说的: 官网英文解释----Dubbo是个RPC框架; RPC框架只是针对服务之间调用的; 作用与Spring Cloud OpenFeign一样; 官网中文解释----Dubbo是个服务框架; 就不仅限与服务之间的调用, 会有网关,服务治理, 监控等一整套功能; 即: 作用整体上与 Spring Cloud Alibaba整套微服务框架一致;
Dubbo原理图
Dubbo架构图
注册中心, 配置中心, 元数据中心不是必须的。 因为消费者和服务提供者可以直连调用的。服务类大也可以,只是这时候的维护成本远比 引入 注册, 配置中心的成本高很多;
基于HTTP的dubbo例子
服务接口模块interface
public interface ProviderServiceInterface {
public User getUser();
}
public class User {
private String username;
}
服务提供者模块Provider
- 配置文件
server.port=8080
spring.application.name=providerDemo
dubbo.scan.base-packages=com..provider
dubbo.application.name=${spring.application.name}
dubbo.protocol.name=http
dubbo.protocol.server=tomcat
dubbo.protocol.port=8080
dubbo.registry.address=zookeeper://127.0.0.1:2181
- 接口实现类
import org.apache.dubbo.config.annotation.Service;
@Service
public class ProviderService implements ProviderServiceInterface {
public User getUser() {
return new User("周瑜");
}
}
注意这个@Service不是Spring的@Service,是dubbo的注解; 3. 控制层
@RestController
@RequestMapping("provider")
public class ProviderController {
@Autowired
private ProviderService providerService;
@RequestMapping(value = "service")
public User test() {
return providerService.getUser();
}
}
- 启动类
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
是一个Spring Boot项目, 启动provider服务
服务消费者模块consumer
- 配置文件
server.port=8080
spring.application.name=consumerDemo
dubbo.scan.base-packages=com.consumer
dubbo.application.name=${spring.application.name}
dubbo.registry.address=zookeeper://127.0.0.1:2181
- 通过HttpClient来调用 Provider接口
public class HttpClient {
public static String get(String url) {
String result = "";
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("Content-type", "application/json");
CloseableHttpResponse response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
result = EntityUtils.toString(response.getEntity(), "utf-8");
}
response.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
- Service层 : 发起HTTP调用, 访问目标服务;
@Service
public class ConsumerService {
@Reference
private ProviderServiceInterface providerService;
public String test() {
String result = HttpClient.get("http://localhost:8081/provider/service");
User user = JSONObject.parseObject(result, User.class);
return user.getUsername();
}
}
@Reference注解: 核心注解, 从容器中获取ProviderServiceInterface 接口的代理类;现在暂时不用;
- controller层: 接受用户请求,调用service层
@RestController
@RequestMapping("consumer")
public class ConsumerController {
@Autowired
private ConsumerService consumerService;
@RequestMapping(value = "service")
public String test() {
return consumerService.test();
}
}
上面例子是通过HTTP请求来调用的,不怎么常用,初步了解一下了;
基于Dubbo协议的例子
项目结构如下: 创建一个maven项目, 再创建三个maven module子模块; simple-demo的pom.xml文件
<?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>org.example</groupId>
<artifactId>simple-demo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>interface</module>
<module>demo-consumer</module>
<module>demo-provider</module>
<module>demo-consumer</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring-boot.version>2.1.0.RELEASE</spring-boot.version>
<dubbo.version>2.7.3</dubbo.version>
<tomcat.version>8.5.31</tomcat.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
</project>
公共接口模块 interface
- 接口
public interface DemoInterface {
public User getUser();
}
public class User implements Serializable {
private static final long serialVersionUID = -6878053406541100993L;
private String username;
public User(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
'}';
}
}
服务提供方provider
- pom.xml文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>simple-demo</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-provider</name>
<description>demo-provider</description>
<properties>
<tomcat.version>8.5.31</tomcat.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-remoting-http</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>interface</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
- 配置文件
server.port=8081
spring.application.name=providerDemo
dubbo.scan.base-packages=com.example.demoprovider
dubbo.application.name=${spring.application.name}
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.registry.address=zookeeper://127.0.0.1:2181
- 公共接口实现类
- @Service : 这个注解是Dubb框架提供的, 而不是Spring的注解;
import com.demo.DemoInterface;
import com.demo.User;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@Service
public class DemoServiceImpl implements DemoInterface {
@Override
public User getUser() {
return new User("zqh");
}
}
- 启动类
@SpringBootApplication
public class DemoProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DemoProviderApplication.class, args);
}
}
其实和Spring Boot的启动类一样, 就是为了启动程序而已;
服务消费者模块Consumer
- pom.xml
<?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>simple-demo</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>demo-consumer</artifactId>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-rpc-http</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
</project>
- 配置文件
spring:
application:
name: demo-consumer
server:
port: 8082
dubbo:
registry:
address: zookeeper://127.0.0.1:2181
- 调用Provider服务
package com.consumer.service;
import com.demo.DemoInterface;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import java.io.IOException;
@EnableAutoConfiguration
public class DemoConsumer {
@Reference
private DemoInterface demoService;
public static void main(String[] args) throws IOException {
ConfigurableApplicationContext context = SpringApplication.run(DemoConsumer.class);
DemoInterface demoService = context.getBean(DemoInterface.class);
System.out.println((demoService.getUser()));
}
}
运行provider后, 再运行consumer, 结果如下:
User{username='zqh'}
有几个需要注意的问题,我过程中出现几个问题:
- ClassNotFoundException: com.alibaba.spring.util.BeanRegistrar
这个问题是因为jar包问题, 我这边一开始的dubbo版本是:
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.5</version>
</dependency>
而zookeeper的版本是2.7.3:
<dubbo.version>2.7.3</dubbo.version>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
就出现了依赖冲突, 需要dubbo与zookeeper的版本一致, 否则就出了问题
- cause: java.lang.IllegalStateException: Serialized class com.demo.User must implement java.io.Serializable
这是因为公共接口模块的pojo类没有序列, 涉及到了对象数据的网络传输转化,创建的pojo类需要实现序列化接口:
public class User implements Serializable {
private static final long serialVersionUID = -6878053406541100993L;
}
项目下载连接:
dubbo入门实例
个人思考使用场景
由于工作中没有使用dubbo框架, 使用的springcloud alibaba一套;
一般情况下, 前端发送请求到 controller接口 ,controller层调用Service层, Service层之间, 该使用事务的使用事务, 涉及到其他服务的时候,需要@Reference引入一下;如果不涉及其他服务, 就跟单体开发差不多。
@Reference
private DemoInterface demoService;
@Reference : 会生成接口的动态代理实现类,并注入SpringIOC容器中,使用动态代理模式, 将发起调用的过程逻辑内部全给封装起来,(像负载均衡, 服务降级, 定时拉取服务等),进而使调用其他服务的接口方法就像调用本地接口方法一样方便快捷
|