欧美free性护士vide0shd,老熟女,一区二区三区,久久久久夜夜夜精品国产,久久久久久综合网天天,欧美成人护士h版

首頁綜合 正文
目錄

柚子快報(bào)邀請(qǐng)碼778899分享:Dubbo基本使用

柚子快報(bào)邀請(qǐng)碼778899分享:Dubbo基本使用

http://yzkb.51969.com/

Dubbo基本使用

1.項(xiàng)目介紹2.開發(fā)步驟2.1 啟動(dòng)注冊(cè)中心2.2 初始化項(xiàng)目2.3 添加 Maven 依賴2.3.1 父pom.xml2.3.1 consumer模塊和provider模塊pom.xml

2.4 定義服務(wù)接口2.5 定義服務(wù)端的實(shí)現(xiàn)2.6 配置服務(wù)端 Yaml 配置文件2.7 配置消費(fèi)端 Yaml 配置文件2.8 基于 Spring 配置服務(wù)端啟動(dòng)類2.9 基于 Spring 配置消費(fèi)端啟動(dòng)類2.10 配置消費(fèi)端請(qǐng)求任務(wù)2.11 啟動(dòng)應(yīng)用

1.項(xiàng)目介紹

在本任務(wù)中,將分為 3 個(gè)子模塊進(jìn)行獨(dú)立開發(fā),模擬生產(chǎn)環(huán)境下的部署架構(gòu)。

// apache/dubbo-samples/1-basic/dubbo-samples-spring-boot

├── dubbo-samples-spring-boot-interface // 共享 API 模塊

├── dubbo-samples-spring-boot-consumer // 消費(fèi)端模塊

└── dubbo-samples-spring-boot-provider // 服務(wù)端模塊

如上所示,共有 3 個(gè)模塊,其中 interface 模塊被 consumer 和 provider 兩個(gè)模塊共同依賴,存儲(chǔ) RPC 通信使用的 API 接口。

. // apache/dubbo-samples/1-basic/dubbo-samples-spring-boot

├── dubbo-samples-spring-boot-interface // 共享 API 模塊

│ ├── pom.xml

│ └── src

│ └── main

│ └── java

│ └── org

│ └── apache

│ └── dubbo

│ └── springboot

│ └── demo

│ └── DemoService.java // API 接口

├── dubbo-samples-spring-boot-consumer // 消費(fèi)端模塊

│ ├── pom.xml

│ └── src

│ ├── main

│ │ ├── java

│ │ │ └── org

│ │ │ └── apache

│ │ │ └── dubbo

│ │ │ └── springboot

│ │ │ └── demo

│ │ │ └── consumer

│ │ │ ├── ConsumerApplication.java // 消費(fèi)端啟動(dòng)類

│ │ │ └── Task.java // 消費(fèi)端模擬調(diào)用任務(wù)

│ │ └── resources

│ │ └── application.yml // Spring Boot 配置文件

├── dubbo-samples-spring-boot-provider // 服務(wù)端模塊

│ ├── pom.xml

│ └── src

│ └── main

│ ├── java

│ │ └── org

│ │ └── apache

│ │ └── dubbo

│ │ └── springboot

│ │ └── demo

│ │ └── provider

│ │ ├── DemoServiceImpl.java // 服務(wù)端實(shí)現(xiàn)類

│ │ └── ProviderApplication.java // 服務(wù)端啟動(dòng)類

│ └── resources

│ └── application.yml // Spring Boot 配置文件

└── pom.xml

如上為本教程接下來會(huì)使用到的項(xiàng)目的文件結(jié)構(gòu)。

2.開發(fā)步驟

2.1 啟動(dòng)注冊(cè)中心

對(duì)于一個(gè)微服務(wù)化的應(yīng)用來說,注冊(cè)中心是不可或缺的一個(gè)組件。只有通過注冊(cè)中心,消費(fèi)端才可以成功發(fā)現(xiàn)服務(wù)端的地址信息,進(jìn)而進(jìn)行調(diào)用。 zookeeper的使用不放在本章中說明。

2.2 初始化項(xiàng)目

使用idea創(chuàng)建如下的一個(gè)項(xiàng)目結(jié)構(gòu)

2.3 添加 Maven 依賴

在初始化完項(xiàng)目以后,我們需要先添加 Dubbo 相關(guān)的 maven 依賴。在最外層的pom及父項(xiàng)目的pom.xml增加配置依賴信息。

2.3.1 父pom.xml

3.2.0-beta.4

2.7.8

17

17

UTF-8

org.springframework.boot

spring-boot-dependencies

${spring-boot.version}

pom

import

org.apache.dubbo

dubbo-bom

${dubbo.version}

pom

import

org.apache.dubbo

dubbo-dependencies-zookeeper-curator5

${dubbo.version}

pom

org.springframework.boot

spring-boot-maven-plugin

${spring-boot.version}

2.3.1 consumer模塊和provider模塊pom.xml

編輯 /dubbo-spring-boot-consumer/pom.xml 和 /dubbo-spring-boot-provider/pom.xml 這兩文件,都添加下列配置。

org.apache.dubbo

dubbo-samples-spring-boot-interface

${project.parent.version}

org.apache.dubbo

dubbo-spring-boot-starter

org.apache.dubbo

dubbo-dependencies-zookeeper-curator5

pom

slf4j-reload4j

org.slf4j

org.springframework.boot

spring-boot-starter

在這份配置中,定義了 dubbo 和 zookeeper(以及對(duì)應(yīng)的連接器 curator)的依賴。

2.4 定義服務(wù)接口

在 dubbo-spring-boot-demo-interface 模塊下建立 DemoService 接口,定義如下:

package org.apache.dubbo.springboot.demo;

public interface DemoService {

String sayHello(String name);

}

在 DemoService 中,定義了 sayHello 這個(gè)方法。后續(xù)服務(wù)端發(fā)布的服務(wù),消費(fèi)端訂閱的服務(wù)都是圍繞著 DemoService 接口展開的。

2.5 定義服務(wù)端的實(shí)現(xiàn)

定義了服務(wù)接口之后,可以在服務(wù)端這一側(cè)定義對(duì)應(yīng)的實(shí)現(xiàn),這部分的實(shí)現(xiàn)相對(duì)于消費(fèi)端來說是遠(yuǎn)端的實(shí)現(xiàn),本地沒有相關(guān)的信息。

在dubbo-spring-boot-demo-provider 模塊的下建立 DemoServiceImpl 類,定義如下:

package org.apache.dubbo.springboot.demo.provider;

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

import org.apache.dubbo.springboot.demo.DemoService;

@DubboService

public class DemoServiceImpl implements DemoService {

@Override

public String sayHello(String name) {

return "Hello " + name;

}

}

在 DemoServiceImpl 中,實(shí)現(xiàn)了 DemoService 接口,對(duì)于 sayHello 方法返回 Hello name。

注:在DemoServiceImpl 類中添加了 @DubboService 注解,通過這個(gè)配置可以基于 Spring Boot 去發(fā)布 Dubbo 服務(wù)。

2.6 配置服務(wù)端 Yaml 配置文件

在 dubbo-spring-boot-demo-provider 模塊的 resources 資源文件夾下建立 application.yml 文件,定義如下:

dubbo:

application:

name: dubbo-springboot-demo-provider

protocol:

name: dubbo

port: -1

registry:

address: zookeeper://${zookeeper.address:127.0.0.1}:2181

在這個(gè)配置文件中,定義了 Dubbo 的應(yīng)用名、Dubbo 協(xié)議信息、Dubbo 使用的注冊(cè)中心地址。

2.7 配置消費(fèi)端 Yaml 配置文件

在 dubbo-spring-boot-demo-consumer 模塊的 resources 資源文件夾下建立 application.yml 文件,定義如下:

dubbo:

application:

name: dubbo-springboot-demo-consumer

protocol:

name: dubbo

port: -1

registry:

address: zookeeper://${zookeeper.address:127.0.0.1}:2181

在這個(gè)配置文件中,定義了 Dubbo 的應(yīng)用名、Dubbo 協(xié)議信息、Dubbo 使用的注冊(cè)中心地址。

2.8 基于 Spring 配置服務(wù)端啟動(dòng)類

在 dubbo-spring-boot-demo-provider 模塊下建立 Application 類,定義如下:

package org.apache.dubbo.springboot.demo.provider;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

@EnableDubbo

public class ProviderApplication {

public static void main(String[] args) {

SpringApplication.run(ProviderApplication.class, args);

}

}

在這個(gè)啟動(dòng)類中,配置了一個(gè) ProviderApplication 去讀取我們前面第 6 步中定義的 application.yml 配置文件并啟動(dòng)應(yīng)用。

2.9 基于 Spring 配置消費(fèi)端啟動(dòng)類

在 dubbo-spring-boot-demo-consumer 模塊下建立 Application 類,定義如下:

package org.apache.dubbo.springboot.demo.consumer;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

@EnableDubbo

public class ConsumerApplication {

public static void main(String[] args) {

SpringApplication.run(ConsumerApplication.class, args);

}

}

在這個(gè)啟動(dòng)類中,配置了一個(gè) ConsumerApplication 去讀取我們前面第 7 步中定義的 application.yml 配置文件并啟動(dòng)應(yīng)用。

2.10 配置消費(fèi)端請(qǐng)求任務(wù)

在 dubbo-spring-boot-demo-consumer 模塊下建立 Task 類,定義如下:

package org.apache.dubbo.springboot.demo.consumer;

import java.util.Date;

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

import org.apache.dubbo.springboot.demo.DemoService;

import org.springframework.boot.CommandLineRunner;

import org.springframework.stereotype.Component;

@Component

public class Task implements CommandLineRunner {

@DubboReference

private DemoService demoService;

@Override

public void run(String... args) throws Exception {

String result = demoService.sayHello("world");

System.out.println("Receive result ======> " + result);

new Thread(()-> {

while (true) {

try {

Thread.sleep(1000);

System.out.println(new Date() + " Receive result ======> " + demoService.sayHello("world"));

} catch (InterruptedException e) {

e.printStackTrace();

Thread.currentThread().interrupt();

}

}

}).start();

}

}

在 Task 類中,通過@DubboReference 從 Dubbo 獲取了一個(gè) RPC 訂閱,這個(gè) demoService 可以像本地調(diào)用一樣直接調(diào)用。在 run方法中創(chuàng)建了一個(gè)線程進(jìn)行調(diào)用。

2.11 啟動(dòng)應(yīng)用

首先是啟動(dòng)provider模塊的Application ,等待一會(huì)出現(xiàn)如下日志(Current Spring Boot Application is await)即代表服務(wù)提供者啟動(dòng)完畢,標(biāo)志著該服務(wù)提供者可以對(duì)外提供服務(wù)了。

[Dubbo] Current Spring Boot Application is await...

然后是啟動(dòng)consumer模塊的Application ,等待一會(huì)出現(xiàn)如下日志(Hello world )即代表服務(wù)消費(fèi)端啟動(dòng)完畢并調(diào)用到服務(wù)端成功獲取結(jié)果。

Receive result ======> Hello world

柚子快報(bào)邀請(qǐng)碼778899分享:Dubbo基本使用

http://yzkb.51969.com/

推薦鏈接

評(píng)論可見,查看隱藏內(nèi)容

本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。

轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。

本文鏈接:http://gantiao.com.cn/post/19090751.html

發(fā)布評(píng)論

您暫未設(shè)置收款碼

請(qǐng)?jiān)谥黝}配置——文章設(shè)置里上傳

掃描二維碼手機(jī)訪問

文章目錄