dubbo入门

原生

服务端

public static void main(String[] args) throws IOException {
    //应用名称
    ApplicationConfig applicationConfig = new ApplicationConfig("server");
    // 注册中心  RegistryConfig.NO_AVAILABLE
    RegistryConfig registryConfig = new RegistryConfig("multicast://224.1.1.1:3333");
    //协议 端口号
    ProtocolConfig protocolConfig = new ProtocolConfig("dubbo", -1);
    ServiceConfig serviceConfig = new ServiceConfig();
    serviceConfig.setInterface(UserService.class);
    serviceConfig.setRef(new UserServiceImpl());
    serviceConfig.setApplication(applicationConfig);
    serviceConfig.setProtocol(protocolConfig);
    serviceConfig.setRegistry(registryConfig);
    serviceConfig.export();
    System.out.println("服务已暴露");
    System.in.read();
}

客户端

public static void main(String[] args) throws IOException {
    ApplicationConfig applicationConfig = new ApplicationConfig("client");

    RegistryConfig registryConfig = new RegistryConfig("multicast://224.1.1.1:3333");

    ReferenceConfig referenceConfig = new ReferenceConfig();
    referenceConfig.setRegistry(registryConfig);
    referenceConfig.setInterface(UserService.class);
    referenceConfig.setUrl("dubbo://192.168.150.1:20880/cn.jaminye.dubbo.base.UserService");
    referenceConfig.setApplication(applicationConfig);
    UserService userService = (UserService) referenceConfig.get();
    System.out.println(userService.getName("1111"));
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
}

spring的方式

服务端

<?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-4.3.xsd
       http://dubbo.apache.org/schema/dubbo
       http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="hello-world-app"/>
    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <!--    <dubbo:registry address="N/A"/>-->
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="cn.jaminye.base.UserService" ref="userService"/>
    <!-- 和本地bean一样实现服务 -->
    <bean id="userService" class="cn.jaminye.dubbo.server.UserServiceImpl"/>
</beans>
public static void main(String[] args) throws IOException {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
    context.start();
    System.out.println("服务暴露完成");
    System.in.read();
}

客户端

<?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-4.3.xsd
       http://dubbo.apache.org/schema/dubbo
       http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="consumer-of-helloworld-app"/>
    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <!--    <dubbo:registry address="multicast://224.5.6.7:1234"/>-->
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="userService"
                     interface="cn.jaminye.base.UserService"
                     url="dubbo://192.168.21.1:20880/cn.jaminye.base.UserService"/>
</beans>
public static void main(String[] args) {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
    context.start();
    UserService userService = (UserService) context.getBean("userService");
    System.out.println(userService.getName("111"));
}

springboot

dubbo.application.name=springboot-server
dubbo.registry.address=N/A
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
@EnableDubbo
@SpringBootApplication
public class SpringbootDubboServerApplication {

public static void main(String[] args) {
    SpringApplication.run(SpringbootDubboServerApplication.class, args);
    System.out.println("服务已启动");
}

}

客户端

dubbo.application.name=springboot-client
dubbo.registry.address=N/A
@EnableDubbo
@SpringBootApplication
public class SpringbootDubboClientApplication {
    @Reference(url = "dubbo://192.168.21.1:20881/cn.jaminye.base.UserService")
    UserService userService;

    public static void main(String[] args) {
        SpringApplication.run(SpringbootDubboClientApplication.class, args).close();
    }

    @Bean
    public ApplicationRunner getBean() {
        return args -> {
            System.out.println(userService.getName("1"));
        };
    }
}
Dubbo 配置项
  1. aplication 当前应用信息
  2. registry 注册中心 register是否注册 subscribe是否订阅服务,check 是否检测注册中心可用
  3. protocol 配置服务协议
  4. service 暴露服务定义服务信息 group 分组 version 版本 timeout 超时时间 loadbalance负载均衡策略
  5. provoder service配置的模版 默认值 threads 线程数量 threadpool 线程池
  6. reference 创建远程代理
  7. consumer reference配置模版
  8. method reference与serice的方法级配置
  9. argument 方法参数的配置

timeout优先级
reference method–>service method -->reference --> service -->consumer–>provider

SDK自动化构建

  1. 接口信息
    • 接口、模型、异常等统一放置于一个模块,实现等放置于另一个模块,调用方使用maven进行引用
    • 服务端开发人员编写接口->push到远程仓库->jekins构建指定版本并发布到私服->调用方基于maven下载使用
  2. 接口兼容
    • 接口要做到向下兼容 接口参数尽量以对象的形式封装,model属性只增不删,做废做好标识
    • 如不可兼容 通知调用方整改
      开发联调
  3. 服务端和客户端使用相同的group
  4. 直连
  5. 只订阅 register=“false” 防止服务不正常导致其他客户端连接错误

Redis注册中心

<dependency>
	<groupId>org.apache.dubbo</groupId>
	<artifactId>dubbo-registry-redis</artifactId>
    <version>2.7.6</version>
</dependency>
  1. redis注册是hash数据结构
  2. value是注册时间加60s 线程每30s续命
  3. 服务端挂掉数据不会删除 30秒后注册中心感知
  4. 客户端与服务端每2秒心跳
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Jamin_Ye

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

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

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

打赏作者

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

抵扣说明:

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

余额充值