DubboSpringBootProject 致力于简化Dubbo应用在SpringBoot环境中的开发,主要包括自动装配(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 applicationspring.application.name = dubbo-provider-demoserver.port = 9090management.port = 9091# Dubbo 组件 (如 @Service , @Reference) 扫描路径,多路径以","分割dubbo.scan.basePackages = com.alibaba.boot.dubbo.demo.provider.service# Dubbo Config Bean 外部化配置dubbo.application.id = dubbo-provider-demodubbo.application.name = dubbo-provider-demodubbo.protocol.id = dubbodubbo.protocol.name = dubbodubbo.protocol.port = 12345dubbo.registry.id = my-registry提供服务提供者引导类:
@SpringBootApplicationpublic class DubboProviderDemo { public static void main(String[] args) { SpringApplication.run(DubboProviderDemo.class,args); }}服务消费者(Consumer)实现服务消费者 Controller (位于com.alibaba.boot.dubbo.demo.consumer.controller包下):
@RestControllerpublic 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 applicationspring.application.name = dubbo-consumer-demoserver.port = 8080management.port = 8081# Dubbo Config Bean 外部化配置dubbo.application.id = dubbo-consumer-demodubbo.application.name = dubbo-consumer-demodubbo.protocol.id = dubbodubbo.protocol.name = dubbodubbo.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); }}
评论