来自官网介绍
中文:Apache Dubbo 是一款高性能、轻量级的开源服务框架;
英文:Apache Dubbo是一款高性能、 基于Java的开源RPC框架
那么就得说一下RPC:
在分布式计算,远程过程调⽤(英语:Remote Procedure Call,缩写为 RPC)是⼀个计算机通信协 议。该协议允许运⾏于⼀台计算机的程序调⽤另⼀个地址空间(通常为⼀个开放⽹络的⼀台计算机)的⼦程序,⽽程序员就像调⽤本地程序⼀样,⽆需额外地为这个交互作⽤编程(⽆需关注细节)。RPC是 ⼀种服务器-客户端(Client/Server)模式,经典实现是⼀个通过发送请求-接受回应进⾏信息交互的 系统。 如果涉及的软件采⽤⾯向对象编程,那么远程过程调⽤亦可称作远程调⽤或远程⽅法调⽤,例:Java RMI。
涉及到
如果实现远程⽅法调⽤,基本的就是通过⽹络,通过传输数据来进⾏调⽤。 方式:
对于交互传输数据,是由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整套微服务框架一致;
注册中心, 配置中心, 元数据中心不是必须的。 因为消费者和服务提供者可以直连调用的。服务类大也可以,只是这时候的维护成本远比 引入 注册, 配置中心的成本高很多;
public interface ProviderServiceInterface {
public User getUser();
}
public class User {
private String username;
//setter,getter, constructor
}
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服务
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
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
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 接口的代理类;现在暂时不用;
@RestController
@RequestMapping("consumer")
public class ConsumerController {
@Autowired
private ConsumerService consumerService;
@RequestMapping(value = "service")
public String test() {
return consumerService.test();
}
}
上面例子是通过HTTP请求来调用的,不怎么常用,初步了解一下了;
项目结构如下:
创建一个maven项目, 再创建三个maven module子模块;
simple-demo的pom.xml文件
<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.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>simple-demoartifactId>
<packaging>pompackaging>
<version>1.0-SNAPSHOTversion>
<modules>
<module>interfacemodule>
<module>demo-consumermodule>
<module>demo-providermodule>
<module>demo-consumermodule>
modules>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<spring-boot.version>2.1.0.RELEASEspring-boot.version>
<dubbo.version>2.7.3dubbo.version>
<tomcat.version>8.5.31tomcat.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${spring-boot.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-dependencies-bomartifactId>
<version>${dubbo.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
<version>${dubbo.version}version>
<exclusions>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>springartifactId>
exclusion>
<exclusion>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
exclusion>
<exclusion>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
exclusion>
exclusions>
dependency>
dependencies>
dependencyManagement>
project>
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 + '\'' +
'}';
}
}
<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-demoartifactId>
<groupId>org.examplegroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<groupId>com.examplegroupId>
<artifactId>demo-providerartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>demo-providername>
<description>demo-providerdescription>
<properties>
<tomcat.version>8.5.31tomcat.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>2.7.3version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-registry-zookeeperartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-remoting-httpartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.examplegroupId>
<artifactId>interfaceartifactId>
<version>1.0-SNAPSHOTversion>
<scope>compilescope>
dependency>
dependencies>
project>
server.port=8081
# Spring boot application
spring.application.name=providerDemo
# Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service
dubbo.scan.base-packages=com.example.demoprovider
dubbo.application.name=${spring.application.name}
# Dubbo Protocol
#dubbo.protocol.name=http
#dubbo.protocol.server=tomcat
# 采用什么协议 | 可以是HTTP 也可以是dubbo
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
## Dubbo Registry
dubbo.registry.address=zookeeper://127.0.0.1:2181
import com.demo.DemoInterface;
import com.demo.User;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author zqh
*/
@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的启动类一样, 就是为了启动程序而已;
<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-demoartifactId>
<groupId>org.examplegroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>demo-consumerartifactId>
<dependencies>
<dependency>
<groupId>org.examplegroupId>
<artifactId>interfaceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>2.7.3version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-dependencies-zookeeperartifactId>
<version>${dubbo.version}version>
<type>pomtype>
<exclusions>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-rpc-httpartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>com.alibaba.springgroupId>
<artifactId>spring-context-supportartifactId>
<version>1.0.2version>
dependency>
dependencies>
project>
spring:
application:
name: demo-consumer
server:
port: 8082
dubbo:
registry:
address: zookeeper://127.0.0.1:2181
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;
/**
* @author Administrator
*/
@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'}
有几个需要注意的问题,我过程中出现几个问题:
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>2.7.5version>
dependency>
而zookeeper的版本是2.7.3:
<dubbo.version>2.7.3dubbo.version>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-dependencies-zookeeperartifactId>
<version>${dubbo.version}version>
<type>pomtype>
<exclusions>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
exclusion>
exclusions>
dependency>
就出现了依赖冲突, 需要dubbo与zookeeper的版本一致, 否则就出了问题
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容器中,使用动态代理模式, 将发起调用的过程逻辑内部全给封装起来,(像负载均衡, 服务降级, 定时拉取服务等),进而使调用其他服务的接口方法就像调用本地接口方法一样方便快捷