什么是Dubbo?
Dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。(来源于百度百科)
RPC的底层是Socket,与Restful的区别是,RPC不需要关心网络通信(状态码)、数据处理(请求头、返回值)的细节,效果像是本地调用一样。
使用Dubbo前,需要部署zookeeper
部署zookeeper
一、到Apache官方网站下载安装包 Apache ZooKeeper
二、将下载好的文件解压出来后,将conf目录下的zoo_sample.cfg文件,复制一份,重命名为zoo.cfg
三、安装目录下面新建一个空的data文件夹和log文件夹
四、打开刚刚新建的zoo.cfg文件,修改配置
原内容:
修改成:
也就是添加了刚刚新建的data和log的文件路径
五、启动zookeeper
打开文件原目录下的bin文件夹,然后双击 zkServer.cmd
启动之后会出现这个,就算是启动zookeeper完成了
如果你打开zookeeper之后一闪而过,有一种可能就是端口冲突,因为3.5之后的zookeeper是会占用8080端口的,看看你其他开着的项目有没有用到8080端口(特别是Tomcat上部署的,我也是醉了)
接着可以打开一个提供好的客户端,验证zookeeper是否启动成功
客户端出现:Welcome to ZooKeeper! 则表示启动zookeeper成功,然后可以关闭这个客户端了
参考:windows环境下安装zookeeper教程详解(单机版)_风轩雨墨的博客-CSDN博客_zookeeper安装教程
SpringBoot整合Dubbo
再进行SpringBoot整合dubbo之前要确保zookeeper部署成功,也就是上面的步骤
一、编写提供者 provider
1)新建一个SpringBoot工程,命名为provider,添加依赖
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo3</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.46</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.1.0</version>
</dependency>
<!--引入zookeeper的客户端工具-->
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2)添加配置
server.port=8081
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.application.name=provider-ticker
dubbo.scan.base-packages=com.example.demo3.service
- 其中的扫描 service 包就是要提供的服务接口的包
- 3.5版本之后的zookeeper启动会占用8080端口
3)编写接口
新建service包
添加接口 PrintHelloService
package com.example.demo3.service;
public interface PrintHelloService {
String function();
}
接着实现接口 PrintHelloService ,其中的@Service注解是dubbo包的,不是Spring包的
package com.example.demo3.service.imp;
import com.alibaba.dubbo.config.annotation.Service;
import com.example.demo3.service.PrintHelloService;
import org.springframework.stereotype.Component;
@Component
@Service
public class PrintHelloServiceImp implements PrintHelloService {
public String function(){
return "Hello";
}
}
4)在启动文件添加注解 @EnableDubbo
package com.example.demo3;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class Demo3Application {
public static void main(String[] args) {
SpringApplication.run(Demo3Application.class, args);
}
}
二、编写消费者 Consume
1)新建一个SpringBoot工程,命名为consume,添加依赖(同提供者的依赖)
2)添加配置
server.port=8082
dubbo.application.name=consumer-user
dubbo.registry.address=zookeeper://127.0.0.1:2181
3)从提供者那复制接口 PrintHelloService 过来
4)使用接口,在需要的地方使用@Reference注入即可(dubbo包里的@Reference)
@Reference
private PrintHelloService printHelloService;
Dubbo管理界面
【dubbo】 部署dubbo管理网站(windows)_piano_diano的博客-CSDN博客
-------------------------------------------------------------------------------------------------------------------------------------------------------
上面用的dubbo版本是2.0.0,这个版本是比较老的,还是属于阿里的时期,到18年后,dubbo捐给了apache,连依赖坐标都改了,下面就使用apache的dubbo配置环境
一、添加依赖
<dependencies>
<!--SpringBoot -->
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- apache dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.3</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.6.2</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.1</version>
</dependency>
<!-- 最新 starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
</dependencies>
二、修改配置(消费者)
server:
port: 8085
dubbo:
application:
name: mail
registry:
address: zookeeper://127.0.0.1:2181
provider:
threads: 10
threadpool: fixed
loadbalance: roundrobin
protocol:
name: dubbo
port: 21600
scan:
base-packages: com.example.demo3.service
剩下的编写接口,以及给接口加注释@Service就不写了,apache版本的dubbo使用跟阿里的差不多,只不过不用在启动类里添加注解@EnableDubbo
参考:https://www.imooc.com/article/292656
------------------------------------------------------------------------------------------------------------------------------------------------------------------
Dubbo的四种负载均衡策略:
同一种服务可以挂到不同的服务器上,以达到负载均衡的效果,dubbo可以在配置文件中设置4种策略来处理负载均衡:
dubbo:
provider:
loadbalance: roundrobin
1、random 随机
2、roundrobin 轮询(每台机器轮流处理请求)
3、leastAction 最少活跃调用(越闲的机器就被分配越多请求,活跃度相同就随机)
4、consistenthash 一致性哈希(相同参数的请求总是发到同一提供者,当某一台机器挂了,就把参数值分配到其他机器)
依赖问题:
如果启动之后报错:
Consider defining a bean of type 'org.apache.dubbo.spring.boot.actuate.endpoint.metadata.DubboMetadata' in your configuration.
***************************
APPLICATION FAILED TO START
***************************
Description:
Field dubboMetadata in org.apache.dubbo.spring.boot.actuate.endpoint.DubboMetadataEndpoint required a bean of type 'org.apache.dubbo.spring.boot.actuate.endpoint.metadata.DubboMetadata' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.apache.dubbo.spring.boot.actuate.endpoint.metadata.DubboMetadata' in your configuration.
Process finished with exit code 1
这是缺少了 spring-boot-starter 的依赖,在pom里加上
<!-- springframework -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>