美文网首页
dubbo初探

dubbo初探

作者: cccccttttyyy | 来源:发表于2018-11-12 17:24 被阅读0次
  1. 定义
  2. 快速开始

2.1 demo-provider部分
2.2 demo-consumer部分
2.3 启动

1.定义

Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和Spring框架无缝集成。

2.快速开始

Dubbo最常用的使用方式是运行在spring 框架中。
创建一个简单的demo我们需要创建两个部分
demo-provider、demo-consumer

demo-provider部分

此部分的结构


image.png

github地址
git clone https://github.com/apache/incubator-dubbo.git
运行时加上 -Djava.net.preferIPv4Stack=true 如果你使用的IDE 是 Intellij Idea

DemoService.java

package org.apache.dubbo.demo;
public interface DemoService {
    String sayHello(String name);
}

DemoServiceImpl.java

package org.apache.dubbo.demo.provider;
import org.apache.dubbo.demo.DemoService;

public class DemoServiceImpl implements DemoService {
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

provider.xml

<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">

    <!-- provider's application name, used for tracing dependency relationship -->
    <dubbo:application name="demo-provider"/>
    <!-- use multicast registry center to export service -->
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <!-- use dubbo protocol to export service on port 20880 -->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!-- service implementation, as same as regular local bean -->
    <bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/>
    <!-- declare the service interface to be exported -->
    <dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService"/>
</beans>

使用multicast不需要额外安装且简单 也可以使用zookeeper

log4j.properties 支持slf4j

###set log levels###
log4j.rootLogger=info, stdout
###output to the console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n

Provider.java

package org.apache.dubbo.demo.provider;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {

    public static void main(String[] args) throws Exception {
        System.setProperty("java.net.preferIPv4Stack", "true");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider.xml"});
        context.start();
        System.out.println("Provider started.");
        System.in.read(); // press any key to exit
    }
}

demo-consumer部分

此部分结构


image.png

github地址:
git clone https://github.com/apache/incubator-dubbo.git
运行前先启动provider
如果IDE使用的是Intellij IDEA 请在运行时加上 -Djava.net.preferIPv4Stack=true

配置
配置文件 resource/META-INFO.spring/dubbo-demo-consumer.xml
修改dubbo:registery 为真正的registery center 地址

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

    <!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
    don't set it same as provider -->
    <dubbo:application name="demo-consumer"/>
    <!-- use multicast registry center to discover service -->
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <!-- generate proxy for the remote service, then demoService can be used in the same way as the
    local regular interface -->
    <dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.demo.DemoService"/>
</beans>

Consumer.java

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.apache.dubbo.demo.DemoService;
 
public class Consumer {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"META-INF/spring/dubbo-demo-consumer.xml"});
        context.start();
        // Obtaining a remote service proxy
        DemoService demoService = (DemoService)context.getBean("demoService");
        // Executing remote methods
        String hello = demoService.sayHello("world");
        // Display the call result
        System.out.println(hello);
    }
}

配置log4j类似provider中的那样

启动

运行org.apache.dubbo.demo.provider.Provider
再运行org.apache.dubbo.demo.provider.Consumer能看到打印结果:
Hello world

相关文章

  • dubbo初探

    定义快速开始2.1 demo-provider部分2.2 demo-consumer部分2.3 启动 1.定义 D...

  • dubbo rpc 初探

    由于网络的抖动的原因,经常在cat监控当中,看到dubbo的com.alibaba.dubbo.rpc.RpcEx...

  • Dubbo Admin 初探

    Dubbo Admin介绍 在Dubbo2.6.0版本之前,Dubbo的工程内部包含有dubbo-admin的模块...

  • Dubbo DefaultFuture 初探

    最近在学习Java并发相关的内容,偶然看到了介绍Dubbo 的DefaultFuture,之前也没有对这方面做过什...

  • dubbo-mybatis初探

    前言 我方阵营项目中用到的微服务架构是spring cloud系,持久层用的是Hibernate+QueryDSL...

  • Dubbo zookeeper 初探【转】

    先把zookeeper在本地给安装好, 这里的话讲述了两个工程一个工程是提供服务的,一个工程是调用服务的,因为du...

  • Dubbo Admin 2.7.X 初探

    开篇 为了统一不同配置中心(如Zookeeper、Nacos、Etcd等)对于服务治理规则的实现,Dubbo2.7...

  • 苹果 ARKit 初探

    苹果 ARKit 初探 苹果 ARKit 初探

  • DUBBO-POSTMAN(dubbo接口测试,dubbo场景测

    DUBBO-POSTMAN(dubbo接口测试,dubbo场景测试,dubbo集成测试) DUBBO-POSTMA...

  • dubbo记录

    目录 《dubbo入门》《dubbo管理控制台》《dubbo超时重传》《dubbo直连》《dubbo实现服务降级》...

网友评论

      本文标题:dubbo初探

      本文链接:https://www.haomeiwen.com/subject/grpcfqtx.html