Spring Cloud Alibaba和Dubbo融合實(shí)現(xiàn)
服務(wù)提供者
創(chuàng)建一個(gè)名為 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)目下有兩個(gè)子模塊,分別是 hello-dubbo-nacos-provider-api 和 hello-dubbo-nacos-provider-service,前者用于定義接口,后者用于實(shí)現(xiàn)接口。
服務(wù)提供者接口模塊
在服務(wù)提供者項(xiàng)目下創(chuàng)建一個(gè)名為 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>
定義一個(gè)接口
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: truedubbo: scan: # 接口掃描路徑 base-packages: com.antoniopeng.hello.dubbo.nacos.provider.service protocol: name: dubbo # -1 代表自動(dòng)分配端口 port: -1 # 配置高速序列化規(guī)則 serialization: kryo registry: # 服務(wù)注冊(cè)地址,也就是 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)接口
通過(guò) 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)證是否成功
啟動(dòng)項(xiàng)目,通過(guò)瀏覽器訪問(wèn)Nacos Server 網(wǎng)址 http://192.168.127.132:8848/nacos ,會(huì)發(fā)現(xiàn)有一個(gè)服務(wù)已經(jīng)注冊(cè)在服務(wù)列表中。
服務(wù)消費(fèi)者
創(chuàng)建一個(gè)名為 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: 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
通過(guò) org.apache.dubbo.config.annotation 包下的 @Reference 注解以 RPC 通信的方式調(diào)用服務(wù),而對(duì)外提供以 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); }}
驗(yàn)證是否成功
通過(guò)瀏覽器訪問(wèn) Nacos Server 網(wǎng)址 http:192.168.127.132:8848/nacos ,會(huì)發(fā)現(xiàn)又多了一個(gè)服務(wù)在服務(wù)列表中。
然后再訪問(wèn)服務(wù)消費(fèi)者對(duì)外提供的 RESTful API http://localhost:8080/echo/hi,瀏覽器會(huì)響應(yīng)以下內(nèi)容:
Echo Hello Dubbo hi
到此,實(shí)現(xiàn)了 Nacos 與 Dubbo 的融合。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. JSP數(shù)據(jù)交互實(shí)現(xiàn)過(guò)程解析2. 解決啟動(dòng)django,瀏覽器顯示“服務(wù)器拒絕訪問(wèn)”的問(wèn)題3. Nginx+php配置文件及原理解析4. vue使用webSocket更新實(shí)時(shí)天氣的方法5. Yii2.0引入CSS,JS文件方法6. Opencv+Python識(shí)別PCB板圖片的步驟7. ASP.NET MVC獲取多級(jí)類別組合下的產(chǎn)品8. python使用selenium爬蟲(chóng)知乎的方法示例9. 討論CSS中的各類居中方式10. 如何使用CSS3畫出一個(gè)叮當(dāng)貓
