首页 > 其他分享 >实战系列(一)Dubbo和Spring Cloud的区别,包含代码详解

实战系列(一)Dubbo和Spring Cloud的区别,包含代码详解

时间:2023-10-25 18:32:57浏览次数:31  
标签:Dubbo 服务 String Spring import Cloud


Dubbo 和 Spring Cloud 都是微服务架构中的重要框架,但它们的定位和关注点不同。Dubbo 是阿里巴巴开源的一个高性能、轻量级的 RPC 框架,主要用于构建微服务之间的服务治理。而 Spring Cloud 是基于 Spring Boot 的一个微服务架构开发工具,它提供了一系列的开发工具和服务,帮助开发者快速构建分布式系统和微服务架构。

实战系列(一)Dubbo和Spring Cloud的区别,包含代码详解_spring

实战系列(一)Dubbo和Spring Cloud的区别,包含代码详解_spring cloud_02

在本文中,我们将从以下几个方面对比 Dubbo 和 Spring Cloud:

  1. 概述
  2. 核心功能
  3. 代码示例
  4. 适用场景

1. 概述

Dubbo 是阿里巴巴开源的一个高性能、轻量级的 RPC 框架,主要用于构建微服务之间的服务治理。它提供了服务注册与发现、服务路由、负载均衡、服务熔断等功能。Dubbo 支持多种服务治理组件,如 Nacos、Zookeeper、Eureka 等。 Spring Cloud 是基于 Spring Boot 的一个微服务架构开发工具,它提供了一系列的开发工具和服务,帮助开发者快速构建分布式系统和微服务架构。Spring Cloud 提供了服务注册与发现、服务路由、负载均衡、服务熔断等功能,同时支持多种服务治理组件,如 Eureka、Consul、Zookeeper 等。

2. 核心功能

Dubbo 和 Spring Cloud 都提供了服务注册与发现、服务路由、负载均衡、服务熔断等功能。下面我们分别介绍它们的核心功能。 2.1 Dubbo 核心功能

  • 服务注册与发现:Dubbo 支持多种服务注册中心,如 Nacos、Zookeeper、Eureka 等。服务提供者将服务注册到注册中心,服务消费者从注册中心获取服务提供者的信息,从而实现服务之间的调用。
  • 服务路由:Dubbo 支持多种服务路由方式,如服务名称路由、服务版本路由、负载均衡路由等。
  • 负载均衡:Dubbo 支持多种负载均衡策略,如轮询、随机、最少连接数等。
  • 服务熔断:Dubbo 支持服务熔断功能,当服务提供者出现异常时,可以自动将流量切换到其他可用的服务提供者。 2.2 Spring Cloud 核心功能
  • 服务注册与发现:Spring Cloud 提供了服务注册与发现功能,支持多种服务注册中心,如 Eureka、Consul、Zookeeper 等。服务提供者将服务注册到注册中心,服务消费者从注册中心获取服务提供者的信息,从而实现服务之间的调用。
  • 服务路由:Spring Cloud 提供了服务路由功能,支持多种服务路由方式,如服务名称路由、服务版本路由、负载均衡路由等。
  • 负载均衡:Spring Cloud 提供了负载均衡功能,支持多种负载均衡策略,如轮询、随机、最少连接数等。
  • 服务熔断:Spring Cloud 提供了服务熔断功能,当服务提供者出现异常时,可以自动将流量切换到其他可用的服务提供者。

3. 代码示例

接下来我们分别给出 Dubbo 和 Spring Cloud 的简单代码示例。 3.1 Dubbo 代码示例 假设我们有一个简单的服务提供者 HelloService,我们使用 Dubbo 构建这个服务:

// HelloService.java  
package com.example.dubbo.service;
import com.alibaba.dubbo.config.annotation.DubboService;
@DubboService  
public interface HelloService {  
   String sayHello(String name);  
}
// HelloServiceImpl.java  
package com.example.dubbo.service.impl;
import com.example.dubbo.service.HelloService;  
import org.springframework.beans.factory.annotation.Value;  
import org.springframework.stereotype.Component;
@Component  
public class HelloServiceImpl implements HelloService {  
   @Value("${hello.name}")  
   private String name;
   @Override  
   public String sayHello(String name) {  
       return "Hello, " + name + "!";  
   }  
}

接下来我们使用 Dubbo 创建一个简单的服务消费者:

// DubboConsumer.java  
package com.example.dubbo.consumer;
import com.alibaba.dubbo.config.annotation.Reference;  
import com.example.dubbo.service.HelloService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.web.bind.annotation.Get;
package com.example.dubbo.consumer;
import com.alibaba.dubbo.config.annotation.Reference;  
import com.example.dubbo.service.HelloService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.RequestParam;  
import org.springframework.web.bind.annotation.RestController;
@RestController  
public class DubboConsumer {
   @Reference(interfaceClass = HelloService.class)  
   private HelloService helloService;
   @GetMapping("/hello")  
   public String sayHello(@RequestParam("name") String name) {  
       return helloService.sayHello(name);  
   }  
}

3.2 Spring Cloud 代码示例 假设我们有一个简单的服务提供者 HelloService,我们使用 Spring Cloud 构建这个服务:

// HelloService.java  
package com.example.springcloud.service;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.RestController;
@RestController  
@EnableDiscoveryClient  
public class HelloService {
   @GetMapping("/hello")  
   public String sayHello() {  
       return "Hello, World!";  
   }  
}

接下来我们使用 Spring Cloud 创建一个简单的服务消费者:

// SpringCloudConsumer.java  
package com.example.springcloud.consumer;
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.cloud.client.discovery.DiscoveryClient;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.RequestParam;  
import org.springframework.web.bind.annotation.RestController;
@RestController  
public class SpringCloudConsumer {
   @Autowired  
   private DiscoveryClient discoveryClient;
   @GetMapping("/hello")  
   public String sayHello(@RequestParam("name") String name) {  
       String serviceUrl = discoveryClient.getServiceUrl("hello-service", "hello-service");  
       return serviceUrl + "/hello";  
   }  
}

3.3 服务发现与注册: Dubbo 使用 Dubbo 服务注册中心进行服务发现和注册,可以实现服务的自动发现和负载均衡。Spring Cloud 则使用 Netflix Eureka 或者 Consul 作为服务注册中心。 以下是 Dubbo 服务注册中心的一个简单示例:

public class DubboServiceRegister {  
   public void register(String interfaceName, String version, String group, String serviceName) {  
       URL url = new URL("register", "127.0.0.1:2181", new Properties());  
       Invoker invoker = new Invoker(interfaceName, version, group);  
       invoker.setServiceName(serviceName);  
       DubboServiceRegister.getRegisterInstance().register(url, invoker);  
   }  
}

以下是 Spring Cloud 使用 Eureka 进行服务注册的一个简单示例:

@Configuration  
public class EurekaServerConfig {  
   @Value("${eureka.client.serviceUrl.defaultZone}")  
   private String defaultZone;
   @Bean  
   public EurekaServer eurekaServer() {  
       EurekaServer eurekaServer = new EurekaServer();  
       eurekaServer.setServiceUrl(defaultZone);  
       return eurekaServer;  
   }  
}

3.4 配置管理: Dubbo 使用 Zookeeper 进行配置管理,可以实现配置的版本控制、动态更新等功能。Spring Cloud 则使用 Spring Cloud Config 进行配置管理,也可以实现配置的版本控制、动态更新等功能。 以下是 Dubbo 使用 Zookeeper 进行配置管理的一个简单示例:

@Component  
public class DubboConfigManager {  
   @Value("${dubbo.config.zkAddress}")  
   private String zkAddress;
   @Autowired  
   private Zookeeper zkClient;
   public void saveConfig(String key, String value) {  
       zkClient.writeData(zkAddress + "/" + key, value, new Watcher() {  
           public void process(WatchedEvent event) {  
               if (event.getState() == Watcher.Event.KeeperState.SyncConnected) {  
                   saveConfig(key, value);  
               }  
           }  
       });  
   }  
}

以下是 Spring Cloud 使用 Spring Cloud Config 进行配置管理的一个简单示例:

@Configuration  
@EnableConfigServer  
public class ConfigServerConfig {  
   @Value("${spring.cloud.config.server.port}")  
   private int port;
   @Bean  
   public ConfigServer configServer(ConfigServerProperties configServerProperties) {  
       return new ConfigServer(configServerProperties, this.port);  
   }  
}

3.5 负载均衡: Dubbo 使用 Dubbo 服务治理中心进行负载均衡,可以实现服务的负载均衡、容错等功能。Spring Cloud 则使用 Spring Cloud Gateway 或者 Ribbon 进行负载均衡。 以下是 Dubbo 进行负载均衡的一个简单示例:

public class DubboLoadBalance {  
   public void doLoadBalance(String interfaceName, String version, String group, String serviceName) {  
       URL url = new URL("loadbalance", "127.0.0.1:2181", new Properties());  
       Invoker invoker = new Invoker(interfaceName, version, group);  
       invoker.setServiceName(serviceName);  
       DubboServiceRegister.getLoadBalanceInstance().doLoadBalance(url, invoker);  
   }  
}

以下是 Spring Cloud 使用 Spring Cloud Gateway 进行负载均衡的一个简单示例:

@Configuration  
public class GatewayConfig {  
   @Value("${spring.cloud.gateway.routes}")  
   private String routes;
   @Bean  
   public RouteLocator routeLocator(RouteLocatorBuilder builder) {  
       return new RouteLocator(builder.routes(routes));  
   }  
}

4. 适用场景

Dubbo 和 Spring Cloud 都是微服务架构中的重要框架,但它们的定位和关注点不同。以下是它们各自的适用场景: 4.1 Dubbo 适用场景 Dubbo 主要适用于以下场景:

  • 需要高性能、轻量级的 RPC 框架。
  • 服务提供者和服务消费者之间需要进行服务治理,如服务注册与发现、服务路由、负载均衡、服务熔断等。
  • 阿里巴巴生态圈中的项目,因为 Dubbo 是阿里巴巴开源的框架,与其他阿里巴巴开源项目(如 Spring Cloud、Nacos 等)集成更加方便。 4.2 Spring Cloud 适用场景 Spring Cloud 主要适用于以下场景:
  • 已经使用 Spring Boot 的项目,希望快速构建分布式系统和微服务架构。
  • 需要使用多种服务治理组件,如 Eureka、Consul、Zookeeper 等。
  • 希望在一个统一的框架中实现服务注册与发现、服务路由、负载均衡、服务熔断等功能。 总之,Dubbo 和 Spring Cloud 都是微服务架构中的重要框架,但它们的定位和关注点不同。在选择时,需要根据项目的具体情况和需求来决定使用哪个框架。

标签:Dubbo,服务,String,Spring,import,Cloud
From: https://blog.51cto.com/u_15814859/8023745

相关文章

  • Springboot 启动
    参考:rhyme  : SPRINGBOOT启动流程及其原理fhfirehuo: SpringBoot的启动流程Spring 框架就像一个家族,有众多衍生产品例如 boot、security、jpa等等;但他们的基础都是Spring的ioc和aop,ioc 提供了依赖注入的容器, aop解决了面向切面编程,然后在此两者的基础上实现了其他延......
  • SpringMVC-成功通过param传递参数
    继续学习SpringMVC,JSON和Param是两种常用的传值的方法,其中JSON是用的最为普遍的,而现在我们来学习一下另一种方法就是param最终结果: 由图可见我们成功访问到了所输的数值,换一个数字照样行得通 简要实现:packagecom.aurora.param;importorg.springframework.stereotype.......
  • SpringBoot自动配置原理解析
    1:什么是SpringBoot自动配置首先介绍一下什么是SpringBoot,SpringBoost是基于Spring框架开发出来的功能更强大的Java程序开发框架,其最主要的特点是:能使程序开发者快速搭建一套开发环境。SpringBoot能将主流的开发框架(例如SpringMVC,Dubbo,Mybatis,Redis等),做到像Maven导入Jar包一......
  • Spring异步线程池-TaskDecorator传递线程上下文
    TaskDecorator:TaskDecorator是一个执行回调方法的装饰器,主要应用于线程间传递数据,或者提供任务的监控/统计信息。从主线程拷贝数据到子线程,具体数据实际上是封装到threadlocal里面。实现方式:定义一个TaskDecorator,在线程池中设置使用这个TaskDecorator。注意......
  • 重磅!Cloud Ace 在菲律宾开设了第六个亚太地区办事处
    【CloudAce是GoogleCloud全球战略合作伙伴,在亚太地区、欧洲、南北美洲和非洲拥有二十多个办公室。CloudAce在谷歌专业领域认证及专业知识目前排名全球第一位,并连续多次获得GoogleCloud各类奖项。作为谷歌云托管服务商,我们提供谷歌云、谷歌地图、谷歌办公套件、谷歌云认......
  • springboot 06 idea提交到Gitee
         --->commit   首次要填Gitee的账号密码  ......
  • SpringBoot内容协商(Content Negotiation)二 —— 自定义消息转换器(MessageConverter)
    SpringBoot内置的消息转换器SpringBoot没有处理返回yaml格式的数据,这里需要手动添加处理这种返回格式的支持。导入依赖<dependency><groupId>com.fasterxml.jackson.dataformat</groupId><artifactId>jackson-dataformat-yaml</artifactId></dependency>添加配......
  • 【Springboot文件上传】前后端双开,大文件秒传、断点续传的解决方案和优雅实现
    思路和解决方案探讨秒传这里指的“秒传”,是指:当用户选择上传一个文件时,服务端检测该文件之前是否已经被上传过,如果服务器已经存有该文件(完全一样),就立马返回前端“文件已上传成功”。前端随即将进度条更新至100%。这样给用户的感觉就是“秒传”的感觉。对于每一个上传到服务......
  • SpringBoot自动配置原理解析 | 京东物流技术团队
    1:什么是SpringBoot自动配置首先介绍一下什么是SpringBoot,SpringBoost是基于Spring框架开发出来的功能更强大的Java程序开发框架,其最主要的特点是:能使程序开发者快速搭建一套开发环境。SpringBoot能将主流的开发框架(例如SpringMVC,Dubbo,Mybatis,Redis等),做到像Maven导入Jar包一样......
  • springboot解决跨域
    新建config包在建文件复制进去即可importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.web.cors.CorsConfiguration;importorg.springframework.web.cors.UrlBasedCorsConfigura......