Dubbo

 

Overview

(+) (#)

Serving 1,000+ services with 1,000,000,000+ invocations everyday, Dubbo becomes the key part of Alibaba's SOA solution and has been deployed to the whole alibaba.com family:

So, What is Dubbo?

Dubbo [] is a distributed service framework enpowers applications with service import/export capability with high performance RPC.

It's composed of three kernel parts:

  • Remoting: a network communication framework provides sync-over-async and request-response messaging.
  • Clustering: a remote procedure call abstraction with load-balancing/failover/clustering capabilities.
  • Registry: a service directory framework for service registration and service event publish/subscription

Dubbo can:

  • Integrate different types of RPC solutions(RMI/Hessian...) with unified behavior by the abstraction layer of RPC
  • Support out-of-box, plug-able load balancing and fault tolerance strategies.
  • Achieve graceful service upgrade/downgrade with service registry.

Quick Start

(+) (#)

Dubbo also support usage WITHOUT spring, please refer to: API Reference

Service Provider

(#)

Define the service interface:

DemoService.java
package com.alibaba.dubbo.demo;
 
public interface DemoService {
 
     String sayHello(String name);
 
}

Provide the service implementation

DemoServiceImpl.java
package com.alibaba.dubbo.demo.provider;
 
import com.alibaba.dubbo.demo.DemoService;
 
public class DemoServiceImpl implements DemoService {
 
     public String sayHello(String name) {
         return "Hello " + name;
     }
 
}

Setup the spring configuration

provider.xml
<? xml version = "1.0" encoding = "UTF-8" ?>
     xsi:schemaLocation="http://www.springframework.org/schema/beans
         ">
 
     <!-- Application name -->
     < dubbo:application name = "hello-world-app"  />
 
     <!-- registry address, used for service to register itself -->
     < dubbo:registry address = "multicast://224.5.6.7:1234" />
 
     <!-- expose this service through dubbo protocol, through port 20880 -->
     < dubbo:protocol name = "dubbo" port = "20880" />
 
     <!-- which service interface do we expose? -->
     < dubbo:service interface = "com.alibaba.dubbo.demo.DemoService" ref = "demoService" />
 
     <!-- designate implementation -->
     < bean id = "demoService" class = "com.alibaba.dubbo.demo.provider.DemoServiceImpl" />
 
</ beans >

Kick it off with following java code

Provider.java
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Provider {
 
     public static void main(String[] args) throws Exception {
         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "provider.xml" });
         context.start();
 
         System.out.println( "Press any key to exit." );
         System.in.read();
     }
 
}

Congrats! The DemoService now is exported by dubbo and waiting for incoming requests at port 20880.

Service Consumer

(#)

Setup the spring XML

consumer.xml
<? xml version = "1.0" encoding = "UTF-8" ?>
     xsi:schemaLocation="http://www.springframework.org/schema/beans
         ">
 
     <!-- consumer application name -->
     < dubbo:application name = "consumer-of-helloworld-app"  />
 
     <!-- registry address, used for consumer to discover services -->
     < dubbo:registry address = "multicast://224.5.6.7:1234" />
 
     <!-- which service to consume? -->
     < dubbo:reference id = "demoService" interface = "com.alibaba.dubbo.demo.DemoService" />
 
</ beans >

Client side java code.

Consumer.java
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.dubbo.demo.DemoService;
 
public class Consumer {
 
     public static void main(String[] args) throws Exception {
         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "consumer.xml" });
         context.start();
 
         DemoService demoService = (DemoService)context.getBean( "demoService" ); // get service invocation proxy
         String hello = demoService.sayHello( "world" ); // do invoke!
 
         System.out.println( hello ); // cool, how are you~
     }
 
}

For more, please click here for a complete user guide.

05-24
### Apache Dubbo 微服务框架使用指南 Apache Dubbo 是一种高性能的 Java 基础设施,专注于微服务之间的高效通信和服务治理。以下是关于 Dubbo 的一些关键特性和常见问题解答。 #### 1. Dubbo 核心组件及其作用 Dubbo 提供了一系列的核心功能来支持微服务架构下的开发和运行: - **RPC 调用**: Dubbo 实现了远程过程调用 (Remote Procedure Call),使客户端能够像调用本地方法一样访问远端服务器的方法[^4]。 - **服务注册与发现**: 利用 Zookeeper 或 Nacos 等工具实现服务的动态注册与发现,从而减少硬编码依赖并提升系统的灵活性[^3]。 - **负载均衡**: 支持多种负载均衡算法(如随机、轮询),以优化资源利用并增强系统稳定性[^4]。 - **容错处理**: 集成有完善的错误恢复机制,比如失败重试、超时控制等,保障即使在网络波动情况下也能维持正常运作[^3]。 #### 2. 安装与基本配置流程 要开始使用 Dubbo 构建自己的微服务体系,需完成以下几个主要步骤: ##### a) 环境搭建 确保 JDK 版本满足最低要求,并下载最新版本的 Dubbo 及其相关依赖库文件。 ##### b) 创建 Maven 工程结构 定义好项目的 pom.xml 文件内容,引入必要的依赖项,例如 spring-boot-starter-dubbo 和 hessian-lite 等[^4]。 ```xml <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.x.x</version> </dependency> ``` ##### c) 编写 Provider/Consumer 接口及其实现类 按照约定俗成的方式分别编写服务提供方(Service Provider Interface, SPI)以及消费方(Client Side Stub), 并通过 @Service/@Reference 注解标注它们的关系[^1]。 ```java // Service interface definition public interface DemoService { String sayHello(String name); } // Implementation of the service on provider side. @Service(version = "1.0.0") public class DemoServiceImpl implements DemoService{ public String sayHello(String name){ return "Hello,"+name; } } ``` 对于消费者而言,则只需声明引用即可获得对应实例对象。 ```java @Reference(version="1.0.0") private DemoService demoService; @Test public void testSayHello(){ System.out.println(demoService.sayHello("world")); } ``` #### 3. 解决 Dubbo 中常见的几个疑问点 - Q: 如果我的项目里既有 RESTful API又有 Dubbo RPC 怎么办? A: 这两种风格完全可以共存于同一个工程之中,只需要区分清楚各自的职责范围就可以了。REST 更适合外部暴露给第三方使用的开放接口;而内部紧密耦合的部分则推荐采用更高效的二进制形式传输——即 Dubbo 所擅长之处[^4]。 - Q: 我们现在正在考虑从传统单体应用迁移到基于 Dubbo 的微服务平台上来,请问有哪些注意事项吗? A: 在迁移过程中需要注意以下几点事项:一是评估现有业务逻辑是否具备拆分的可能性;二是提前规划好新旧两套体系间的过渡方案;三是充分测试各个子模块独立部署后的表现情况,最后再逐步替换掉原有的部分直至完全切换完毕为止[^2]. ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值