HOME> 世界杯意大利名单> Dubbo(从入门到掌握)看完这一篇就够了

Dubbo(从入门到掌握)看完这一篇就够了

2025-12-19 16:31:45

文章目录

一、 Dubbo概述1.Dubbo概念2.Dubbo架构3.Dubbo快速入门

二、springboot项目整合dubbo1.创建生产者服务(1)项目路径总览(2)添加项目依赖(3)创建UserService接口,并创建sayHello方法(4)创建UserServiceImpl实现类,并实现sayHello方法(5)配置yaml文件(6)项目pom文件

2.创建消费者服务(1)项目路径总览(2)添加项目依赖(3)创建UserController类,并创建sayHello接口(5)配置yaml文件(6)项目pom文件

3.测试4.源码压缩包如下

三、dubbo-admin四、 Dubbo 高级特性1.序列化2.地址缓存3.超时与重试(1)设置超时和重试机制(2)超时和重试代码

4.多版本(1)代码如下

5.负载均衡(1)代码如下

5.集群容错(1)代码如下

「其他文章」 【Zookeeper(从入门到掌握)看完这一篇就够了 https://blog.csdn.net/weixin_45404884/article/details/137697459】

一、 Dubbo概述

1.Dubbo概念

Dubbo是阿里巴巴公司开源的一个高性能、轻量级的 Java RPC 框架。致力于提供高性能和透明化的 RPC 远程服务调用方案,以及 SOA 服务治理方案。官网:http://dubbo.apache.org

2.Dubbo架构

节点角色说明:

Provider:暴露服务的服务提供方Container:服务运行容器Consumer:调用远程服务的服务消费方Registry:服务注册与发现的注册中心Monitor:统计服务的调用次数和调用时间的监控中心

3.Dubbo快速入门

创建服务提供者Provider模块创建服务消费者Consumer模块在服务提供者模块编写 UserServiceImpl 提供服务在服务消费者中的 UserController 远程调用UserServiceImpl 提供的服务分别启动两个服务,测试。

二、springboot项目整合dubbo

1.创建生产者服务

dubbo-service代表服务生产者

(1)项目路径总览

(2)添加项目依赖

org.apache.dubbo

dubbo-dependencies-zookeeper

2.7.4.1

pom

org.slf4j

slf4j-log4j12

org.apache.dubbo

dubbo-spring-boot-starter

2.7.4.1

(3)创建UserService接口,并创建sayHello方法

package com.example.service;

public interface UserService {

public String sayHello();

}

(4)创建UserServiceImpl实现类,并实现sayHello方法

package com.example.service.impl;

import com.example.service.UserService;

import org.apache.dubbo.config.annotation.Service;

@Service //导入dubbo的service注解,将这个类提供的方法对外发布,注册到注册中心中

public class UserServiceImpl implements UserService {

@Override

public String sayHello() {

return "hello dubbo !";

}

}

(5)配置yaml文件

dubbo:

registry:

address: zookeeper://127.0.0.1:2181 #自己的zookeeper服务器的IP:默认端口号

application:

name: dubbo-zookeeper-producer1 #注册进去的名字

protocol:

name: dubbo #设置类型

port: -1 #因为dubbo的服务器端口号是不能唯一的,所以,设置为-1会帮我们自动改变端口号

config-center:

timeout: 120000 #超时时间 (毫秒)

server:

port: 9001

(6)项目pom文件

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.7.11

com.example

dubbo-service

0.0.1-SNAPSHOT

dubbo-service

Demo project for Spring Boot

1.8

7.12.1

4.0.0

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-web-services

org.springframework.boot

spring-boot-starter-test

test

junit

junit

org.springframework.boot

spring-boot-test

org.apache.dubbo

dubbo-dependencies-zookeeper

2.7.4.1

pom

org.slf4j

slf4j-log4j12

org.apache.dubbo

dubbo-spring-boot-starter

2.7.4.1

${project.artifactId}

org.apache.maven.plugins

maven-compiler-plugin

1.8

1.8

org.apache.maven.plugins

maven-surefire-plugin

true

2.创建消费者服务

dubbo-web代表服务消费者

(1)项目路径总览

(2)添加项目依赖

org.apache.dubbo

dubbo-dependencies-zookeeper

2.7.4.1

pom

org.slf4j

slf4j-log4j12

org.apache.dubbo

dubbo-spring-boot-starter

2.7.4.1

(3)创建UserController类,并创建sayHello接口

import com.example.service.UserService;

import org.apache.dubbo.config.annotation.Reference;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

@RequestMapping("/user")

public class UserController {

@Reference //dubbo 的 Reference

private UserService userService;

@RequestMapping("/sayHello")

public String sayHello() {

return userService.sayHello();

}

}

(5)配置yaml文件

dubbo:

application:

name: dubbo-zookeeper-consumer1

protocol:

name: dubbo

port: -1

registry:

address: zookeeper://127.0.0.1:2181 #默认端口号为2181

config-center:

timeout: 12000 #超时时间

server:

port: 8083

(6)项目pom文件

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.7.11

com.example

dubbo-web

0.0.1-SNAPSHOT

dubbo-web

Demo project for Spring Boot

1.8

7.12.1

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-web-services

org.springframework.boot

spring-boot-starter-test

test

junit

junit

org.springframework.boot

spring-boot-test

com.example

dubbo-service

0.0.1-SNAPSHOT

compile

org.apache.dubbo

dubbo-dependencies-zookeeper

2.7.4.1

pom

org.slf4j

slf4j-log4j12

org.apache.dubbo

dubbo-spring-boot-starter

2.7.4.1

${project.artifactId}

org.apache.maven.plugins

maven-compiler-plugin

1.8

1.8

org.apache.maven.plugins

maven-surefire-plugin

true

3.测试

先启动zookeeper 运行zkServer.cmd再启动服务生产者再启动服务消费者最后访问http://localhost:8083/user/sayHello查看效果

4.源码压缩包如下

https://pan.baidu.com/s/1lJBViy5_ipHYoKoQI3AC6w?pwd=gbui

三、dubbo-admin

dubbo-admin 管理平台,是图形化的服务管理页面从注册中心中获取到所有的提供者 / 消费者进行配置管理路由规则、动态配置、服务降级、访问控制、权重调整、负载均衡等管理功能dubbo-admin 是一个前后端分离的项目。前端使用vue,后端使用springboot安装 dubbo-admin 其实就是部署该项目

dubbo-admin其实就是dubbo服务的管理平台,一个可视化工具,可以自行下载

四、 Dubbo 高级特性

1.序列化

dubbo 内部已经将序列化和反序列化的过程内部封装了我们只需要在定义pojo类时实现Serializable接口即可一般会定义一个公共的pojo模块,让生产者和消费者都依赖该模块。

2.地址缓存

假如注册中心挂了,服务是否可以正常访问?

可以,因为dubbo服务消费者在第一次调用时,会将服务提供方地址缓存到本地,以后在调用则不会访问注册中心。 当服务提供者地址发生变化时,注册中心会通知服务消费者。

3.超时与重试

服务消费者在调用服务提供者的时候发生了阻塞、等待的情形,这个时候,服务消费者会一直等待下去。在某个峰值时刻,大量的请求都在同时请求服务消费者,会造成线程的大量堆积,势必会造成雪崩。 解决办法:

(1)设置超时和重试机制

dubbo 利用超时机制来解决这个问题,设置一个超时时间,在这个时间段内,无法完成服务访问,则自动断开连接。使用timeout属性配置超时时间,默认值1000,单位毫秒。设置了超时时间,在这个时间段内,无法完成服务访问,则自动断开连接。如果出现网络抖动,则这一次请求就会失败。Dubbo 提供重试机制来避免类似问题的发生。通过 retries 属性来设置重试次数。默认为 2 次。

(2)超时和重试代码

其实就是在service上加配置属性,

@Service(timeout=3000) @Reference(timeout=3000)

@Service(retries=2) @Reference(retries=2)

4.多版本

灰度发布:当出现新功能时,会让一部分用户先使用新功能,用户反馈没问题时,再将所有用户迁移到新功能。dubbo 中使用version 属性来设置和调用同一个接口的不同版本

可以控制服务使用范围

(1)代码如下

其实就是在服务生产者的service注解和服务消费者的Reference注解上加version属性,并确保值相同,才能访问的到。

@Service(version="1")

@Reference(version="1")

5.负载均衡

负载均衡策略(4种):

Random :按权重随机,默认值。按权重设置随机概率。RoundRobin :按权重轮询。LeastActive:最少活跃调用数,相同活跃数的随机。ConsistentHash:一致性 Hash,相同参数的请求总是发到同一提供者。

(1)代码如下

再在生产者的service注解上加loadbalance属性,默认是random随机负载

@Service(loadbalance="random")

@Reference(loadbalance="random")

5.集群容错

集群容错模式:

Failover Cluster:失败重试。默认值。当出现失败,重试其它服务器 ,默认重试2次,使用 retries 配置。一般用于读操作Failfast Cluster :快速失败,只发起一次调用,失败立即报错。通常用于写操作。Failsafe Cluster :失败安全,出现异常时,直接忽略。返回一个空结果。Failback Cluster :失败自动恢复,后台记录失败请求,定时重发。通常用于消息通知操作。Forking Cluster :并行调用多个服务器,只要一个成功即返回。Broadcast Cluster :广播调用所有提供者,逐个调用,任意一台报错则报错。

(1)代码如下

@Service(cluster="failfast")

@Reference(cluster="failfast")

华硕傲世一体机
小米耳机报价