springboot集成dubbo实现微服务系统

server/2024/9/23 9:14:20/

目录

1.说明

2.示例

3.总结


1.说明

dubbo官网:https://cn.dubbo.apache.org/zh-cn/

Apache Dubbo 是一款 RPC 服务开发框架,用于解决微服务架构下的服务治理与通信问题,支持多种语言,官方提供了 Java、Golang 等多语言 SDK 实现。使用 Dubbo 开发的微服务原生具备相互之间的远程地址发现与通信能力, 利用 Dubbo 提供的丰富服务治理特性,可以实现诸如服务发现、负载均衡、流量调度等服务治理诉求。Dubbo 被设计为高度可扩展,用户可以方便的实现流量拦截、选址的各种定制逻辑。

2.示例

实现说明:

        创建一个空项目,在空项目中创建3个模块,分别定义接口工程,生产者工程及消费者工程。并在生产者工程及消费者工程中引入接口工程。

        接口工程存放表的实体类及服务接口。

        生产者工程提供服务接口的实现。

        消费者工程调用服务接口。

实现步骤:

①引入dubbo依赖

        <!-- Dubbo Spring Boot Starter --><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>2.7.8</version></dependency><dependency><!--zookerper版本一定要匹配! --><groupId>org.apache.dubbo</groupId><artifactId>dubbo-registry-zookeeper</artifactId><version>2.7.8</version></dependency>

 ②在接口工程中创建接口

package com.example.service;public interface PrivoderService {String getInfo();
}

③在生产者工程中实现接口,并进行dubbo的配置

接口实现:使用dbboservice注解,将服务的实现暴露给dubbo

package com.example.provider.service.impl;import com.example.service.PrivoderService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;/*** @Author linaibo* @Date 2023/11/18 15:28* @Version 1.0*/
@Service
@DubboService
public class PrividerServiceImpl implements PrivoderService {@Overridepublic String getInfo() {return "执行成功";}
}

 配置文件:

server:port: 8881
dubbo:application:name: provider-service //dubbo的应用名registry:protocol: zookeeper //使用zookeeper作为服务的注册中心address: 127.0.0.1:2181 //zookeeper地址protocol:name: dubbo //使用dubbo协议port: 20885consumer:timeout: 60000 //调用接口的超时时间check: false //启动时不校验消费者是否已启动
spring:datasource:url: jdbc:mysql://localhost:3306/ry-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8username: rootpassword: 123456
mybatis:mapper-locations: classpath*:mapper/*Mapper.xmltype-aliases-package: com.**.domain

启动类配置:添加@EnableDubbo,用于将dubbo相关的配置bean加载到spring容器

package com.example.provider;import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @Author linaibo* @Date 2023/11/18 15:32* @Version 1.0*/
@SpringBootApplication
@EnableDubbo
public class ProviderApplication {public static void main(String[] args) {SpringApplication.run(ProviderApplication.class, args);}
}

 ④生产者工程中调用接口

调用:使用DubboReference指定调用的服务

package com.example.consumer.service.impl;import com.example.consumer.service.ConsumerService;
import com.example.domain.AjaxResult;
import com.example.service.ISysConfigService;
import com.example.service.PrivoderService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;import static com.example.domain.AjaxResult.success;/*** @Author linaibo* @Date 2023/11/18 15:56* @Version 1.0*/
@Service
public class ConsumerServiceImpl implements ConsumerService {@DubboReferenceprivate PrivoderService privoderService;@DubboReferenceprivate ISysConfigService sysConfigService;@Overridepublic String getInfo() {String info = privoderService.getInfo();return info;}@Overridepublic AjaxResult getConfig(Long configId) {return success(sysConfigService.selectConfigById(configId));}
}

配置文件及启动类配置和生产者工程一致

启动zookeeper服务及生产者工程及消费者工程,就可以进行服务的调用。

3.总结

可以通过dubbo-admin进行服务的管理及查看。

dubbo.consumer.timeout:调用超时时间(毫秒),默认为 1000。debug模式下会导致调用失败,所以需要调大。

dubbo.consumer.check:为true时,开启服务启动时检查依赖的服务是否可用,默认为 true。

也就是说,生产者没有启动时,消费者无法启动,需要设置为false

参照:SpringBoot整合dubbo+zooker搭建分布式服务(超详细)_springboot+dubbo分布式项目-CSDN博客

SpringBoot项目集成Dubbo_springboot集成dubbo-CSDN博客


http://www.ppmy.cn/server/41973.html

相关文章

Linux|如何允许 awk 使用 Shell 变量

引言 当我们编写 shell 脚本时&#xff0c;我们通常会在脚本中包含其他较小的程序或命令&#xff0c;例如 awk 操作。就 Awk 而言&#xff0c;我们必须找到将一些值从 shell 传递到 Awk 操作的方法。 这可以通过在 Awk 命令中使用 shell 变量来完成&#xff0c;在本文中&#x…

如何在阿里云申请免费SSL证书(三个月有效)

SSL证书主要用于建立Web服务器和客户端间可信的HTTPS协议加密链接&#xff0c;以防止数据在传输过程中被篡改&#xff0c;避免信息泄露。阿里云提供了多种品牌和类型的SSL证书&#xff0c;以满足不同用户的需求。您可以根据自己的预算、域名类型以及网站类型&#xff0c;选择购…

Redis20种使用场景

Redis20种使用场景 1缓存2抽奖3Set实现点赞/收藏功能4排行榜5PV统计&#xff08;incr自增计数&#xff09;6UV统计&#xff08;HeyperLogLog&#xff09;7去重&#xff08;BloomFiler&#xff09;8用户签到&#xff08;BitMap&#xff09;9GEO搜附近10简单限流11全局ID12简单分…

机器学习(3)

目录 3-1线性回归 3-2最小二乘解 3-3多元线性回归 3-4广义线性模型 3-5对率回归 3-6对率回归求解 3-7线性判别分析 3-8LDA的多类推广 3-9多分类学习基本思路 3-10类别不平衡 3-1线性回归 线性模型为什么重要&#xff1f; 人类在考虑问题时&#xff0c;通常…

创新指南 | 企业AI战略 实施方案探讨(上):如何构建基于AI的新商业模型和业务场景

2023年以ChatGPT为代表的生成式AI推出以来&#xff0c;从投资界到企业界都掀起了一股热潮。那么从企业角度来看&#xff0c;生成式AI到底能为业务带来哪些增量呢&#xff1f;企业如何构建基于AI的商业模式并进行落地实施呢&#xff1f; 企业AI战略 实施方案探讨分为上下两篇&am…

人脸识别之bbox【det_10g】-ncnn(c++)

模型描述 det_10g是insightface 人脸框图和人脸关键点的分类&#xff0c;最终能够得到人脸框图bbox&#xff0c;分值还有人脸五官&#xff08;眼x2、鼻子x1、嘴巴x2&#xff09; 由于我这里没有采用最终结果&#xff0c;通过onnx转换为ncnn&#xff0c;所以后面的步骤结果丢弃…

安全关闭Tcp连接

close与shutdwon int close(int sockfd);关闭sokcet&#xff0c;这里注意&#xff1a;当程序调用close关闭socket的时候,如果缓冲区中仍然有数据的话,协议栈会发送RST包代替FIN包&#xff0c;丢弃缓冲的数据&#xff0c;强行关闭连接 int shutdown(int sockfd, int howto);该…

太阳能无人机的多元化应用

随着新能源技术的不断发展和成熟&#xff0c;太阳能在无人机的应用技术已经成熟。太阳能无人机得到了量产和广泛的应用。传统无人机相比&#xff0c;太阳能无人机无需燃油&#xff0c;运行费用低廉&#xff0c;搭载多种高科技设备&#xff0c;能够高效、多元化地采集和分析各类…