dubbo-spring-boot-starter使用指南

https://github.com/apache/incubator-dubbo-spring-boot-project.git

dependencies

<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>0.2.0</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.6.4</version>
</dependency>

千万不要使用以下依赖(已废弃)

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

另外,如果你的公共包如xx-common也引用了dubbo,请在xx-common里加上<optional>true</optional>

provider

application.yml

dubbo:
  registry:
    protocol: zookeeper
    address: 127.0.0.1:2181
    id: my-registry
  protocol:
    port: 20882
    name: dubbo
    status: server
    id: dubbo
  application:
    name: demo-provider
    id: demo-provider
    qosEnable: true
    qosPort: 22223
  scan:
    basePackages: org.hongxi.whatsmars.dubbo.demo.provider.service

provider

@Service(version = "1.0.0")
public class DemoServiceImpl implements DemoService {

    @Autowired
    private UserService userService;

    public String sayHello(String name) {
        boolean registerSuccess = userService.register(name);
        System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name + ", registerSuccess:" + registerSuccess + ", response form provider: " + RpcContext.getContext().getLocalAddress();
    }
    
}

consumer

application.yml

dubbo:
  registry:
    protocol: zookeeper
    address: 127.0.0.1:2181
    id: my-registry
  protocol:
    port: 20882
    name: dubbo
    id: dubbo
  application:
    name: demo-consumer
    id: demo-consumer
    qosEnable: true
    qosPort: 22224

consumer

/**
 * Created by javahongxi on 2017/12/4.
 * 将服务调用包装,可以方便进行调用监控
 */
@Component
public class DemoRpc {

    @Reference(version = "1.0.0")
    private DemoService demoService;

    public String sayHello(String name) {
        String result = null;
        try {
            result = demoService.sayHello(name);
        } catch (Exception e) {
            // log
            e.printStackTrace();
        }
        return result;
    }

}

调用多个注册中心的服务

application.yml

dubbo:
  registry:
    protocol: zookeeper
    address: 127.0.0.1:2181
    id: my-registry
  protocol:
    port: 20882
    name: dubbo
    id: dubbo
  application:
    name: demo-consumer
    id: demo-consumer
    qosEnable: true
    qosPort: 22224
registry:
  other:
    protocol: zookeeper
    address: 127.0.0.1:2181
    id: other
@Configuration
public class DubboConfig {

    /**
     * 配置文件里配置默认的,这里配置其他需要的
     */
    @Bean("otherRegistry")
    @ConfigurationProperties(prefix = "registry.other")
    public RegistryConfig otherRegistry() {
        return new RegistryConfig();
    }
}
/**
 * Created by javahongxi on 2017/12/4.
 * 将服务调用包装,可以方便进行调用监控
 */
@Component
public class DemoRpc {

    @Reference(version = "1.0.0")
    private DemoService demoService;

    @Reference(version = "1.0.0", registry = "otherRegistry")
    private OtherService otherService;

    public String sayHello(String name) {
        String result = null;
        try {
            result = demoService.sayHello(name);
        } catch (Exception e) {
            // log
            e.printStackTrace();
        }
        return result;
    }

    public String sayHello2(String name) {
        String result = null;
        try {
            result = otherService.sayHello(name);
        } catch (Exception e) {
            // log
            e.printStackTrace();
        }
        return result;
    }
}

发布服务到多个注册中心

@Service(
        version = "1.0.0",
        registry = {"my-registry", "otherRegistry"}
)
public class OtherServiceImpl implements OtherService {

    @Autowired
    private UserService userService;

    public String sayHello(String name) {
        boolean registerSuccess = userService.register(name);
        System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name + ", registerSuccess:" + registerSuccess + ", response form provider: " + RpcContext.getContext().getLocalAddress();
    }
    
}

PS: Dubbo优雅停机应该 kill -> sleep -> kill -9,切勿直接kill -9

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
<h3>回答1:</h3><br/>dubbo-spring-boot-starter是一个基于Spring Boot框架的Dubbo服务框架的启动器。它可以帮助开发者快速集成Dubbo服务框架到Spring Boot应用中,简化了Dubbo服务的配置和使用。通过使用dubbo-spring-boot-starter,开发者可以更加方便地实现Dubbo服务的注册、发现、调用等功能,提高了开发效率和代码质量。 <h3>回答2:</h3><br/>Dubbo-Spring-Boot-Starter是一款用于在Spring Boot应用中接入Apache Dubbo的开源框架。它可以帮助开发人员快速、方便地实现Dubbo在Spring Boot项目中的集成和使用。 Dubbo是基于Java的高性能、轻量级分布式服务框架,帮助开发者更轻松、更透明地构建分布式服务,同时它也是阿里巴巴开源的一款优秀的分布式框架。然而,Dubbo在使用过程中存在一些繁琐的配置,为开发人员增加了很多工作量和麻烦,而Dubbo-Spring-Boot-Starter则是为了消除这些繁琐的配置和使用难度而产生的。 在使用Dubbo-Spring-Boot-Starter之后,开发人员可以将Dubbo与Spring Boot框架快速整合,并通过简单的配置实现Dubbo的相关功能,比如注册中心、服务提供者和服务消费者等。在使用过程中,Dubbo-Spring-Boot-Starter为开发人员提供了大量的自动化配置和默认值,比如默认的Dubbo协议、负载均衡策略、超时时间等,同时支持自定义扩展自动配置的方式。此外,Dubbo-Spring-Boot-Starter还提供了一些Web端的监控和管理功能,帮助开发人员更方便地进行运维和监控。 总之,Dubbo-Spring-Boot-Starter是对Dubbo和Spring Boot框架的完美结合,它简化了Dubbo在Spring Boot项目中的使用和配置,提高了开发效率,降低了使用成本,是一款值得开发人员尝试的框架。 <h3>回答3:</h3><br/>Dubbo-spring-boot-starter是Spring Boot与Dubbo框架集成的一个starter,它提供了快速集成Dubbo框架的能力。Dubbo是一款分布式的服务治理框架,支持高性能的异步通信,并提供了负载均衡、服务降级、集群容错等丰富的特性。 Dubbo-spring-boot-starter包含了Dubbo所需的基本配置信息,只需要在Spring Boot项目的配置文件中添加相应的配置即可快速集成Dubbo框架。通过简单的配置,Dubbo-spring-boot-starter能够让Spring Boot项目成为Dubbo服务的提供者或消费者。 Dubbo-spring-boot-starter的使用非常方便,只需要在项目中添加对应的依赖即可开始使用,它提供了多种配置方式,如注解、XML和properties等。同时,Dubbo-spring-boot-starter也提供了丰富的自定义配置选项,可以满足不同场景的需求。 除了Dubbo-spring-boot-starter,还有其他与Dubbo框架集成的starter,如dubbo-spring-cloud-starterdubbo-spring-boot-project。这些starter都提供了方便快捷的Dubbo集成方式,使得Dubbo框架在Spring Boot生态中的应用更加广泛。 总之,Dubbo-spring-boot-starter是一款优秀的starter,可以让Spring Boot项目快速集成Dubbo框架,提供或消费Dubbo服务,为分布式服务治理带来便利。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

武汉红喜

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值