一 概述 Dubbo如今作为apache的开源项目,也是非常活跃,使用的公司也不少。并且apache官网dubbo的中文文档也非常详尽,朋友们如果用了dubbo,建议仔细阅读几遍官网 的文档。
二 快速上手 您可以为您的工程引入最新 dubbo-spring-boot-starter
的发布,增加以下依赖到工程的 pom.xml
文件中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 <properties > <spring-boot.version > 2.3.0.RELEASE</spring-boot.version > <dubbo.version > 2.7.7</dubbo.version > </properties > <dependencyManagement > <dependencies > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-dependencies</artifactId > <version > ${spring-boot.version}</version > <type > pom</type > <scope > import</scope > </dependency > <dependency > <groupId > org.apache.dubbo</groupId > <artifactId > dubbo-dependencies-bom</artifactId > <version > ${dubbo.version}</version > <type > pom</type > <scope > import</scope > </dependency > <dependency > <groupId > org.apache.dubbo</groupId > <artifactId > dubbo</artifactId > <version > ${dubbo.version}</version > <exclusions > <exclusion > <groupId > org.springframework</groupId > <artifactId > spring</artifactId > </exclusion > <exclusion > <groupId > javax.servlet</groupId > <artifactId > servlet-api</artifactId > </exclusion > <exclusion > <groupId > log4j</groupId > <artifactId > log4j</artifactId > </exclusion > </exclusions > </dependency > </dependencies > </dependencyManagement > <dependencies > <dependency > <groupId > org.apache.dubbo</groupId > <artifactId > dubbo-spring-boot-starter</artifactId > <version > 2.7.7</version > </dependency > <dependency > <groupId > org.apache.dubbo</groupId > <artifactId > dubbo</artifactId > </dependency > </dependencies >
Dubbo RPC API ,由服务提供方为服务消费方暴露接口 :
1 2 3 public interface DemoService { String sayHello (String name) ; }
实现 DemoService
接口(服务提供者)
1 2 3 4 5 6 7 8 9 10 11 12 13 @Service (version = "1.0.0" )public class DefaultDemoService implements DemoService { @Value ("${dubbo.application.name}" ) private String serviceName; public String sayHello (String name) { return String.format("[%s] : Hello, %s" , serviceName, name); } }
编写Spring Boot 引导程序
1 2 3 4 5 6 7 @EnableAutoConfiguration public class DubboProviderDemo { public static void main (String[] args) { SpringApplication.run(DubboProviderDemo.class,args); } }
配置 application.properties
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # Spring boot application spring.application.name=dubbo-auto-configuration-provider-demo # Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service dubbo.scan.base-packages=org.apache.dubbo.spring.boot.sample.provider.service # Dubbo Application ## The default value of dubbo.application.name is ${spring.application.name} ## dubbo.application.name=${spring.application.name} # Dubbo Protocol dubbo.protocol.name=dubbo dubbo.protocol.port=12345 ## Dubbo Registry dubbo.registry.address=N/A
服务消费者:通过 @Reference
注入 DemoService
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 @EnableAutoConfiguration public class DubboAutoConfigurationConsumerBootstrap { private final Logger logger = LoggerFactory.getLogger(getClass()); @Reference (version = "1.0.0" , url = "dubbo://127.0.0.1:12345" ) private DemoService demoService; public static void main (String[] args) { SpringApplication.run(DubboAutoConfigurationConsumerBootstrap.class).close(); } @Bean public ApplicationRunner runner () { return args -> { logger.info(demoService.sayHello("mercyblitz" )); }; } }
配置 application.yml
:
1 2 3 spring: application: name: dubbo-auto-configure-consumer-sample