zoukankan      html  css  js  c++  java
  • 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秒心跳
    作者: JaminYe
    版权声明:本文原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
  • 相关阅读:
    Json 格式化, 排序, 标准格式 ,修改字段名 @JSONField @JsonIgnore @JsonIgnoreProperties
    skywalking 服务间链路追踪 (4) --- 项目中追踪各个方法, 监测方法运行时间
    skywalking 服务间链路追踪 (3) ---整合docker使用
    skywalking 服务间链路追踪 (2) ---使用
    skywalking 服务间链路追踪 (1) ---安装
    Exception AOP 异常处理
    messageHelper 统一管理项目的中message
    Json 对象比较
    Nginx 针对建立TCP连接优化
    Nginx 建立三次握手
  • 原文地址:https://www.cnblogs.com/JaminYe/p/13526434.html
Copyright © 2011-2022 走看看