国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Spring Cloud Alibaba和Dubbo融合實現(xiàn)

瀏覽:2日期:2023-09-11 09:01:34

服務(wù)提供者

創(chuàng)建一個名為 hello-dubbo-nacos-provider 的服務(wù)提供者項目

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,前者用于定義接口,后者用于實現(xiàn)接口。

服務(wù)提供者接口模塊

在服務(wù)提供者項目下創(chuàng)建一個名為 hello-dubbo-nacos-provider-api 的模塊, 該項目模塊只負(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ù)提供者接口實現(xiàn)模塊

創(chuàng)建名為 hello-dubbo-nacos-provider-service 服務(wù)提供者接口的實現(xiàn)模塊,用于實現(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><!-- 依賴接口模塊,用于實現(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: truedubbo: 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

實現(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 屬性

驗證是否成功

啟動項目,通過瀏覽器訪問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)者項目

引入依賴

同樣在 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: truedubbo: scan: # 配置 Controller 掃描路徑 base-packages: com.antoniopeng.dubbo.nacos.consumer.controller protocol: name: dubbo port: -1 registry: address: nacos://192.168.127.132:8848server: port: 8080# 服務(wù)監(jiān)控檢查endpoints: dubbo: enabled: truemanagement: 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;@RestControllerpublic 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 網(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

到此,實現(xiàn)了 Nacos 與 Dubbo 的融合。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 万源市| 务川| 平昌县| 门源| 辽宁省| 四子王旗| 常山县| 福清市| 台湾省| 衡山县| 蒙城县| 菏泽市| 富蕴县| 元谋县| 弥勒县| 疏勒县| 承德县| 吉林省| 黄山市| 五华县| 阜南县| 海原县| 赤壁市| 额尔古纳市| 忻州市| 兴海县| 朝阳县| 隆子县| 桓仁| 锦屏县| 冕宁县| 鹤山市| 民丰县| 皮山县| 方城县| 天祝| 红桥区| 沂水县| 晋州市| 兴文县| 仁布县|