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
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

武汉红喜

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

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值