摘要:无。
环境
Windows 10 企业版 LTSC 21H2
Java 1.8
Maven 3.6.3
Spring 5.3.31
Spring Boot 2.7.18
Dubbo 3.3.2
1 注解配置
注解配置是最推荐的方式,特别是与Spring Boot集成时最为方便。通过注解可以直接在代码中完成服务的暴露和引用,减少配置文件的编写。
1.1 使用注解
核心注解:
- @DubboService:标注在服务实现类上,替代XML中的
<dubbo:service>。
- @DubboReference:标注在字段或方法上,替代XML中的
<dubbo:reference>。
- @EnableDubbo:标注在启动类上,启用Dubbo注解支持,扫描上述两个注解,自动完成服务的注册和引用。
1.2 项目配置
1.2.1 提供者项目
创建application.yml文件:
application.yml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| server: port: 8081
spring: application: name: dubbo-provider
dubbo: application: name: dubbo-provider qos-enable: false registry: id: demo-center address: zookeeper://127.0.0.1:2181 protocol: name: dubbo port: 20880 scan: base-packages: com.example
|
1.2.2 消费者项目
创建application.yml文件:
application.yml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| server: port: 8082
spring: application: name: dubbo-consumer
dubbo: application: name: dubbo-consumer qos-enable: false registry: id: demo-center address: zookeeper://127.0.0.1:2181 scan: base-packages: com.example
|
1.3 服务管理
1.3.1 提供者项目
使用@DubboService注解:
java1 2 3 4 5 6 7
| @DubboService(registry = "demo-center", timeout = 5000) public class DemoServiceImpl implements DemoService { @Override public String demo(String name) { return name; } }
|
1.3.2 消费者项目
使用@DubboReference注解:
java1 2 3 4 5 6 7 8 9 10
| @RestController public class DemoController { @DubboReference private DemoService demoService;
@GetMapping("/demo") public String demo(@RequestParam String name) { return demoService.demo(name); } }
|
2 XML配置
XML配置是Dubbo最早支持的配置方式,通过Spring的XML命名空间扩展来实现。这种方式配置直观、功能完整,适合传统的Spring项目。
2.1 命名空间
在Spring的XML配置文件中引入Dubbo的命名空间:
pom.xml1 2 3 4 5 6 7 8 9 10
| <?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> </beans>
|
2.2 项目配置
2.2.1 提供者项目
创建spring-context.xml文件:
spring-context.xml1 2 3 4
| <context:component-scan base-package="com.example"/>
<import resource="dubbo-provider.xml"/>
|
创建dubbo-provider.xml文件:
dubbo-provider.xml1 2 3 4 5 6 7 8
| <dubbo:application name="dubbo-provider" qos-enable="false"/>
<dubbo:registry id="demo-center" address="zookeeper://127.0.0.1:2181"/>
<dubbo:protocol name="dubbo" port="20880"/>
<dubbo:service ref="demoService" interface="com.example.service.DemoService" registry="demo-center" timeout="5000"/>
|
2.2.2 消费者项目
创建spring-context.xml文件:
spring-context.xml1 2 3 4
| <context:component-scan base-package="com.example"/>
<import resource="dubbo-consumer.xml"/>
|
创建dubbo-consumer.xml文件:
dubbo-consumer.xml1 2 3 4 5 6
| <dubbo:application name="dubbo-consumer" qos-enable="false"/>
<dubbo:registry id="demo-center" address="zookeeper://127.0.0.1:2181"/>
<dubbo:reference id="demoService" interface="com.example.service.DemoService" registry="demo-center" timeout="5000"/>
|
2.3 服务管理
2.3.1 提供者项目
实现服务:
java1 2 3 4 5 6 7
| @Service("demoService") public class DemoServiceImpl implements DemoService { @Override public String demo(String name) { return name; } }
|
2.3.2 消费者项目
使用服务:
java1 2 3 4 5 6 7 8 9 10
| @RestController public class DemoController { @Autowired private DemoService demoService;
@GetMapping("/demo") public String demo(@RequestParam String name) { return demoService.demo(name); } }
|
3 API配置
API配置是Dubbo最底层的配置方式,通过Java代码直接创建Config对象来完成配置。XML配置和注解配置在底层都是转化为API调用来执行的。这种方式最灵活,适合需要动态配置的场景。
3.1 基本使用
3.1.1 提供者项目
通过API暴露服务:
java1 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
| public class ProviderApplication { public static void main(String[] args) { ApplicationConfig application = new ApplicationConfig(); application.setName("demo-provider");
RegistryConfig registry = new RegistryConfig(); registry.setAddress("zookeeper://127.0.0.1:2181");
ProtocolConfig protocol = new ProtocolConfig(); protocol.setName("dubbo"); protocol.setPort(20880);
ServiceConfig<DemoService> service = new ServiceConfig<>(); service.setInterface(DemoService.class); service.setRef(new DemoServiceImpl());
DubboBootstrap.getInstance() .application(application) .registry(registry) .protocol(protocol) .service(service) .start() .await(); } }
|
3.1.2 消费者项目
通过API引用服务:
java1 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
| public class ConsumerApplication { public static void main(String[] args) { ApplicationConfig application = new ApplicationConfig(); application.setName("demo-consumer");
RegistryConfig registry = new RegistryConfig(); registry.setAddress("zookeeper://127.0.0.1:2181");
ReferenceConfig<DemoService> reference = new ReferenceConfig<>(); reference.setInterface(DemoService.class);
DubboBootstrap.getInstance() .application(application) .registry(registry) .reference(reference) .start();
DemoService service = reference.get();
String result = service.demo("测试"); System.out.println(result); } }
|
3.2 动态配置
3.2.1 提供者项目
动态暴露服务:
java1 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
| public class ProviderApplication { private static final ApplicationConfig application = new ApplicationConfig("demo-provider"); private static final RegistryConfig registry = new RegistryConfig("zookeeper://127.0.0.1:2181"); private static final ProtocolConfig protocol = new ProtocolConfig("dubbo", 20880);
public static void main(String[] args) { initBootstrap(); dynamicExport(DemoService.class, new DemoServiceImpl()); DubboBootstrap.getInstance().start().await(); }
private static volatile boolean initialized = false;
private static synchronized void initBootstrap() { if (initialized) { return; } DubboBootstrap.getInstance() .application(application) .registry(registry) .protocol(protocol); initialized = true; }
public static <T> void dynamicExport(Class<T> interfaceClass, T impl) { ServiceConfig<T> service = new ServiceConfig<>(); service.setInterface(interfaceClass); service.setRef(impl); DubboBootstrap.getInstance().service(service); } }
|
3.2.2 消费者项目
动态引用服务:
java1 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
| public class ConsumerApplication { private static final ApplicationConfig application = new ApplicationConfig("demo-consumer"); private static final RegistryConfig registry = new RegistryConfig("zookeeper://127.0.0.1:2181");
public static void main(String[] args) { initBootstrap(); DemoService service = dynamicReference(DemoService.class); String result = service.demo("测试"); System.out.println(result); }
private static volatile boolean initialized = false;
private static synchronized void initBootstrap() { if (initialized) { return; } DubboBootstrap.getInstance() .application(application) .registry(registry); initialized = true; }
public static <T> T dynamicReference(Class<T> interfaceClass) { ReferenceConfig<T> reference = new ReferenceConfig<>(); reference.setInterface(interfaceClass); DubboBootstrap.getInstance().reference(reference); return reference.get(); } }
|
条