dubbo实战之二:与SpringBoot集成

  1. 创建子工程springbootmulticastconsumer,启动后提供一个web接口,咱们调用这个web接口时,springbootmulticastconsumer会远程调用springbootmulticastprovider提供的服务,如下图:

在这里插入图片描述

  1. 本篇的实战暂不使用注册中心,而是服务提供方启动时广播自己的地址,再由消费方启动时订阅,并随时远程调用,调用逻辑如下图所示:

在这里插入图片描述

[](()源码下载

  1. 如果您不想编码,可以在GitHub下载所有源码,地址和链接信息如下表所示:

| 名称 | 链接 | 备注 |

| :-- | :-- | :-- |

| 项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |

| git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |

| git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |

  1. 这个git项目中有多个文件夹,本章的应用在dubbopractice文件夹下,如下图红框所示:

在这里插入图片描述

  1. dubbopractice是父子结构的工程,本篇的代码在springbootmulticastprovider和springbootmulticastconsumer这两个子工程中,如下图:

在这里插入图片描述

[](()编码(服务提供方)

  • 先创建提供服务的工程springbootmulticastprovider,一共要创建4个文件,创建顺序和功能如下表:

| 创建顺序 | 文件名 | 作用 |

| — | — | — |

| 1 | pom.xml | 工程的pom文件 |

| 2 | src/main/resources/application.yml | 配置文件 |

| 3 | DemoServiceImpl.java | 提供具体的服务 |

| 4 | SpringBootMulticastProviderApplication.java | 启动类 |

  • 完整的文件位置如下图:

在这里插入图片描述

  • 接下来逐个创建上述内容;
  1. 创建名为springbootmulticastprovider的子工程,pom.xml内容如下,要重点关注的是新增依赖dubbo-spring-boot-starter,这就是dubbo在SpringBoot环境的starter依赖:
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns=“http://maven.apache.org/POM/4.0.0”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>

dubbopractice

com.bolingcavalry

1.0-SNAPSHOT

4.0.0

com.bolingcavalry

springbootmulticastprovider

1.0-SNAPSHOT

springbootmulticastprovider

Demo project for dubbo service provider from Spring Boot, multicast mode

org.springframework.boot

spring-boot-dependencies

${springboot.version}

pom

import

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

com.bolingcavalry

practiceinterface

${project.version}

org.projectlombok

lombok

</de 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 pendency>

org.apache.dubbo

dubbo-spring-boot-starter

org.springframework.boot

spring-boot-maven-plugin

${springboot.version}

  1. 配置文件application.yml,要注意的是registry.address的配置是广播模式:

dubbo:

application:

#application-name 本模块名字

name: springboot-multicast-provider

id: springboot-multicast-provider

registry:

address: multicast://224.5.6.7:1234

id: registry

protocol:

name: dubbo

port: 20880

  1. 编写服务实现类DemoServiceImpl.java,注意@Service注解将当前类的实例作为远程服务对外暴露:

package com.bolingcavalry.springbootmulticastprovider;

import com.bolingcavalry.dubbopractice.service.DemoService;

import lombok.extern.slf4j.Slf4j;

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

import org.apache.dubbo.rpc.RpcContext;

@Slf4j

@Service

public class DemoServiceImpl implements DemoService {

@Override

public String sayHello(String name) {

log.info("I’m springboot-multicast-provider, Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

return "I’m springboot-multicast-provider, Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();

}

}

  1. 编写SpringBoot启动类SpringBootMulticastProviderApplication.java,注意要添加@EnableDubbo注解:

package com.bolingcavalry.springbootmulticastprovider;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

@EnableDubbo

public class SpringBootMulticastProviderApplication {

public static void main(String[] args) {

SpringApplication.run(SpringBootMulticastProviderApplication.class, args);

}

}

  1. 至此服务提供方编码完成,直接在IDEA上运行SpringBootMulticastProviderApplication类即可启动服务,启动成功后的日志输出如下图:

在这里插入图片描述

[](()编码(服务消费方)

  • 现在网络上已经有了服务,咱们再来编写服用消费方的代码,一共要创建6个文件,创建顺序和功能如下表:

| 创建顺序 | 文件名 | 作用 |

| — | — | — |

| 1 | pom.xml | 工程的pom文件 |

| 2 | src/main/resources/application.yml | 配置文件 |

| 3 | RemoteInvokeServiceImpl.java | service层,在这里远程调用服务提供方的服务 |

| 4 | DemoController.java | web接口类,对外提供web服务 |

| 5 | SwaggerConfig.java | swagger配置类,便于通过页面测试接口 |

| 6 | SpringBootMulticastConsumerApplication.java | 启动类 |

  • 完整的文件位置如下图:

在这里插入图片描述

  • 接下来逐个创建上述文件;
  1. 创建名为springbootmulticastconsumer的子工程,pom.xml内容如下,同样需要依赖dubbo-spring-boot-starter:
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns=“http://maven.apache.org/POM/4.0.0”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>

dubbopractice

com.bolingcavalry

1.0-SNAPSHOT

4.0.0

com.bolingcavalry

springbootmulticastconsumer

1.0-SNAPSHOT

springbootmulticastconsumer

Demo project for dubbo service consumer from Spring Boot, multicast mode

org.springframework.boot

spring-boot-dependencies

${springboot.version}

pom

import

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

io.springfox

springfox-swagger2

io.springfox

springfox-swagger-ui

com.bolingcavalry

practiceinterface

${project.version}

org.projectlombok

lombok

org.apache.dubbo

dubbo-spring-boot-starter

org.springframework.boot

spring-boot-maven-plugin

${springboot.version}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值