Apache Dubbo學習筆記-使用Dubbo發布、調用服務

Apache Dubbo經常作為一個RPC框架來使用,這篇文章主要介紹使用Dubbo配合注冊中心來發布和調用服務。


Apache Dubbo和Spring Boot、JDK的版本對應關系。

Dubbo 分支最新版本JDKSpring Boot組件版本詳細說明
3.3.x (當前文檔)3.3.08, 17, 212.x、3.x詳情- 版本變更記錄 - 生產可用(推薦,長期維護)! 最新Triple協議升級,內置Metrics、Tracing、GraalVM支持等
3.2.x3.2.108, 172.x、3.x詳情- 版本變更記錄 - 生產可用(長期維護)!
3.1.x3.1.118, 172.x、3.x詳情- 版本變更記錄 - 僅修復安全漏洞!
3.0.x3.0.1582.x詳情- 版本變更記錄 - 停止維護!
2.7.x2.7.2382.x詳情- 了解如何升級到Dubbo3 - 停止維護!

博主使用的Spring Boot版本是2.3.4.RELEASE、jdk版本是1.8.0_461,所以采用dubbo的2.7.23版本(因為3.0.x版本不兼容)。

<dependencyManagement><dependencies><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-bom</artifactId><version>2.7.23</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement>

一、創建服務接口

文章只是為了簡單介紹一下使用Dubbo來發布和調用服務(RPC接口),所以把所有接口都存放在一個dubbo-api的項目上,服務提供者和服務消費者只需要依賴這個項目即可。

1、創建項目

在Intellij IDEA中創建一個Maven項目,項目名為dubbo-api

2、添加依賴

添加lombok的依賴和Maven的jar包插件。

<?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><version>20250906</version><groupId>cn.edu.sgu.www</groupId><artifactId>dubbo-api</artifactId><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.22</version><optional>true</optional></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>3.2.0</version></plugin></plugins></build>
</project>

3、創建接口

在項目的src/main/java包下創建多級包cn.edu.sgu.www.dubbo.provider

  • 在provider子包下創建entity子包
  • 在provider子包下創建service子包

User.java

在entity包下創建一個類User.java。在User類上使用lombok的@Data注解,實現序列化接口,指定序列化版本號。

package cn.edu.sgu.www.dubbo.provider.entity;import lombok.Data;import java.io.Serializable;/*** @author 沐雨橙風ιε* @version 1.0*/
@Data
public class User implements Serializable {private static final long serialVersionUID = 18L;private String id;/*** 姓名*/private String name;/*** 身高*/private Integer height;/*** 體重*/private Integer weight;
}

UserService.java

在service包下創建user子包,在user包下創建UserService接口,定義兩個方法。

package cn.edu.sgu.www.dubbo.provider.service.user;import cn.edu.sgu.www.dubbo.provider.entity.User;import java.util.List;/*** @author 沐雨橙風ιε* @version 1.0*/
public interface UserService {void save(User user);List<String> selectAll();
}

HelloService.java

在service包下創建hello子包,在hello包下創建HelloService接口,定義一個greet()方法。

package cn.edu.sgu.www.dubbo.provider.service.hello;/*** @author 沐雨橙風ιε* @version 1.0*/
public interface HelloService {void greet();
}

4、項目結構預覽

完成上面三個步驟之后,項目的目錄結構如下。

5、安裝項目到本地倉庫

在當前項目目錄下執行Maven命令mvn install或直接通過Maven插件安裝本項目到本地倉庫。

成功安裝到Maven本地倉庫:

二、創建服務提供者

1、創建項目

在Intellij IDEA中創建一個Spring Boot項目,項目名為dubbo-provider

2、添加依賴

添加Apache Dubbo的Spring Boot啟動器依賴,這個啟動器包含了所有使用Dubbo需要的依賴。

因為還需要通過注冊中心暴露服務信息,所以額外引入Dubbo整合nacos的依賴。

<?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.3.4.RELEASE</version></parent><version>20250907</version><artifactId>dubbo-provider</artifactId><description>Apache Dubbo服務提供者項目</description><properties><java.version>1.8</java.version><dubbo.version>2.7.23</dubbo.version><dubbo-registry.version>3.2.10</dubbo-registry.version></properties><dependencyManagement><dependencies><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-bom</artifactId><version>${dubbo.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><version>20250906</version><groupId>cn.edu.sgu.www</groupId><artifactId>dubbo-api</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-nacos-spring-boot-starter</artifactId><version>${dubbo-registry.version}</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.3.4.RELEASE</version></plugin></plugins></build>
</project>

3、實現接口

在項目src/main/java包下創建cn.edu.sgu.www.dubbo.provider子包,在provider子包下創建service子包。

UserServiceImpl.java

在service包下創建user子包。

在user包下創建UserServiceImpl,實現UserService接口,重寫兩個方法。

package cn.edu.sgu.www.dubbo.provider.service.user;import cn.edu.sgu.www.dubbo.provider.entity.User;import java.util.ArrayList;
import java.util.List;/*** @author 沐雨橙風ιε* @version 1.0*/
public class UserServiceImpl implements UserService {@Overridepublic void save(User user) {System.out.println("save: " + user);}@Overridepublic List<String> selectAll() {List<String> list = new ArrayList<>();list.add("沐雨橙風");list.add("一葉之秋");list.add("蘇沐秋");return list;}}

HelloServiceImpl.java

在service包下創建hello子包。

在user包下創建HelloServiceImpl,實現HelloService接口,重寫兩個方法。

package cn.edu.sgu.www.dubbo.provider.service.hello;/*** @author 沐雨橙風ιε* @version 1.0*/
public class HelloServiceImpl implements HelloService {@Overridepublic void greet() {System.out.println("Hello,沐雨橙風ιε");}}

完成以上步驟的項目目錄結構如下:

  • 啟動類類名刪除了Dubbo前綴
  • 配置類后綴由.properties修改為了.yml

4、發布服務

使用API發布服務

使用Dubbo原生API發布服務:在provider包下創建ApiProvider.java

package cn.edu.sgu.www.dubbo.provider;import cn.edu.sgu.www.dubbo.provider.service.hello.HelloService;
import cn.edu.sgu.www.dubbo.provider.service.hello.HelloServiceImpl;
import cn.edu.sgu.www.dubbo.provider.service.user.UserService;
import cn.edu.sgu.www.dubbo.provider.service.user.UserServiceImpl;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;import java.io.IOException;/*** @author 沐雨橙風ιε* @version 1.0*/
public class ApiProvider {public static void main(String[] args) throws IOException {// 當前應用配置ApplicationConfig application = new ApplicationConfig();application.setName("dubbo-provider");// 連接注冊中心配置RegistryConfig registry = new RegistryConfig();registry.setAddress("nacos://localhost:8848");// 服務提供者協議配置ProtocolConfig protocol = new ProtocolConfig();protocol.setName("dubbo");protocol.setPort(20880);protocol.setThreads(200);// 服務提供者暴露服務配置ServiceConfig<HelloService> helloServiceServiceConfig = new ServiceConfig<>();helloServiceServiceConfig.setApplication(application);helloServiceServiceConfig.setRegistry(registry); // 多個注冊中心可以用setRegistries()helloServiceServiceConfig.setProtocol(protocol); // 多個協議可以用setProtocols()helloServiceServiceConfig.setInterface(HelloService.class);helloServiceServiceConfig.setRef(new HelloServiceImpl());helloServiceServiceConfig.setVersion("20250906");// 暴露及注冊服務helloServiceServiceConfig.export();ServiceConfig<UserService> userServiceServiceConfig = new ServiceConfig<>();userServiceServiceConfig.setApplication(application);userServiceServiceConfig.setRegistry(registry); // 多個注冊中心可以用setRegistries()userServiceServiceConfig.setProtocol(protocol); // 多個協議可以用setProtocols()userServiceServiceConfig.setInterface(UserService.class);userServiceServiceConfig.setRef(new UserServiceImpl());userServiceServiceConfig.setVersion("20250906");// 暴露及注冊服務userServiceServiceConfig.export();System.out.println("dubbo-provider is running.");System.in.read();}}

啟動nacos服務器、運行ApiProvider,訪問nacos的控制臺。

可以看到在默認命名空間中多了兩個配置文件。

同時,Dubbo往nacos中注冊了兩個接口級服務。

使用Spring發布服務

基于XML文件

Dubbo支持通過xml配置文件的方式,結合Spring框架來發布服務。

dubbo-provider.xml

在classpath下創建一個xml配置文件,文件名為dubbo-provider.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://dubbo.apache.org/schema/dubbohttp://dubbo.apache.org/schema/dubbo/dubbo.xsd"><!-- 提供方應用信息,用于計算依賴關系 --><dubbo:application name="dubbo-provider"  /><!-- 用dubbo協議在20880端口暴露服務 --><dubbo:protocol name="dubbo" port="20880" /><!-- 使用nacos注冊中心暴露服務地址 --><dubbo:registry address="nacos://localhost:8848" /><!-- 聲明需要暴露的服務接口 --><dubbo:service interface="cn.edu.sgu.www.dubbo.provider.service.user.UserService" ref="userService" /><dubbo:service interface="cn.edu.sgu.www.dubbo.provider.service.hello.HelloService" ref="helloService" /><!-- 和本地bean一樣實現服務 --><bean id="userService" class="cn.edu.sgu.www.dubbo.provider.service.user.UserServiceImpl" /><bean id="helloService" class="cn.edu.sgu.www.dubbo.provider.service.hello.HelloServiceImpl" />
</beans>

SpringProvider.java

在provider包下創建SpringProvider類,通過xml配置文件創建并啟動Spring容器。

package cn.edu.sgu.www.dubbo.provider;import org.springframework.context.support.ClassPathXmlApplicationContext;import java.io.IOException;/*** @author 沐雨橙風ιε* @version 1.0*/
public class SpringProvider {public static void main(String[] args) throws IOException {ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("dubbo-provider.xml");applicationContext.start();System.out.println("dubbo-provider is running.");System.in.read();}}

停止之前運行的ApiProvider,并刪除其創建的nacos配置文件。

啟動SpringProvider,發現又創建了這兩個配置文件

并且這兩個服務也注冊到了nacos

完成以上步驟的項目目錄結構如下:

基于注解

除了基于xml配置文件的方式以外,Spring還支持使用注解的方式發布(暴露)服務。

dubbo-provider.properties

在classpath下創建一個properties配置文件,文件名為dubbo-provider.properties

dubbo.application.name=dubbo-provider
dubbo.registry.address=nacos://localhost:8848
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

ProviderConfig.java

在HelloServiceImpl和UserServiceImpl兩個類上使用@DubboService注解,將這兩個類定義為Dubbo服務。


在provider包下創建config子包,在config包下創建一個配置類ProviderConfig。

  • 在配置類上使用@EnableDubbo注解,指定掃描service包下的Dubbo服務。
  • 在配置類上使用@PropertySource注解,加載classpath下的配置文件dubbo-provider.properties。
package cn.edu.sgu.www.dubbo.provider.config;import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;/*** @author 沐雨橙風ιε* @version 1.0*/
@Configuration
@EnableDubbo(scanBasePackages = "cn.edu.sgu.www.dubbo.provider.service")
@ComponentScan(basePackages = "cn.edu.sgu.www.dubbo.provider.service")
@PropertySource("classpath:/dubbo-provider.properties")
public class ProviderConfig {}

SpringProvider.java

通過配置文件創建并啟動Spring容器。

package cn.edu.sgu.www.dubbo.provider;import cn.edu.sgu.www.dubbo.provider.config.ProviderConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;import java.io.IOException;/*** @author 沐雨橙風ιε* @version 1.0*/
public class SpringProvider {public static void main(String[] args) throws IOException {AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ProviderConfig.class);applicationContext.start();System.out.println("dubbo-provider is running.");System.in.read();}}

停止運行中的SpringProvider,并刪除其創建的nacos配置文件。

再次運行SpringProvider,發現配置文件被重新創建,并且服務也注冊到了nacos。


完成以上步驟的項目目錄結構如下:

三、創建服務消費者

1、創建項目

在Intellij IDEA中創建一個Spring Boot項目,項目名為dubbo-consumer

2、添加依賴

<?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.3.4.RELEASE</version></parent><version>20250907</version><artifactId>dubbo-consumer</artifactId><description>Apache Dubbo服務消費者項目</description><properties><java.version>1.8</java.version><dubbo.version>2.7.23</dubbo.version><dubbo-registry.version>3.2.10</dubbo-registry.version></properties><dependencyManagement><dependencies><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-bom</artifactId><version>${dubbo.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><version>20250906</version><groupId>cn.edu.sgu.www</groupId><artifactId>dubbo-api</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-nacos-spring-boot-starter</artifactId><version>${dubbo-registry.version}</version></dependency>
<!--        <dependency>-->
<!--            <groupId>org.apache.dubbo</groupId>-->
<!--            <artifactId>dubbo-zookeeper-spring-boot-starter</artifactId>-->
<!--            <version>${dubbo-registry.version}</version>-->
<!--        </dependency>--></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.3.4.RELEASE</version></plugin></plugins></build>
</project>

3、調用服務

使用API調用服務

使用Dubbo原生API調用服務:在consumer包下創建ApiConsumer.java

package cn.edu.sgu.www.dubbo.consumer;import cn.edu.sgu.www.dubbo.provider.service.hello.HelloService;
import cn.edu.sgu.www.dubbo.provider.service.user.UserService;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;import java.util.List;/*** @author 沐雨橙風ιε* @version 1.0*/
public class ApiConsumer {public static void main(String[] args) {// 當前應用配置ApplicationConfig application = new ApplicationConfig();application.setName("dubbo-consumer");// 連接注冊中心配置RegistryConfig registry = new RegistryConfig();registry.setAddress("nacos://localhost:8848");/** 1、引用HelloService遠程服務*/ReferenceConfig<HelloService> helloServiceReferenceConfig = new ReferenceConfig<>();helloServiceReferenceConfig.setApplication(application);helloServiceReferenceConfig.setRegistry(registry); // 多個注冊中心可以用setRegistries()helloServiceReferenceConfig.setInterface(HelloService.class);helloServiceReferenceConfig.setVersion("20250906");// 和本地bean一樣使用HelloServiceHelloService helloService = helloServiceReferenceConfig.get();helloService.greet();/** 2、引用UserService遠程服務*/ReferenceConfig<UserService> userServiceReferenceConfig = new ReferenceConfig<>();userServiceReferenceConfig.setApplication(application);userServiceReferenceConfig.setRegistry(registry); // 多個注冊中心可以用setRegistries()userServiceReferenceConfig.setInterface(UserService.class);userServiceReferenceConfig.setVersion("20250906");// 和本地bean一樣使用UserServiceServiceUserService userService = userServiceReferenceConfig.get();List<String> list = userService.selectAll();System.out.println(list);}}

啟動服務提供者的ApiProvider,然后啟動當前項目的ApiConsumer

可以看到控制臺打印出來了調用UserService接口的selectAll()方法返回的結果。

使用Spring調用服務

基于XML文件
dubbo-consumer.xml

在classpath下創建一個xml配置文件,文件名為dubbo-consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://dubbo.apache.org/schema/dubbohttp://dubbo.apache.org/schema/dubbo/dubbo.xsd"><!-- 消費方應用名,用于計算依賴關系,不是匹配條件,不要與提供方一樣 --><dubbo:application name="dubbo-consumer"  /><!-- 使用nacos注冊中心暴露發現服務地址 --><dubbo:registry address="nacos://localhost:8848" /><!-- 生成遠程服務代理,可以和本地bean一樣使用demoService --><dubbo:reference id="userService" interface="cn.edu.sgu.www.dubbo.provider.service.user.UserService" /><dubbo:reference id="helloService" interface="cn.edu.sgu.www.dubbo.provider.service.hello.HelloService" />
</beans>

SpringConsumer.java

在sonsumer包下創建SpringConsumer類,通過xml配置文件創建并啟動Spring容器。

通過Spring容器獲取Dubbo引用的Dubbo服務的Bean對象,調用Bean對象的方法。

package cn.edu.sgu.www.dubbo.consumer;import cn.edu.sgu.www.dubbo.provider.service.hello.HelloService;
import cn.edu.sgu.www.dubbo.provider.service.user.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.List;/*** @author 沐雨橙風ιε* @version 1.0*/
public class SpringConsumer {public static void main(String[] args) {ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("dubbo-consumer.xml");applicationContext.start();// HelloServiceHelloService helloService = applicationContext.getBean(HelloService.class);helloService.greet();// UserServiceUserService userService = applicationContext.getBean(UserService.class);List<String> list = userService.selectAll();System.out.println(list);}}

基于注解
dubbo-consumer.properties

在classpath下創建一個properties配置文件,文件名為dubbo-provider.properties

dubbo.application.name=dubbo-consumer
dubbo.registry.address=nacos://localhost:8848

HelloController.java

在consumer包下創建controller包,在controller包下創建HelloController類。

通過@DubboReference注解引入Dubbo暴露的接口級服務HelloService

package cn.edu.sgu.www.dubbo.consumer.controller;import cn.edu.sgu.www.dubbo.provider.service.hello.HelloService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author 沐雨橙風ιε* @version 1.0*/
@RestController
@RequestMapping(path = "/hello", produces = "application/json;charset=utf-8")
public class HelloController {@DubboReferenceprivate HelloService helloService;@GetMapping("/greet")public void greet() {helloService.greet();}}

UserController.java

在controller包下創建UserController類。

通過@DubboReference注解引入Dubbo暴露的接口級服務UserService

package cn.edu.sgu.www.dubbo.consumer.controller;import cn.edu.sgu.www.dubbo.provider.service.user.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;/*** @author 沐雨橙風ιε* @version 1.0*/
@RestController
@RequestMapping(path = "/user", produces = "application/json;charset=utf-8")
public class UserController {@DubboReferenceprivate UserService userService;@GetMapping("/selectAll")public List<String> selectAll() {return userService.selectAll();}}

ConsumerConfig.java

在consumer包下創建config子包,在config包下創建一個配置類ConsumerConfig。

  • 在配置類上使用@EnableDubbo注解,指定掃描service包下引用Dubbo服務的類。
  • 在配置類上使用@PropertySource注解,加載classpath下的配置文件dubbo-consumer.properties。
package cn.edu.sgu.www.dubbo.consumer.config;import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;/*** @author 沐雨橙風ιε* @version 1.0*/
@Configuration
@EnableDubbo(scanBasePackages = "cn.edu.sgu.www.dubbo.consumer.controller")
@ComponentScan(basePackages = "cn.edu.sgu.www.dubbo.consumer.controller")
@PropertySource("classpath:/dubbo-consumer.properties")
public class ConsumerConfig {}

SpringConsumer.java

通過配置文件創建并啟動Spring容器,通過容器獲取引用Dubbo服務的控制器Bean對象。

package cn.edu.sgu.www.dubbo.consumer;import cn.edu.sgu.www.dubbo.consumer.config.ConsumerConfig;
import cn.edu.sgu.www.dubbo.consumer.controller.HelloController;
import cn.edu.sgu.www.dubbo.consumer.controller.UserController;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;import java.util.List;/*** @author 沐雨橙風ιε* @version 1.0*/
public class SpringConsumer {public static void main(String[] args) {AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ConsumerConfig.class);applicationContext.start();HelloController helloController = applicationContext.getBean(HelloController.class);helloController.greet();UserController userController = applicationContext.getBean(UserController.class);List<String> list = userController.selectAll();System.out.println(list);}}

四、Dubbo生產級應用

Java應用的開發幾乎離不開Spring Boot,結合Spring Boot使用Dubbo框架簡單許多。

只需要在啟動類上使用@EnableDubbo注解即可,前面用到的配置類已經不需要了。

dubbo-provider

注釋掉ProviderConfig配置類的@Configuration注解,Spring Boot會自動處理@DubboService注解標注的類,將其暴露為Dubbo服務。

在啟動類ProviderApplication上使用@EnableDubbo注解(因為項目沒有其他配置類了)

application.yml

在配置文件中配置dubbo

dubbo:scan:base-packages: cn.edu.sgu.www.dubbo.provider.serviceapplication:name: dubbo-providerlogger: slf4jqos-port: 22221qos-enable: trueprotocol:name: dubboport: 20880registry:address: nacos://localhost:8848  # 使用Nnacos注冊中心parameters:group: dubboserver:port: 8090spring:application:name: dubbo-provider

dubbo-consumer

注釋掉ConsumerConfig配置類的@Configuration注解。

在啟動類ConsumerApplication上使用@EnableDubbo注解

application.yml

在配置文件中配置dubbo

dubbo:application:name: dubbo-consumerlogger: slf4jqos-port: 22222qos-enable: trueprotocol:name: triport: 50051registry:address: nacos://localhost:8848?group=dubboserver:port: 8091

最后,依次啟動服務提供者dubbo-provider、服務消費者dubbo-consumer,訪問兩個控制器的接口。


好了,文章就分享到這里了,看完不要忘了點贊+收藏哦~


文章代碼已經上傳到Gitee,可按需獲取:

dubbo-apihttps://gitee.com/muyu-chengfeng/dubbo-api.gitApache Dubbo服務提供者項目https://gitee.com/muyu-chengfeng/dubbo-provider.gitApache Dubbo服務消費者項目https://gitee.com/muyu-chengfeng/dubbo-consumer.git

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/98761.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/98761.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/98761.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Python學習——字典和文件

前面python的學習中我們已經學習了python的函數和列表元組相關的內容&#xff0c;接下來我們來學習剩下的python語法&#xff1a;字典和文件 相關代碼已經上傳到作者的個人gitee&#xff1a;樓田莉子/Python 學習喜歡請點個贊謝謝 目錄 字典 創建字典 查找key 新增/修改元素 …

swiper插件的使用

官方網址&#xff1a;https://www.swiper.com.cn/ 1、點擊導航欄&#xff0c;獲取Swiper里邊的下載Swiper 2、選擇要下載的版本【本次案例版本5.4.5】&#xff0c;然后解壓縮文件夾&#xff0c;拿到swiper.min.js和swiper.min.css文件&#xff0c;放到項目對應的css文件和js文…

Vue3+JS 組合式 API 實戰:從項目痛點到通用 Hook 封裝

Vue3 組合式 API 的實戰技巧 —— 組合式 API 幫我解決了不少 Options API 難以應對的問題&#xff0c;尤其是在代碼復用和復雜組件維護上。一、為什么放棄 Options API&#xff1f;聊聊三年項目里的真實痛點?剛接觸 Vue3 時&#xff0c;我曾因 “慣性” 繼續用 Options API 寫…

把 AI 塞進「電梯按鈕」——基于 64 kB 零樣本聲紋的離線故障預測按鈕

標簽&#xff1a;零樣本聲紋、電梯按鈕、離線 AI、TinyML、RISC-V、低功耗、GD32V303、故障預警 ---- 1. 背景&#xff1a;為什么按鈕要「聽聲音」&#xff1f; 全國 700 萬臺電梯&#xff0c;按鈕故障率 0.3 %/年&#xff0c;卻常出現&#xff1a; ? 機械卡滯、觸點氧化&…

清華大學聯合項目 論文解讀 | MoTo賦能雙臂機器人:實現零樣本移動操作

研究背景 移動操作是機器人領域的核心挑戰&#xff0c;它使機器人能夠在各種任務和動態日常環境中為人類提供幫助。傳統的移動操作方法由于缺乏大規模訓練&#xff0c;往往難以在不同任務和環境中實現泛化。而現有操作基礎模型雖在固定基座任務中表現出強泛化性&#xff0c;卻無…

go webrtc - 2 webrtc重要概念

webrtc是一套音視頻傳輸技術生態&#xff0c;不是一個協議或一個什么東西。3種模式本文基于 SFU 形式闡述&#xff01;重要概念&#xff1a;sfu 服務負責&#xff1a;信令 服務負責&#xff1a;peerConnection&#xff1a;track&#xff1a;房間&#xff1a;虛擬分組概念用戶&a…

“下游任務”概念詳解:從定義到應用場景

“下游任務”概念詳解&#xff1a;從定義到應用場景 一、什么是“下游任務”&#xff1f; 在機器學習&#xff08;尤其是深度學習&#xff09;中&#xff0c;“下游任務”&#xff08;Downstream Task&#xff09;是相對“上游過程”而言的目標任務——可以理解為&#xff1a;我…

視頻怎么做成 GIF?用 oCam 一鍵錄制 GIF 動畫超簡單

GIF 動圖因其生動直觀、無需點擊播放的特點&#xff0c;越來越受歡迎。你是否也曾看到一段有趣的視頻&#xff0c;想把它做成 GIF 發給朋友或用在PPT里&#xff1f;其實&#xff0c;將視頻片段轉換為 GIF 并不需要復雜的視頻剪輯技術&#xff0c;使用一款支持直接錄制為 GIF 的…

Vue.config.js中的Webpack配置、優化及多頁面應用開發

Vue.config.js中的Webpack配置、優化及多頁面應用開發 在Vue CLI 3項目中&#xff0c;vue.config.js文件是工程化配置的核心入口&#xff0c;它通過集成Webpack配置、優化策略和多頁面開發支持&#xff0c;為項目構建提供高度可定制化的解決方案。本文將從基礎配置、性能優化、…

行業學習【電商】:直播電商的去頭部化、矩陣號?

聲明&#xff1a;以下部分內容含AI生成這兩個詞是當前直播電商和MCN領域的核心戰略&#xff0c;理解了它們就理解了行業正在發生的深刻變化。一、如何理解“去頭部化”&#xff1f;“去頭部化” 指的是平臺或MCN機構有意識地減少對超頭部主播&#xff08;如曾經的李佳琦、薇婭&…

【MFC視圖和窗口基礎:文檔/視圖的“雙胞胎”魔法 + 單文檔程序】

大家好&#xff0c;我是你的MFC編程小伙伴&#xff01;學MFC就像探險古墓&#xff1a;到處是神秘的“房間”&#xff08;窗口&#xff09;和“寶藏”&#xff08;數據&#xff09;。今天咱們聊聊核心概念 – 視圖、窗口和文檔。這些是MFC的“骨架”&#xff0c;懂了它們&#x…

深度學習(六):代價函數的意義

在深度學習的浩瀚世界中&#xff0c;代價函數&#xff08;Cost Function&#xff09;&#xff0c;又稱損失函數&#xff08;Loss Function&#xff09;或目標函數&#xff08;Objective Function&#xff09;&#xff0c;扮演著至關重要的角色&#xff0c;它就像一個導航員&…

Kable使用指南:Android BLE開發的現代化解決方案

概述 Kable&#xff08;com.juul.kable:core&#xff09;是一個專為Android藍牙低功耗&#xff08;BLE&#xff09;開發設計的Kotlin協程友好庫。它通過提供簡潔的API和響應式編程模式&#xff0c;極大地簡化了BLE設備交互的復雜性。本文將詳細介紹Kable的使用方法&#xff0c;…

Android圖案解鎖繪制

使用到的庫是Pattern Locker,根據示例進行了修改,把默認樣式和自定義樣式進行了合并調整。 設置密碼 布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xm…

Kotlin 協程之 Flow 的理解使用及源碼解析

前言 在前面的文章中&#xff0c;我們已經討論了 Channel 的概念和基本使用以及 Channel 的高階應用。這篇我們來看日常開發中更常用的Flow。 “冷流” 和 “熱流” 的本質 先來梳理一下所謂的 “冷流” 和 “熱流”。 核心概念 我們已經知道 Channel 是 “熱流”&#xff…

簡述ajax、node.js、webpack、git

本系列可作為前端學習系列的筆記&#xff0c;HTML、CSS和JavaScript系列文章 已經收錄在前端專欄&#xff0c;有需要的寶寶們可以點擊前端專欄查看&#xff01; 點贊關注不迷路&#xff01;您的點贊、關注和收藏是對小編最大的支持和鼓勵&#xff01; 系列文章目錄 簡述ajax、…

經營幫會員經營:全方位助力企業高效發展,解鎖商業新可能

在商業競爭愈發激烈的當下&#xff0c;企業若想脫穎而出&#xff0c;高效的經營管理體系至關重要。經營幫的會員經營板塊&#xff0c;憑借豐富且實用的功能&#xff0c;為企業打造了一站式的經營助力平臺&#xff0c;從多維度賦能企業&#xff0c;讓發展之路更順暢。會員經營與…

Vue 封裝Input組件 雙向通信

子組件<template><div class"box"><div class"box-left"><input blur"handleBlur" v-model"localInput" class"box-left-input"> </div><div class"box-right"><p style…

伽馬(gamma)變換記錄

此只記錄伽馬變換原理及其應用結果&#xff08;文章所有內容基于數字圖像處理-岡薩雷斯&#xff09;&#xff0c;和直接用MATLAB代碼生成伽馬變換代碼。一、原理伽馬變換的公式很簡答 就是一個有規律的冪運算 公式如下&#xff1a;一般在圖像中進行應用是 C1 y為不同值時r的輸…

電路學習(六)三極管

三極管是一種電流驅動元器件&#xff08;MOS管為電壓驅動&#xff09;&#xff0c;在電路中可以充當開關&#xff0c;放大電流等作用。本文章參考了尚硅谷的視頻資料。1. 什么是三極管&#xff1f;三極管又被稱為晶體三極管&#xff08;Bipolar Junction Transistor&#xff0c…