Spring Cloud Alibaba和Dubbo融合實(shí)現(xiàn)
服務(wù)提供者
創(chuàng)建一個名為 hello-dubbo-nacos-provider 的服務(wù)提供者項(xiàng)目
POM
<?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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.antoniopeng</groupId> <artifactId>hello-dubbo-nacos-provider</artifactId> <packaging>pom</packaging> <modules> <module>hello-dubbo-nacos-provider-api</module> <module>hello-dubbo-nacos-provider-service</module> </modules> </project>
該項(xiàng)目下有兩個子模塊,分別是 hello-dubbo-nacos-provider-api 和 hello-dubbo-nacos-provider-service,前者用于定義接口,后者用于實(shí)現(xiàn)接口。
服務(wù)提供者接口模塊
在服務(wù)提供者項(xiàng)目下創(chuàng)建一個名為 hello-dubbo-nacos-provider-api 的模塊, 該項(xiàng)目模塊只負(fù)責(zé) 定義接口
POM
<?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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.antoniopeng</groupId> <artifactId>hello-dubbo-nacos-provider</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>hello-dubbo-nacos-provider-api</artifactId> <packaging>jar</packaging> </project>
定義一個接口
public interface EchoService { String echo(String string); }
服務(wù)提供者接口實(shí)現(xiàn)模塊
創(chuàng)建名為 hello-dubbo-nacos-provider-service 服務(wù)提供者接口的實(shí)現(xiàn)模塊,用于實(shí)現(xiàn)在接口模塊中定義的接口。
引入依賴
在 pom.xml 中主要添加以下依賴
<!-- Nacos And Dubbo--> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-serialization-kryo</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo-registry-nacos</artifactId> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> </dependency> <dependency> <groupId>com.alibaba.spring</groupId> <artifactId>spring-context-support</artifactId> </dependency> <!-- 依賴接口模塊,用于實(shí)現(xiàn)接口 --> <dependency> <groupId>com.antoniopeng</groupId> <artifactId>hello-dubbo-nacos-provider-api</artifactId> <version>${project.parent.version}</version> </dependency>
相關(guān)配置
在 application.yml 中加入相關(guān)配置
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 # 配置高速序列化規(guī)則 serialization: kryo registry: # 服務(wù)注冊地址,也就是 Nacos 的服務(wù)器地址 address: nacos://192.168.127.132:8848 provider: # 配置負(fù)載均衡策略(輪詢) loadbalance: roundrobin
附:Duubo 負(fù)載均衡策略
- random:隨機(jī)
- roundrobin:輪詢
- leastactive:最少活躍數(shù)
- consistenthash:一致性 Hash
實(shí)現(xiàn)接口
通過 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 屬性
驗(yàn)證是否成功
啟動項(xiàng)目,通過瀏覽器訪問Nacos Server 網(wǎng)址 http://192.168.127.132:8848/nacos ,會發(fā)現(xiàn)有一個服務(wù)已經(jīng)注冊在服務(wù)列表中。
服務(wù)消費(fèi)者
創(chuàng)建一個名為 hello-dubbo-nacos-consumer 的服務(wù)消費(fèi)者項(xiàng)目
引入依賴
同樣在 pom.xml中添加以下主要依賴
<!-- Nacos And Dubbo --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-serialization-kryo</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-actuator</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo-registry-nacos</artifactId> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> </dependency> <dependency> <groupId>com.alibaba.spring</groupId> <artifactId>spring-context-support</artifactId> </dependency> <!-- 依賴服務(wù)提供者接口模塊,用于調(diào)用接口 --> <dependency> <groupId>com.antoniopeng</groupId> <artifactId>hello-dubbo-nacos-provider-api</artifactId> <version>${project.parent.version}</version> </dependency>
相關(guān)配置
在 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 # 服務(wù)監(jiān)控檢查 endpoints: dubbo: enabled: true management: health: dubbo: status: defaults: memory extras: threadpool endpoints: web: exposure: include: "*"
Controller
通過 org.apache.dubbo.config.annotation 包下的 @Reference 注解以 RPC 通信的方式調(diào)用服務(wù),而對外提供以 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); } }
驗(yàn)證是否成功
通過瀏覽器訪問 Nacos Server 網(wǎng)址 http:192.168.127.132:8848/nacos ,會發(fā)現(xiàn)又多了一個服務(wù)在服務(wù)列表中。
然后再訪問服務(wù)消費(fèi)者對外提供的 RESTful API http://localhost:8080/echo/hi,瀏覽器會響應(yīng)以下內(nèi)容:
Echo Hello Dubbo hi
到此,實(shí)現(xiàn)了 Nacos 與 Dubbo 的融合。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot讀取properties或者application.yml配置文件中的數(shù)據(jù)
這篇文章主要介紹了SpringBoot讀取properties或者application.yml配置文件中的數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Spring?Boot讀取配置文件內(nèi)容的3種方式(@Value、Environment和@ConfigurationP
工作中經(jīng)常會有一些參數(shù)需要配置,同時在代碼里面需要用到,所有就需要配置類讀取,然后在使用的時候注入該類進(jìn)行獲取相關(guān)參數(shù),下面這篇文章主要給大家介紹了關(guān)于Spring?Boot讀取配置文件內(nèi)容的3種方式,需要的朋友可以參考下2023-01-01利用spring-data-redis實(shí)現(xiàn)incr自增的操作
這篇文章主要介紹了利用spring-data-redis實(shí)現(xiàn)incr自增的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11