Dubbo Spring Boot ProjectDubbo 的 Spring Boot 简化整合
Dubbo Spring Boot Project 致力于简化 Dubbo 应用在 Spring Boot 环境中的开发,主要包括自动装配(Auto-Configure)、外部化配置(Externalized-Configuration)以及生产准备(Actuator)等。
快速上手
服务接口(RPC)
public interface DemoService { String sayHello(String name); }
服务提供者(Provider)
实现DemoServer
接口提供服务(放置于 com.alibaba.boot.dubbo.demo.provider.service
包下):
@Service( version = "1.0.0", application = "${dubbo.application.id}", protocol = "${dubbo.protocol.id}", registry = "${dubbo.registry.id}" ) public class DefaultDemoService implements DemoService { public String sayHello(String name) { return "Hello, " + name + " (from Spring Boot)"; } }
配置application.properties
以提供外部化配置源:
# Spring boot application spring.application.name = dubbo-provider-demo server.port = 9090 management.port = 9091 # Dubbo 组件 (如 @Service , @Reference) 扫描路径,多路径以","分割 dubbo.scan.basePackages = com.alibaba.boot.dubbo.demo.provider.service # Dubbo Config Bean 外部化配置 dubbo.application.id = dubbo-provider-demo dubbo.application.name = dubbo-provider-demo dubbo.protocol.id = dubbo dubbo.protocol.name = dubbo dubbo.protocol.port = 12345 dubbo.registry.id = my-registry
提供服务提供者引导类:
@SpringBootApplication public class DubboProviderDemo { public static void main(String[] args) { SpringApplication.run(DubboProviderDemo.class,args); } }
服务消费者(Consumer)
实现服务消费者 Controller
(位于com.alibaba.boot.dubbo.demo.consumer.controller
包下):
@RestController public class DemoConsumerController { @Reference(version = "1.0.0", application = "${dubbo.application.id}", url = "dubbo://localhost:12345") private DemoService demoService; @RequestMapping("/sayHello") public String sayHello(@RequestParam String name) { return demoService.sayHello(name); } }
增加外部化配置至application.properties
:
# Spring boot application spring.application.name = dubbo-consumer-demo server.port = 8080 management.port = 8081 # Dubbo Config Bean 外部化配置 dubbo.application.id = dubbo-consumer-demo dubbo.application.name = dubbo-consumer-demo dubbo.protocol.id = dubbo dubbo.protocol.name = dubbo dubbo.protocol.port = 12345
提供服务消费者引导类:
@SpringBootApplication(scanBasePackages = "com.alibaba.boot.dubbo.demo.consumer.controller") public class DubboConsumerDemo { public static void main(String[] args) { SpringApplication.run(DubboConsumerDemo.class,args); } }
评论