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;
//setter
//getter
}
回到开头说的:
官网英文解释----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;
//setter,getter, constructor
}
服务提供者模块Provider
- 配置文件
server.port=8080
# Spring boot application
spring.application.name=providerDemo
# Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service
dubbo.scan.base-packages=com..provider
dubbo.application.name=${spring.application.name}
# Dubbo Protocol
dubbo.protocol.name=http
dubbo.protocol.server=tomcat
dubbo.protocol.port=8080
## Dubbo Registry
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 boot application
spring.application.name=consumerDemo
# Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service
dubbo.scan.base-packages=com.consumer # 扫描@service注解所在的包
dubbo.application.name=${spring.application.name}
## Dubbo Registry
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协议的例子
推荐使用注解方式:
服务提供方Provider
- 接口实现类
public interface AnnotationService {
public String sayHello(String name);
}
@Service
public class AnnotationServiceImpl implements AnnotationService {
@Override
public String sayHello(String name) {
return "annotation: hello, " + name;
}
}
- 配置
# dubbo-provider.properties
dubbo.application.name=annotation-provider
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
- 扫描路径
@Configuration
@EnableDubbo(scanBasePackages = "org.apache.dubbo.samples.simple.annotation.impl")
@PropertySource("classpath:/spring/dubbo-provider.properties")
static public class ProviderConfiguration {
}
服务消费方consumer
- Reference注解引用服务
作用: 生成接口的动态代理实现类, 后会将动态代理实现类注入Spring容器中
@Component("annotationAction")
public class AnnotationAction {
@Reference
private AnnotationService annotationService;
public String doSayHello(String name) {
return annotationService.sayHello(name);
}
}
- 配置
# dubbo-consumer.properties
dubbo.application.name=annotation-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.consumer.timeout=3000
- 指定Spring扫描路径
@Configuration
@EnableDubbo(scanBasePackages = "org.apache.dubbo.samples.simple.annotation.action")
@PropertySource("classpath:/spring/dubbo-consumer.properties")
@ComponentScan(value = {"org.apache.dubbo.samples.simple.annotation.action"})
static public class ConsumerConfiguration {
}
- 服务调用
public static void main(String[] args) throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
context.start();
final AnnotationAction annotationAction = (AnnotationAction) context.getBean("annotationAction");
String hello = annotationAction.doSayHello("world");
}