搜文章
推荐 原创 视频 Java开发 iOS开发 前端开发 JavaScript开发 Android开发 PHP开发 数据库 开发工具 Python开发 Kotlin开发 Ruby开发 .NET开发 服务器运维 开放平台 架构师 大数据 云计算 人工智能 开发语言 其它开发
Lambda在线 > 麦田里的守望者 > Apache Dubbo (incubating)

Apache Dubbo (incubating)

麦田里的守望者 2018-07-01

介绍

Apache Dubbo™ (incubating) is a high-performance, java based, open source RPC framework open-sourced by Alibaba. As in many RPC systems, dubbo is based around the idea of defining a service, specifying the methods that can be called remotely with their parameters and return types. On the server side, the server implements this interface and runs a dubbo server to handle client calls. On the client side, the client has a stub that provides the same methods as the server.

Apache Dubbo (incubating) offers three key functionalities, which include interface based remote call, fault tolerance & load balancing, and automatic service registration & discovery. Apache Dubbo (incubating) framework is widely adopted inside Alibaba and outside by other companies including jingdongdangdangqunarkaola, and many others.

环境构建

Prerequisites

  • JDK: version 6 or higher

  • Maven: version 3 or higher

Maven dependency

 
   
   
 
  1. <dependency>

  2.    <groupId>com.alibaba</groupId>

  3.    <artifactId>dubbo</artifactId>

  4.    <version>2.6.2</version>

  5. </dependency>

Define service interface

Since both service provider and service consumer rely on the same interface, it is strongly recommended to put the interface definition below in one separated module which could be shared by both provider module and consumer module.

 
   
   
 
  1. package com.alibaba.dubbo.demo;

  2. public interface DemoService {

  3.    String sayHello(String name);

  4. }

Implement service provider

 
   
   
 
  1. package com.alibaba.dubbo.demo.provider;

  2. import com.alibaba.dubbo.demo.DemoService;

  3. public class DemoServiceImpl implements DemoService {

  4.    public String sayHello(String name) {

  5.        return "Hello " + name;

  6.    }

  7. }

Configure service provider

The code snippet below shows how a dubbo service provider is configured with spring framework, which is recommended, however you could also use API configuration if it’s preferred.

 
   
   
 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3.       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4.       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

  5.       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

  6.    <dubbo:application name="demo-provider"/>

  7.    <dubbo:registry address="zookeeper://192.168.204.128:2181"/>

  8.    <dubbo:protocol name="dubbo" port="20880"/>

  9.    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>

  10.    <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>

  11. </beans>

Start service provider

 
   
   
 
  1. import org.springframework.context.support.ClassPathXmlApplicationContext;

  2. public class Provider {

  3.    public static void main(String[] args) throws Exception {

  4.        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(

  5.                new String[] {"META-INF/spring/dubbo-demo-provider.xml"});

  6.        context.start();

  7.        // press any key to exit

  8.        System.in.read();

  9.    }

  10. }

Configure service consumer

Again, the code below demonstrates spring integration

 
   
   
 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3.       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4.       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

  5.       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

  6.    <dubbo:application name="demo-consumer"/>

  7.    <dubbo:registry address="zookeeper://192.168.204.128:2181"/>

  8.    <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService"/>

  9. </beans>

Run service consumer

 
   
   
 
  1. import com.alibaba.dubbo.demo.DemoService;

  2. import org.springframework.context.support.ClassPathXmlApplicationContext;

  3. public class Consumer {

  4.    public static void main(String[] args) throws Exception {

  5.        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(

  6.                new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});

  7.        context.start();

  8.        // obtain proxy object for remote invocation

  9.        DemoService demoService = (DemoService) context.getBean("demoService");

  10.        // execute remote invocation

  11.        String hello = demoService.sayHello("world");

  12.        // show the result

  13.        System.out.println(hello);

  14.    }

  15. }

note befoer setting up the environment,you need to install zookeeper and make sure that the zookeeper is working properly.

微信公众账号

jiangzz_wx

版权声明:本站内容全部来自于腾讯微信公众号,属第三方自助推荐收录。《Apache Dubbo (incubating)》的版权归原作者「麦田里的守望者」所有,文章言论观点不代表Lambda在线的观点, Lambda在线不承担任何法律责任。如需删除可联系QQ:516101458

文章来源: 阅读原文

相关阅读

关注麦田里的守望者微信公众号

麦田里的守望者微信公众号:jiangzz_wy

麦田里的守望者

手机扫描上方二维码即可关注麦田里的守望者微信公众号

麦田里的守望者最新文章

精品公众号随机推荐