17

公告

今天小马哥非常高兴地向各位小伙伴宣布,官方 Dubbo Spring Boot Starter 1.0.0 公测版已开发完毕,即将发布至 Maven 公有仓库,目前正在内部测试中。

为了收集更多的用户反馈,小马哥诚邀大家一同参与使用、测试以及共同维护,项目工程地址:https://github.com/dubbo/dubb...
如果您喜爱并想了解 Dubbo Spring Boot 工程的动态,不妨点击 GitHub Star 按钮加以关注。

如有任何意见和建议,请将反馈内容发送于此:https://github.com/dubbo/dubb...

了解更多计划和细节,请登录 Dubbo Google Group 参与讨论。

主要特性

Dubbo Spring Boot Starter 致力于简化 Dubbo 应用在 Spring Boot 环境中的开发,主要包括自动装配(Auto-Configure)、外部化配置(Externalized-Configuration)以及生产准备(Actuator)

版本依赖

特别提醒,以下版本依赖尚未发布到 Maven 公有仓库,请自行 checkout 代码到本地,并且通过命令$ ./mvnw clean install构建:

<dependency>
  <groupId>com.alibaba.boot</groupId>
  <artifactId>dubbo-spring-boot-starter</artifactId>
  <version>1.0.0-SNAPSHOT</version>
</dependency>

同时,dubbo-spring-boot-starter 底层依赖 Dubbo 2.5.9

快速上手

服务接口(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);

    }

}

详情请参考官方示例代码:https://github.com/dubbo/dubb...

自动装配(Auto-Configure)

外部化配置(Externalized-Configuration)

中文版本

生产准备(Actuator)

相关文章

Dubbo 新编程模型之注解驱动
Dubbo 新编程模型之外部化配置


mercyblitz
14.7k 声望4.3k 粉丝