Spring Cloud Alibaba和Dubbo融合实现

网友投稿 341 2022-12-09


Spring Cloud Alibaba和Dubbo融合实现

服务提供者

创建一个名为 hello-dubbo-nacos-provider 的服务提供者项目

POM

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

4.0.0

org.springframework.boot

spring-boot-starter-parent

http://2.1.6.RELEASE

com.antoniopeng

hello-dubbo-nacos-provider

pom

hello-dubbo-nacos-provider-api

hello-dubbo-nacos-provider-service

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

4.0.0

org.springframework.boot

spring-boot-starter-parent

http://2.1.6.RELEASE

com.antoniopeng

hello-dubbo-nacos-provider

pom

hello-dubbo-nacos-provider-api

hello-dubbo-nacos-provider-service

该项目下有两个子模块,分别是 hello-dubbo-nacos-provider-api 和 hello-dubbo-nacos-provider-service,前者用于定义接口,后者用于实现接口。

服务提供者接口模块

在服务提供者项目下创建一个名为 hello-dubbo-nacos-provider-api 的模块, 该项目模块只负责 定义接口

POM

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

4.0.0

com.antoniopeng

hello-dubbo-nacos-provider

hello-dubbo-nacos-provider-api

jar

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

4.0.0

com.antoniopeng

hello-dubbo-nacos-provider

hello-dubbo-nacos-provider-api

jar

定义一个接口

public interface EchoService {

String echo(String string);

}

服务提供者接口实现模块

创建名为 hello-dubbo-nacos-provider-service 服务提供者接口的实现模块,用于实现在接口模块中定义的接口。

引入依赖

在 pom.xml 中主要添加以下依赖

org.apache.dubbo

dubbo-spring-boot-starter

org.apache.dubbo

dubbo

org.apache.dubbo

dubbo-serialization-kryo

com.alibaba

dubbo-registry-nacoshttp://

com.alibaba.nacos

nacos-client

com.alibaba.spring

spring-context-support

com.antoniopeng

hello-dubbo-nacos-provider-api

${project.parent.version}

相关配置

在 application.yml 中加入相关配置

spring:

application:

name: dubbo-nacos-provider

main:

allow-bean-definition-overriding: true

dubbo:

scan:

# 接口扫描路径

base-packages: com.antoniopeng.hello.dubbo.nacos.provider.service

protocol:

name: dubbo

# -1 代表自动分配端口

port: -1

# 配置高速序列化规则

serialization: kryo

registry:

# 服务注册地址,也就是 Nacos 的服务器地址

address: nacos://192.168.127.132:8848

provider:

# 配置负载均衡策略(轮询)

loadbalance: roundrobin

附:Duubo 负载均衡策略

random:随机

roundrobin:轮询

leastactive:最少活跃数

consistenthash:一致性 Hash

实现接口

通过 org.apache.dubbo.config.annotation 包下的 @Service 注解将接口暴露出去

import com.antoniopeng.hello.dubbo.nacos.provider.api.EchoService;

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

@Service(version = "1.0.0")

public class EchoServiceImpl implements EchoService {

@Override

public String echo(String string) {

return "Echo Hello Dubbo " + string;

}

}

注意:@Service 注解要注明 version 属性

验证是否成功

启动项目,通过浏览器访问Nacos Server 网址 http://192.168.127.132:8848/nacos ,会发现有一个服务已经注册在服务列表中。

服务消费者

创建一个名为 hello-dubbo-nacos-consuTaPawbLlZpmer 的服务消费者项目

引入依赖

同样在 pom.xml中添加以下主要依赖

org.apache.dubbo

dubbo-spring-boot-starter

org.apache.dubbo

dubbo-serialization-kryo

org.apache.dubbo

dubbo-spring-boot-actuator

org.apache.dubbo

dubbo

com.alibaba

dubbo-registry-nacos

com.alibaba.nacos

nacos-client

com.alibaba.spring

spring-context-support

com.antoniopeng

hello-dubbo-nacos-provider-api

${project.parent.version}

相关配置

在 application.yml 中添加以下配置

spring:

application:

name: dubbo-nacos-consumer

main:

allow-bean-definition-overriding: true

dubbo:

scan:

# 配置 Controller 扫描路径

base-packages: com.antoniopeng.dubbo.nacos.consumer.controller

protocol:

name: dubbo

port: -1

registry:

address: nacos://192.168.127.132:8848

server:

port: 8080

# 服务监控检查

endpoints:

dubbo:

enabled: true

management:

health:

dubbo:

status:

defaults: memory

extras: threadpool

endpoints:

web:

exposure:

include: "*"

Controller

通过 org.apache.dubbo.config.annotation 包下的 @Reference 注解以 RPC 通信的方式调用服务,而对外提供以 HTTP 通信的方式的 Restful API

import com.antoniopeng.dubbo.nacos.provider.api.EchoService;

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

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class EchoController {

@Reference(version = "1.0.0")

private EchoService echoService;

@GetMapping(value = "/echo/{string}")

public String echo(@PathVariable String string) {

return echoService.echo(string);

}

}

验证是否成功

通过浏览器访问 Nacos Server 网址 http:192.168.127.132:8848/nacos ,会发现又多了一个服务在服务列表中。

然后再访问服务消费者对外提供的 RESTful API http://localhost:8080/echo/hi,浏览器会响应以下内容:

Echo Hello Dubbo hi

到此,实现了 Nacos 与 Dubbo 的融合。


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

上一篇:Spring Cloud Zipkin服务端追踪服务
下一篇:IDEA 2020.1打开时闪退的问题及解决方法(完美解决方法)
相关文章

 发表评论

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