Spring Cloud Alibaba和Dubbo融合實現
服務提供者
建立一個名為 hello-dubbo-nacos-provider 的服務提供者專案
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>
該專案下有兩個子模組,分別是 hello-dubbo-nacos-provider-api 和 hello-dubbo-nacos-provider-service,前者用於定義介面,後者用於實現介面。
服務提供者介面模組
在服務提供者專案下建立一個名為 hello-dubbo-nacos-provider-api 的模組, 該專案模組只負責 定義介面
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); }
服務提供者介面實現模組
建立名為 hello-dubbo-nacos-provider-service 服務提供者介面的實現模組,用於實現在介面模組中定義的介面。
引入依賴
在 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> <!-- 依賴介面模組,用於實現介面 --> <dependency> <groupId>com.antoniopeng</groupId> <artifactId>hello-dubbo-nacos-provider-api</artifactId> <version>${project.parent.version}</version> </dependency>
相關配置
在 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-consumer 的服務消費者專案
引入依賴
同樣在 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> <!-- 依賴服務提供者介面模組,用於呼叫介面 --> <dependency> <groupId>com.antoniopeng</groupId> <artifactId>hello-dubbo-nacos-provider-api</artifactId> <version>${project.parent.version}</version> </dependency>
相關配置
在 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 的融合。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。