前言

这几天在研究 dubbo,看了官网不少的 samples,因为有点代码洁癖,看到下述代码的编写方式,难受的不行,综合查了多个方案,springboot+dubbo+xml 形式,个人感觉是能最大程度的达到 代码解耦、业务代码无污染情况
官网文档很全: http://dubbo.apache.org/zh-cn/docs/user/quick-start.html
GitHub 地址: https://github.com/tigerge000/HeroDubboProvider

public class Provider {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"http://10.20.160.198/wiki/display/dubbo/provider.xml"});
        context.start();
        System.in.read(); // 按任意键退出
    }
}

工程结构

hero-dubbo-provider.xml

准备发布的服务,直接填写到该 xml 中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <bean id="userService" class="com.hero.server.impl.UserInfoServiceImpl"/>
    <dubbo:service interface="com.hero.server.UserInfoService" ref="userService"/>

</beans>

主方法

/**
 * @Des:
 * @Auther: 飞狐
 * @Date: 2019-04-24
 */
@SpringBootApplication
@EnableDubbo //启动dubbo注解,自动导入application.yml配置
@ImportResource(locations = "classpath:spring/hero-dubbo-provider.xml")// 导入provider配置
public class Application {
    public static void main(String[] args){
        SpringApplication.run(Application.class, args);
    }
}

application.yml 配置

dubbo:
  application:
    name: hero-dubbo-provider
    qosEnable: true
    qosAcceptForeignIp: false
    qosPort: 33333
  registry:
    address: 127.0.0.1:2181
    # 读者请自行更改zookeeper地址
    protocol: zookeeper
    check: false
  protocol:
    name: dubbo
    port: 30005
  monitor:
    protocol: register
  consumer:
    check: false
    timeout: 3000

server:
  port: 8061

spring:
  main:
    allow-bean-definition-overriding: true
  output:
    ansi:
      enabled: always

dubbo 特性

  1. 添加了 qos 运维配置,可支持上线/下线指定服务
  2. dubbo telnet 调试地址&端口

详细可看下针对性的测试验证文章
https://testerhome.com/articles/18900


↙↙↙阅读原文可查看相关链接,并与作者交流