springcloud如何使用dubbo开发rpc服务及调用

网友投稿 257 2022-12-15


springcloud如何使用dubbo开发rpc服务及调用

这篇文章主要介绍了springcloud如何使用dubbo开发rpc服务及调用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

spring cloud中基于springboot开发的微服务,是基于http的rest接口,也可以开发基于dubbo的rpc接口。

一,创建goodsService模块

1, 在创建的goodsService模块中再创建goodsServiceApi和goodsServiceServer模块

2,在oodsServiceApi模块中定义接口 ,goodsServiceServer用于接口实现

3,goodsServiceServer模块中pom文件引入相关依赖

net.biui

goods-service-api

1.0-SNAPSHOT

com.alibaba.cloud

spring-cloud-starter-alibaba-nacos-discovery

com.alibaba.cloud

spring-cloud-starter-dubbo

4,goodsServiceServer中添加配置

spring:

application:

name: goods-service

cloud:

nacos:

discovery:

server-addr: 127.0.0.1:8848

namespace: c22e5019-0bee-43bfteVua1-b80b-fc0b9d847501

dubbo:

registry:

address: nacos://127.0.0.1:8848

scan:

fteVuabase-packages: net.biui.impl

protocol:

port: 20881

name: dubbo

5,goodsServiceServer编写接口实现

@org.apache.dubbo.config.annotation.Service

public class GoodsImpl implements GoodsApi {

public String getGoodsName() {

return "商品一";

}

}

6,goodsServiceServer编写启动类

@SpringBootApplication

@EnableDiscoveryClient

public class GoodsServiceServerApplication {

public static void main(String[] args) {

SpringApplication.run(GoodsServiceServerApplication.class, args);

}

}

启动后,dubbo服务会自动注册到nacos服务发现中心

二,创建调用dubbo服务的模块

1,new -> module -> 填写信息 -> finish

2,添加pom依赖

org.springframework.boot

spring-boot-starter-web

com.alibaba.cloud

spring-cloud-starter-alibaba-nacos-discovery

com.alibaba.cloud

spring-cloud-starter-dubbo

net.biui

goods-service-api

1.0-SNAPSHOT

3,添加配置

spring:

application:

name: demo-dubbo

cloud:

nacos:

discovery:

server-addr: 127.0.0.1:8848

namespace: c22e5019-0bee-43b1-b80b-fc0b9d847501

4,编写controller调用dubbo服务

@RestController

@RequestMapping("/demo")

public class demoController {

@org.apache.dubbo.config.annotation.Reference

GoodsApi goodsApi;

@GetMapping("/test")

public String test(){

return "test " + goodsApi.getGoodsName();

}

}

5,编写启动类

@SpringBootApplication

@EnableDiscoveryClient

public class demoDubboApplication {

public static void main(String[] args) {

SpringApplication.run(demoDubboApplication.class, args);

}

}

启动后,demo-dubbo服务也会自动注册到nacos(因为nacos.register.enable默认为true,即代表自动注册,可以只订阅,不注册),对应接口返回了dubbo服务返回的信息!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java通过Scanner了解if...else if语句
下一篇:spring cloud gateway网关路由分配代码实例解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~