阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

SpringBoot集成Apache Dubbo

195次阅读
没有评论

共计 5815 个字符,预计需要花费 15 分钟才能阅读完成。

1.Apache Dubbo 的前身 -Dubbo

Dubbo 是阿里巴巴内部使用的一个分布式服务治理框架, 于 2012 年开源。

2018 年 2 月份,Dubbo 进入 Apache 孵化,2019 年 5 月,Apache Dubbo 框架正式从孵化器中毕业, 代表着 Apache Dubbo 正式成为 Apache 的顶级项目

2.Apache Dubbo 概述

Apache Dubbo 是一个分布式服务框架, 主要实现多个系统之间的高性能、透明化调用, 简单来说它是一个 RPC 框架,但是和普通的 RPC 框架不同的是, 它提供了服务治理功能, 比如服务注册、监控、路由、容错等。

3.Spring Boot 集成 Apache Dubbo

3.1 开发服务提供者

步骤:

1. 创建一个普通的 Maven 工程 springboot-provider

2. 添加依赖

<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring-boot.version>2.2.2.RELEASE</spring-boot.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>

2. 添加子模块 sample-api

3. 添加依赖

<parent> <groupId>com.tyschool</groupId> <artifactId>springboot-provider</artifactId> <version>1.0-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> <plugin> <artifactId>maven-site-plugin</artifactId> <version>3.7.1</version> </plugin> <plugin> <artifactId>maven-project-info-reports-plugin</artifactId> <version>3.0.0</version> </plugin> </plugins> </pluginManagement> </build>

4. 在子模块 sample-api 中添加接口 IHelloService

public interface IHelloService {String sayHello(String name); }

5. 将 sample-api 安装到本地仓库

6. 添加子模块 sample-provider, 此模块为 springboot 工程

7. 添加依赖

<parent> <groupId>com.tyschool</groupId> <artifactId>springboot-provider</artifactId> <version>1.0-SNAPSHOT</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.5</version> </dependency> <dependency> <groupId>com.tyschool</groupId> <version>1.0-SNAPSHOT</version> <artifactId>sample-api</artifactId> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.5.3-beta</version> </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> <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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

8. 添加 HelloServiceImpl

@Service public class HelloServiceImpl implements IHelloService {@Value("${spring.application.name}") private String serviceName; @Override public String sayHello(String name) {return serviceName; } }

9. 添加配置信息

spring.application.name=sample-provider
#提供方应用信息
dubbo.application.name=springboot-provider
# Dubbo 服务暴露的协议配置,其中子属性 name 为协议名称,port 为协议端口(-1 表示自增端口,从 20880 开始)dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
# 服务注册中心的地址,N/ A 表示不注册
dubbo.registry.address=N/A

10. 修改启动类

@DubboComponentScan @SpringBootApplication public class SampleProviderApplication {public static void main(String[] args) {SpringApplication.run(SampleProviderApplication.class, args); } }

3.2 开发服务消费者

步骤:

1. 创建 springboot 工程:springboot-consumer

2. 添加依赖

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.5</version> </dependency> <dependency> <groupId>com.tyschool</groupId> <version>1.0-SNAPSHOT</version> <artifactId>sample-api</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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

3. 添加 controller, 调用服务提供者

@RestController public class HelloController {@Reference(url = "dubbo://localhost:20880/com.tyschool.service.IHelloService") private IHelloService helloService; @GetMapping("/hello") public String hello(){return helloService.sayHello("ss"); } }

4. 添加配置

spring.application.name=springboot-consumer
# dubbo 服务扫描基础包路径
dubbo.scan.base-packages=com.tyschool
dubbo.application.name=springboot-consumer

3.3 测试

步骤:

1. 启动服务提供者

2. 启动服务调用者

3. 访问服务调用者的 controller

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-06-06发表,共计5815字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中