Dubbo
Apache Dubbo 是一款 RPC 服务开发框架,用于解决微服务架构下的服务治理与通信问题,官方提供了 …
Apache Dubbo 是一款 RPC 服务开发框架,用于解决微服务架构下的服务治理与通信问题,官方提供了 Java、Golang 等多语言 SDK 实现。使用 Dubbo 开发的微服务原生具备相互之间的远程地址发现与通信能力, 利用 Dubbo 提供的丰富服务治理特性,可以实现诸如服务发现、负载均衡、流量调度等服务治理诉求。Dubbo 被设计为高度可扩展,用户可以方便的实现流量拦截、选址的各种定制逻辑。
配置依赖:
<dependencies>
<dependency>
<groupId>fun.maluyao</groupId>
<artifactId>api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.4.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.16.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
</dependencies>方便我们开启多个应用的maven插件!
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>9000</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>要使用dubbo需要配置服务注册中心,我们使用:zookeeper作为我们的服务注册发现中心!
执行前要保证conf路径下含有zoo.cfg文件才能正常使用
linux下解压之后执行
zkServer.sh startwindows下执行
zkServer.cmd使用 docker部署zookeeper也是个不错的选择!
服务提供者配置
spring.xml
<!--dubbo配置-->
<!--1、配置项目名称,唯一-->
<dubbo:application name="dubbo-service"/>
<!--2、配置注册中心地址-->
<dubbo:registry address="zookeeper://192.168.1.5:2181"/>
<!--3、配置dubbo包扫描-->
<dubbo:annotation package="fun.maluyao.service.impl"/>web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>services
import org.apache.dubbo.config.annotation.Service;
//将这个类提供的方法(服务)对外发布。将访问的地址ip,端口,路径注册到注册中心中
@Service
public class MainServiceImpl implements MainService{
public MainServiceImpl() {
}
public String sayHello() {
return "Hello";
}
}注意
@Service注解是dubbo提供的!
服务消费者
web.xml
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>spring-mvc.xml
<mvc:annotation-driven/>
<context:component-scan base-package="fun.maluyao.controller"/>
<context:component-scan base-package="fun.maluyao.service"/>
<dubbo:application name="dubbo-web"/>
<!--2、配置注册中心地址-->
<dubbo:registry address="zookeeper://192.168.1.5:2181"/>
<!--3、配置dubbo包扫描-->
<dubbo:annotation package="fun.maluyao.controller"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>需要注入服务的地方使用
import org.apache.dubbo.config.annotation.Reference;
@Reference
MainService mainService;dubbo-admin
Dubbo Admin是为更好地可视化Dubbo服务而设计的控制台,它支持Dubbo 3,并与2.7.x、2.6.x和2.5.x兼容。
有四种方法可以将杜博Admin部署到生产环境中。
- Compile from source 从源代码编译 需要修改部分配置,保证使用java8来编译
- Run with Docker 使用Docker 需要配置好zookeeper,要熟练使用docker网络知识
- Run with Kubernetes 与Kubernetes一起运行
- Run with Helm 与Helm一起运行
docker run -p 38080:38080 --name dubbo-admin -e admin.registry.address=zookeeper://192.168.1.5:2181 -e admin.config-center=zookeeper://192.168.1.5:2181 -e admin.metadata-report.address=zookeeper://192.168.1.5:2181 -d dubbo-admin
docker network create --driver bridge --subnet 192.168.1.0/24 --gateway 192.168.1.1 my-custom-network
npm install --registry=http://registry.npmmirror.com
npm config set registry=http://registry.npmmirror.com
npm config get registry
https://registry.npm.taobao.org
http://registry.npmmirror.com
/c/Users/maluyao/Desktop/theme/dubbo-admin/dubbo-admin-ui/node/npm高级特性
序列化
当传输类对象的时候需要实现Serializable接口,实现完了之后就可以传输类对象了 !
地址缓存
注册中心挂了,服务如果本地已经有了,那么服务依然可以使用
超时与重试
服务提供者使用
@Service(timeout = 5000)timeout对应超时时间,单位是毫秒
@Service(timeout = 5000,retries = 1)同时retries属性可以设置重试次数
服务消费者使用
@Reference(timeout = 3000)
MainService mainService;这里是就近原则,Reference 和Service注解同时配置,Reference生效
最好服务提供者配置,因为服务者知道自己的情况
多版本
dubbo使用 version属性来设置和调用同一个接口的不同版本
服务提供者
@Service(version = "v1.0")
serviceimpl1
@Service(version = "v2.0")
serviceimpl2
服务消费者
@Reference(version = "1.0")
MainService mainService;负载均衡
负载均衡策略(4种)
配置策略方式:
配置loadbalance属性
@Reference(loadbalance =“策略” )策略的字符串怎么获得?

抽象类AbstractLoadBalance有多个实现,这些实现里写了对应的字符串

-
Random:按照权重随机,默认值
java@Service(weight = 100) -
RoundRobin:按权重轮询
-
leastActive:最少活跃调用数,相同活跃数随机(找个最快的)
-
ConstentHash:一致性Hash,相同参数的请求总是发送给同一提供者
集群容错
设置容错处理
@Reference(cluster = "")这些字符串怎么找呢?和前面一样

每个实现了这个接口的都有一个字符串
| 策略名称 | 描述 |
|---|---|
| failover | 当服务调用失败时,尝试调用集群中的其他节点。如果所有节点都失败,则最终失败。通常用于需要高可用性的场景。 |
| failfast | 当服务调用失败时,立即抛出异常,不进行任何重试。适用于快速失败的场景,不希望因为故障而影响系统的响应时间。 |
| failsafe | 当服务调用失败时,忽略异常,不进行任何重试,并返回一个默认的结果。适用于即使服务调用失败也不会对系统造成重大影响的场景。 |
| failback | 当服务调用失败时,记录失败的请求,并在服务恢复后重新尝试执行这些请求。适用于可以容忍延迟处理的场景。 |
| forking | 同时向所有可用节点发送请求,并返回第一个成功响应的结果。适用于需要尽快获取结果,且可以接受多个请求同时发生的场景。 |
服务降级
服务降级方式:
mock=force:return null表示消费方对该服务的方法调用都值直 接返回null值,不发起远程调用。用来屏蔽不重要服务不可用时对调用方的影响。mock=fail:return null表示消费方对该服务的方法调用在失败 后,再返回null值,不抛异常。用来容忍不重要服务不稳定时对调用方的影响。
这个是在@Reference(mock = "force:return null")上配置的
XML 配置
Dubbo 有基于 Spring Schema 扩展的自定义配置组件,XML 支持的配置项与 配置参考手册 中描述的一一对。本文使用的示例请参考 dubbo-samples-spring-xml
XML完整示例
服务提供者
定义服务接口
DemoService.java:
package org.apache.dubbo.demo;
public interface DemoService {
String sayHello(String name);
}在服务提供方实现接口
DemoServiceImpl.java:
package org.apache.dubbo.demo.provider;
import org.apache.dubbo.demo.DemoService;
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return "Hello " + name;
}
}用 Spring 配置声明暴露服务
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder/>
<dubbo:application name="demo-provider"/>
<dubbo:registry address="zookeeper://${zookeeper.address:127.0.0.1}:2181"/>
<dubbo:provider token="true"/>
<bean id="demoService" class="org.apache.dubbo.samples.basic.impl.DemoServiceImpl"/>
<dubbo:service interface="org.apache.dubbo.samples.basic.api.DemoService" ref="demoService"/>
</beans>加载 Spring 配置
public class Application {
public static void main(String[] args) throws InterruptedException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-demo-provider.xml");
context.start();
System.out.println("dubbo service started");
// to hang up main thread
new CountDownLatch(1).await();
}
}服务消费者
通过 Spring 配置引用远程服务
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder/>
<dubbo:application name="demo-consumer"/>
<dubbo:registry address="zookeeper://${zookeeper.address:127.0.0.1}:2181"/>
<dubbo:reference id="demoService" check="true" interface="org.apache.dubbo.samples.basic.api.DemoService"/>
</beans>加载 Spring 配置,并调用远程服务
public class Application {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-demo-consumer.xml");
context.start();
GreetingsService greetingsService = (GreetingsService) context.getBean("greetingsService");
String message = greetingsService.sayHi("dubbo");
System.out.println("Receive result ======> " + message);
System.in.read();
System.exit(0);
}
}更多示例
版本与分组
<dubbo:service interface="com.foo.BarService" version="1.0.0" />
<dubbo:service interface="org.apache.dubbo.example.service.DemoService" group="demo2"/>集群容错
配置 failover 重试次数:
<dubbo:service retries="2" />
<dubbo:reference>
<dubbo:method name="findFoo" retries="2" />
</dubbo:reference>多协议
<dubbo:application name="world" />
<dubbo:registry id="registry" address="10.20.141.150:9090" username="admin" password="hello1234" />
<!-- 多协议配置 -->
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:protocol name="rmi" port="1099" />
<!-- 使用dubbo协议暴露服务 -->
<dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" protocol="dubbo" />
<!-- 使用rmi协议暴露服务 -->
<dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" protocol="rmi" />多注册中心
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="world" />
<!-- 多注册中心配置 -->
<dubbo:registry id="chinaRegistry" address="10.20.141.150:9090" />
<dubbo:registry id="intlRegistry" address="10.20.154.177:9010" default="false" />
<!-- 向中文站注册中心注册 -->
<dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" registry="chinaRegistry" />
<!-- 向国际站注册中心注册 -->
<dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" registry="intlRegistry" />
</beans>全局默认值
指定全局默认超时时间,多所有服务生效:
<dubbo:provider timeout="5000" />
<dubbo:consumer timeout="5000" />基于分组的默认值:
<dubbo:provider timeout="5000">
<dubbo:service interface="com.alibaba.hello.api.HelloService" ref="helloService"/>
<dubbo:service interface="com.alibaba.hello.api.HelloService2" ref="helloService2"/>
</dubbo:provider>
<dubbo:provider timeout="8000">
<dubbo:service interface="com.alibaba.hello.api.DemoService" ref="demoService"/>
<dubbo:service interface="com.alibaba.hello.api.DemoService2" ref="demoService2"/>
</dubbo:provider>Spring Boot
关于 Spring Boot 的注解、基本使用方法等请参考 使用教程 - Spring Boot。以下是 spring boot 支持的配置详情与 starter 列表。
application.yaml
以下是 Dubbo 框架支持的配置组件列表,可以在 Spring Boot 配置文件中指定所需配置。
配置示例
dubbo:
application:
name: dubbo-springboot-demo-provider
logger: slf4j
protocol:
name: dubbo
port: 50052
registry:
address: nacos://${nacos.address:127.0.0.1}:8848?username=nacos&password=nacosdubbo
- dubbo.application -
org.apache.dubbo.config.ApplicationConfig - dubbo.config-center -
org.apache.dubbo.config.ConfigCenterConfig - dubbo.consumer -
org.apache.dubbo.config.ConsumerConfig - dubbo.metadata-report -
org.apache.dubbo.config.MetadataReportConfig - dubbo.protocol -
org.apache.dubbo.config.ProtocolConfig - dubbo.provider -
org.apache.dubbo.config.ProviderConfig - dubbo.registry -
org.apache.dubbo.config.RegistryConfig - dubbo.metrics -
org.apache.dubbo.config.MetricsConfig - dubbo.tracing -
org.apache.dubbo.config.TracingConfig - dubbo.ssl -
org.apache.dubbo.config.SslConfig dubbo.monitor -org.apache.dubbo.config.MonitorConfigdubbo.module -org.apache.dubbo.config.ModuleConfig
dubbo.metrics
- dubbo.metrics.aggregation -
org.apache.dubbo.config.nested.AggregationConfig - dubbo.metrics.histogram -
org.apache.dubbo.config.nested.HistogramConfig - dubbo.metrics.prometheus -
org.apache.dubbo.config.nested.PrometheusConfig - dubbo.metrics.prometheus.exporter -
org.apache.dubbo.config.nested.PrometheusConfig$Exporter - dubbo.metrics.prometheus.pushgateway -
org.apache.dubbo.config.nested.PrometheusConfig$Pushgateway
dubbo.tracing
- dubbo.tracing.baggage.correlation -
org.apache.dubbo.config.nested.BaggageConfig$Correlation - dubbo.tracing.tracing-exporter.otlp-config -
org.apache.dubbo.config.nested.ExporterConfig$OtlpConfig - dubbo.tracing.tracing-exporter.zipkin-config -
org.apache.dubbo.config.nested.ExporterConfig$ZipkinConfig - dubbo.tracing.baggage -
org.apache.dubbo.config.nested.BaggageConfig - dubbo.tracing.propagation -
org.apache.dubbo.config.nested.PropagationConfig - dubbo.tracing.sampling -
org.apache.dubbo.config.nested.SamplingConfig - dubbo.tracing.tracing-exporter -
org.apache.dubbo.config.nested.ExporterConfig
starter列表
dubbo-spring-boot-starter
以下是一些 dubbo-spring-boot-starter 版本对应的 SpringBoot、JDK 依赖:
| 版本 | 兼容 Spring Boot 范围 |
|---|---|
| 3.3.x | [1.x ~ 3.x) |
| 3.2.x | [1.x ~ 3.x) |
| 3.1.x | [1.x ~ 2.x) |
| 2.7.x | [1.x ~ 2.x) |
其他组件starter
以下是 Dubbo 官方社区提供的 starter 列表(3.3.0+ 版本),方便在 Spring Boot 应用中快速使用:
dubbo-spring-boot-starter,管理 dubbo 核心依赖,用于识别 application.properties 或 application.yml 中dubbo.开头的配置项,扫描 @DubboService 等注解。dubbo-spring-boot-starter3,管理 dubbo 核心依赖,与 dubbo-spring-boot-starter 相同,支持 spring boot 3.2 版本。dubbo-nacos-spring-boot-starter,管理 nacos-client 等依赖,使用 Nacos 作为注册中心、配置中心时引入。dubbo-zookeeper-spring-boot-starter,管理 zookeeper、curator 等依赖,使用 Zookeeper 作为注册中心、配置中心时引入(Zookeeper server 3.4 及以下版本使用)。dubbo-zookeeper-curator5-spring-boot-starter,管理 zookeeper、curator5 等依赖,使用 Zookeeper 作为注册中心、配置中心时引入。dubbo-sentinel-spring-boot-starter,管理 sentinel 等依赖,使用 Sentinel 进行限流降级时引入。dubbo-seata-spring-boot-starter,管理 seata 等依赖,使用 Seata 作为分布式事务解决方案时引入。dubbo-observability-spring-boot-starter,加入该依赖将自动开启 Dubbo 内置的 metrics 采集,可用于后续的 Prometheus、Grafana 等监控系统。dubbo-tracing-brave-spring-boot-starter,管理 brave/zipkin、micrometer 等相关相关依赖,使用 Brave/Zipkin 作为 Tracer,将 Trace 信息 export 到 Zipkin。dubbo-tracing-otel-otlp-spring-boot-starter,管理 brave/zipkin、micrometer 等相关相关依赖,使用 OpenTelemetry 作为 Tracer,将 Trace 信息 export 到 OTlp Collector。dubbo-tracing-otel-zipkin-spring-boot-starter,管理 brave/zipkin、micrometer 等相关相关依赖,使用 OpenTelemetry 作为 Tracer,将 Trace 信息 export 到 Zipkin。
评论
评论加载中……