组团学

SpringBoot集成Apache Dubbo

阅读 (9844380)

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

需要 登录 才可以提问哦