Quick Start
Dubbo!用更優(yōu)雅的方式來實(shí)現(xiàn)RPC調(diào)用吧 - 掘金
dubbo+zookeeper demo
項(xiàng)目結(jié)構(gòu):
RpcService
僅僅是提供服務(wù)的接口:
public interface HelloService {
String sayHello(String name);
}
DubboServer
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 https://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.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>DubboServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DubboServer</name>
<description>DubboServer</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<!-- Dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.5</version>
</dependency>
<!-- Spring Context Extras -->
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.12</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>RpcService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<!-- 這是個編譯java代碼的 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
DubboServerApplication:
package com.example.dubboserver;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo(scanBasePackages = {"com.example.dubboserver"})
public class DubboServerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboServerApplication.class, args);
}
}
HelloServiceImpl實(shí)現(xiàn)dubbo服務(wù):
package com.example.dubboserver;
import com.alibaba.dubbo.config.annotation.Service;
import com.example.rpcservice.HelloService;
import org.springframework.stereotype.Component;
@Component
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "hello " + name + "!";
}
}
配置文件application.yaml:
dubbo:
application:
name: example-provider
registry:
address: zookeeper://43.143.229.22:2181
protocol:
name: dubbo
port: 20880
zookeeper是我的一臺云服務(wù)器,zookeeper需要先部署好。
DubboClient
pom文件與Server相似:
<?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 https://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.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>DubboClient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DubboClient</name>
<description>DubboClient</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>RpcService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<!-- Dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.5</version>
</dependency>
<!-- Spring Context Extras -->
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
DubboClientApplication:
package com.example.dubboclient;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo(scanBasePackages = {"com.example.dubboclient"})
public class DubboClientApplication {
public static void main(String[] args) {
SpringApplication.run(DubboClientApplication.class, args);
}
}
RpcService:
package com.example.dubboclient;
import com.alibaba.dubbo.config.annotation.Reference;
import com.example.rpcservice.HelloService;
import org.springframework.stereotype.Service;
@Service
public class RpcService {
@Reference
private HelloService helloService;
public String getHelloServiceResponse(String name) {
return helloService.sayHello(name);
}
}
通過Reference注解標(biāo)識這是一個dubbo服務(wù)接口。
TriggerController:(通過get方法觸發(fā)dubbo調(diào)用,debug用)
package com.example.dubboclient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/demo/dubbo")
public class TriggerController {
@Autowired
private RpcService rpcService;
@GetMapping("/{name}")
public String getTime(@PathVariable("name") String name) {
return rpcService.getHelloServiceResponse(name);
}
}
配置文件:
server:
port: 8081
dubbo:
application:
name: example-consumer
registry:
address: zookeeper://43.143.229.22:2181
client: curator
protocol:
name: dubbo
server: false
consumer:
timeout: 3000
dubbo使用nacos作為注冊中心
dubbo也可以用nacos作為注冊中心。使用docker一鍵啟動nacos2: docker run --name nacos-quick -e MODE=standalone -p 8848:8848 -p 9848:9848 -p 9849:9849 -d nacos/nacos-server:2.0.2
,如果是部署在云服務(wù)器上的話記得防火墻暴露接口。
client和server的代碼與上面zookeeper的一致,但是依賴和配置換了,依賴采用alibaba-spring-cloud來提供。這里我為了方便沒有創(chuàng)建父項(xiàng)目寫dependencyManagement,而是直接在子項(xiàng)目寫來管理包版本,這種做法不是很規(guī)范。
Server
pom.xml:
<?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 https://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.6.3</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>DubboServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DubboServer</name>
<description>DubboServer</description>
<properties>
<spring-cloud-clibaba.version>2021.0.1.0</spring-cloud-clibaba.version>
<spring-cloud.version>2021.0.1</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- spring cloud 版本控制 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring cloud alibaba 版本控制 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-clibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- nacos 配置 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>RpcService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<!-- 這是個編譯java代碼的 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
application.yaml:
spring:
main:
# spring boot 2.6.3默認(rèn)不允許循環(huán)依賴, dubbo的這個版本存在循環(huán)依賴
allow-circular-references: true
application:
name: demo-server
cloud:
nacos:
config:
enable: false
discovery:
server-addr: 123.56.98.228:8848
namespace: public
dubbo:
consumer:
retries: 5
protocol:
port: -1
name: dubbo
scan:
# 掃描實(shí)現(xiàn)類的包路徑(可配置多個或數(shù)組)
base-packages: com.example.dubboserver # 更改為自己dubbo實(shí)現(xiàn)類的路徑
registry:
address: nacos://${spring.cloud.nacos.discovery.server-addr}?namespace=${spring.cloud.nacos.discovery.namespace}
cloud:
# 默認(rèn)*訂閱所有, 所以在此處寫一個不存在的提供者
# (可訂閱多個用,隔開或者用數(shù)組的配置方式 -: name)
subscribed-services: "-"
client
pom.xml:
<?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 https://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.6.3</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>DubboClient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DubboClient</name>
<description>DubboClient</description>
<properties>
<spring-cloud-clibaba.version>2021.0.1.0</spring-cloud-clibaba.version>
<spring-cloud.version>2021.0.1</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- spring cloud 版本控制 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring cloud alibaba 版本控制 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-clibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>RpcService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- nacos 配置 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yaml:
spring:
main:
# spring boot 2.6.3默認(rèn)不允許循環(huán)依賴, dubbo的這個版本存在循環(huán)依賴
allow-circular-references: true
application:
name: demo-client
cloud:
nacos:
config:
enable: false
discovery:
server-addr: 123.56.98.228:8848
namespace: public
dubbo:
consumer:
retries: 5
protocol:
port: -1
name: dubbo # 協(xié)議名稱
測試
先上nacos看看服務(wù) ip:8848/nacos:
其中有一個是dubbo自己的服務(wù),剩下三個一個是client應(yīng)用,一個是server應(yīng)用,一個是暴露的RpcService服務(wù)。
瀏覽器輸入:http://localhost:8080/demo/dubbo/dsasdfaasdf
響應(yīng):
沒啥問題。
dubbo泛化調(diào)用
dubbo泛化調(diào)用即在調(diào)用方不引入服務(wù)方接口包的情況下直接調(diào)用服務(wù)接口。
適用場景:
比如你做了一個網(wǎng)關(guān)項(xiàng)目,網(wǎng)關(guān)需要調(diào)用后端的多個服務(wù)的多個接口,這些接口假如都是dubbo,網(wǎng)關(guān)需要引入所有服務(wù)的接口包才能調(diào)用,這顯然不太合理,網(wǎng)關(guān)的代碼應(yīng)該和后端服務(wù)解耦。
那如何實(shí)現(xiàn)泛化調(diào)用呢?
- 服務(wù)方不需要更改配置。
- 調(diào)用方更改rpc調(diào)用的代碼。
不使用泛化調(diào)用的代碼:
@Service
public class RpcService {
@Reference
private HelloService helloService; // 普通調(diào)用
public String getHelloServiceResponse(String name) {
return helloService.sayHello(name); // 普通調(diào)用
}
}
改為泛化調(diào)用:
@Service
public class RpcService {
@DubboReference(interfaceName = "com.example.rpcservice.HelloService", generic = true)
private GenericService genericService;
public String getHelloServiceResponse(String name) {
Object res = genericService.$invoke("sayHello" , new String[]{"java.lang.String"},
new Object[]{name});
return (String) res;
}
}
可以看到將原本的具體的Service接口改為了GenericService, @DubboReference中寫明了調(diào)用的接口全限定名稱,并且generic打開為true。文章來源:http://www.zghlxwxcb.cn/news/detail-850615.html
至此完成泛化調(diào)用改動,還是挺簡單的。文章來源地址http://www.zghlxwxcb.cn/news/detail-850615.html
到了這里,關(guān)于Alibaba spring cloud Dubbo使用(基于Zookeeper或者基于Nacos+泛化調(diào)用完整代碼一鍵啟動)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!