dubbo整合springcloud实现RPC远程调用

矫情吗;* 2023-10-12 13:27 155阅读 0赞

本项目是基于nacos作为注册中心实现的dubbo实现RPC远程调用,没有接触到nacos的小伙伴可以先对nacos进行学习。

项目目录结构

10f2e8f272bc4fa5b335c3a8a88613fd.png

父项目的pom文件

  1. <properties>
  2. <java.version>1.8</java.version>
  3. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  4. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  5. <spring-boot.version>2.3.0.RELEASE</spring-boot.version>
  6. </properties>
  7. <modules>
  8. <module>springcloud-dubbo-sample-api</module>
  9. <module>springcloud-dubbo-sample-provider</module>
  10. <module>springcloud-dubbo-sample-consumer</module>
  11. </modules>
  12. <dependencyManagement>
  13. <dependencies>
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-dependencies</artifactId>
  17. <version>${spring-boot.version}</version>
  18. <type>pom</type>
  19. <scope>import</scope>
  20. </dependency>
  21. </dependencies>
  22. </dependencyManagement>

统一访问接口API

  1. public interface ISayHelloService {
  2. String sayHello();
  3. }

provide服务提供者

pom文件

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter</artifactId>
  5. </dependency>
  6. <!-- <dependency>-->
  7. <!-- <groupId>org.apache.httpcomponents</groupId>-->
  8. <!-- <artifactId>httpclient</artifactId>-->
  9. <!-- <version>4.5.8</version>-->
  10. <!-- </dependency>-->
  11. <dependency>
  12. <groupId>com.jgh</groupId>
  13. <artifactId>springcloud-dubbo-sample-api</artifactId>
  14. <version>1.0-SNAPSHOT</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>com.alibaba.cloud</groupId>
  18. <artifactId>spring-cloud-starter-dubbo</artifactId>
  19. <version>2.2.1.RELEASE</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>com.alibaba.cloud</groupId>
  23. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  24. <version>2.2.1.RELEASE</version>
  25. </dependency>
  26. </dependencies>

暴露接口

  1. import com.jgh.dubbo.service.ISayHelloService;
  2. import org.apache.dubbo.config.annotation.Service;
  3. /**
  4. * @program: dubbo-springcloud-example
  5. * @description:
  6. * @author: 是小浩呀~
  7. * @create: 2023-06-10 09:10
  8. **/
  9. @Service
  10. public class sayHelloServiceImpl implements ISayHelloService {
  11. public String sayHello() {
  12. return "hello~!!!!";
  13. }
  14. }

注意:此处的@Service注解不是spring官方加载spring容器的bean配置的Service,而是dubbo用来暴露服务的Service注解

application.properties配置文件(nacos配置成自己的nacos地址)

  1. spring.application.name=spring-cloud-dubbo-sample-provider
  2. dubbo.scan.base-packages=com.jgh.dubbo.service.impl
  3. dubbo.protocol.port=20882
  4. dubbo.protocol.name=dubbo
  5. spring.cloud.nacos.discovery.server-addr=192.168.56.1:8848

springboot启动类

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  4. /**
  5. * @program: dubbo-springcloud-example
  6. * @description:
  7. * @author: 是小浩呀~
  8. * @create: 2023-06-10 09:26
  9. **/
  10. @EnableDiscoveryClient
  11. @SpringBootApplication
  12. public class App {
  13. public static void main(String[] args) {
  14. SpringApplication.run(App.class,args);
  15. }
  16. }

consumer服务消费者

pom文件

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.alibaba.cloud</groupId>
  4. <artifactId>spring-cloud-starter-dubbo</artifactId>
  5. <version>2.2.1.RELEASE</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter</artifactId>
  10. </dependency>
  11. <dependency>
  12. <groupId>com.alibaba.cloud</groupId>
  13. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  14. <version>2.2.1.RELEASE</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-web</artifactId>
  19. </dependency>
  20. <dependency>
  21. <groupId>com.jgh</groupId>
  22. <artifactId>springcloud-dubbo-sample-api</artifactId>
  23. <version>1.0-SNAPSHOT</version>
  24. </dependency>
  25. </dependencies>

application.properties文件

  1. spring.application.name=spring-cloud-dubbo-sample-consumer
  2. spring.cloud.nacos.discovery.server-addr=192.168.56.1:8848

springboot启动类

为了方便,直接在启动类上添加测试接口@RestController,请各位自行优化

  1. import com.jgh.dubbo.service.ISayHelloService;
  2. import org.apache.dubbo.config.annotation.Reference;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. /**
  8. * @program: dubbo-springcloud-example
  9. * @description:
  10. * @author: 是小浩呀~
  11. * @create: 2023-06-10 09:45
  12. **/
  13. @RestController
  14. @SpringBootApplication
  15. public class App {
  16. public static void main(String[] args) {
  17. SpringApplication.run(App.class,args);
  18. }
  19. @Reference
  20. ISayHelloService sayHelloService;
  21. @GetMapping("/say")
  22. public String say(){
  23. String s = sayHelloService.sayHello();
  24. return s;
  25. }
  26. }

其中,@Reference是获取服务提供者注册在nacos的服务,该过程使用动态代理的方式实现。

最后最后,先启动provide,在启动consumer。然后就能成功的调用啦。。。

发表评论

表情:
评论列表 (有 0 条评论,155人围观)

还没有评论,来说两句吧...

相关阅读