• SpringCloud的那些中间件


    前言

    随着互联网的快速发展,微服务的架构思想逐渐成为了一种趋势。SpringCloud作为众多微服务框架中的佼佼者,其提供了丰富的中间件组件,使得我们在微服务项目中能够更加便捷地进行开发和部署。本篇博文将为大家介绍一些SpringCloud中比较重要的中间件及其使用场景。

    一、Eureka

    Eureka是SpringCloud提供的服务注册中心,通过Eureka可以轻松地实现服务注册和发现。在一个分布式系统中,服务注册和服务发现是非常重要的一环。Eureka通过心跳检测和成员列表维护,实现了服务的自我保护机制,并且在服务存在问题时及时移除失效节点,保证了系统的高可用性。

    使用Eureka非常简单,只需要在项目中引入相应的依赖,并通过配置文件配置好服务注册地址即可。下面是一个简单的使用示例:

    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
        <version>${spring-cloud.version}version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    spring:
      application:
        name: eureka-server
    server:
      port: 8761
    eureka:
      instance:
        hostname: localhost
      client:
        register-with-eureka: false
        fetch-registry: false
        service-url:
          defaultZone: http://localhost:8761/eureka/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    二、Ribbon

    Ribbon是一款负载均衡组件,它可以让请求在多个服务之间进行负载均衡,从而实现了服务的高可用性和高并发性。Ribbon提供了多种负载均衡算法,如轮询和随机等,可以根据实际需求选择不同的算法。

    在SpringCloud中使用Ribbon也非常简单,只需要在项目中引入相应的依赖,并且通过配置文件配置好服务列表即可。下面是一个简单的使用示例:

    <dependency>
      <groupId>org.springframework.cloudgroupId>
      <artifactId>spring-cloud-starter-netflix-ribbonartifactId>
      <version>${spring-cloud.version}version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/
    payment-service:
      ribbon:
        listOfServers: localhost:8001,localhost:8002,localhost:8003
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    三、Feign

    Feign是一款声明式的HTTP客户端,它支持HTTP请求和响应的对象绑定,提供了更加简单和易用的方式来调用REST服务。在SpringCloud中,Feign结合了Ribbon和Hystrix,使得我们可以更加方便地实现服务接口调用和容错处理。

    使用Feign同样非常简单,只需要在项目中引入相应的依赖以及开启Feign功能即可。下面是一个简单的使用示例:

    <dependency>
      <groupId>org.springframework.cloudgroupId>
      <artifactId>spring-cloud-starter-openfeignartifactId>
      <version>${spring-cloud.version}version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/
    feign:
      client:
        config:
          default:
            connectTimeout: 5000
            readTimeout: 5000
      hystrix:
        enabled: true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    @FeignClient(value = "payment-service")
    public interface PaymentService {
    
        @GetMapping("/payment/{id}")
        String getPayment(@PathVariable("id") Long id);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    四、Hystrix

    Hystrix是一款熔断器组件,它能够快速、可靠地处理分布式系统中出现的延迟和故障。Hystrix通过断路器模式,当某个服务出现故障时,自动切换到备用服务,避免服务出现瓶颈,从而保证系统的稳定性。

    在SpringCloud中使用Hystrix同样非常简单,只需要在项目中引入相应的依赖,并且通过注解配置好断路器相关的信息和业务逻辑即可。下面是一个简单的使用示例:

    <dependency>
      <groupId>org.springframework.cloudgroupId>
      <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
      <version>${spring-cloud.version}version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    hystrix:
      command:
        default:
          execution:
            isolation:
              strategy: SEMAPHORE
              semaphore:
                maxConcurrentRequests: 50
          circuitBreaker:
            requestVolumeThreshold: 20
            sleepWindowInMilliseconds: 5000
            errorThresholdPercentage: 50
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    @Service
    public class PaymentServiceImpl implements PaymentService {
    
        @Override
        @HystrixCommand(fallbackMethod = "fallbackPayment")
        public String getPayment(Long id) {
            // ......
        }
    
        public String fallbackPayment(Long id) {
            return "Payment service is unavailable.";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    五、Zuul

    Zuul是一款网关组件,它可以通过路由和过滤来将请求转发到不同的微服务中。Zuul提供了丰富的路由规则和过滤器,可以实现请求的控制和转发、请求的监控和跟踪等功能。

    在SpringCloud中使用Zuul同样非常简单,只需要在项目中引入相应的依赖,并通过配置文件配置好网关相关的信息即可。下面是一个简单的使用示例:

    <dependency>
      <groupId>org.springframework.cloudgroupId>
      <artifactId>spring-cloud-starter-netflix-zuulartifactId>
      <version>${spring-cloud.version}version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/
    zuul:
      routes:
        user-service:
          path: /user/**
          serviceId: user-service
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在上面的配置中,我们将所有请求路径以/user/**开头的请求都转发到了user-service微服务中。

    结语

    本文只介绍了一些比较重要的SpringCloud中间件组件,还有许多其他的中间件组件,如Config、Bus等,读者可以进一步探索。通过SpringCloud提供的这些中间件组件,我们可以更加便捷地开发出高可用、高性能、高并发的微服务系统。

    以上就是本篇博文的内容,希望对读者有所帮助。如果您对该主题还有任何疑问或者建议,欢迎在评论区留言。

  • 相关阅读:
    请你说说Spring
    h5修改钉钉双标题栏问题
    敏捷也许是最适合外包团队的项目管理方法
    安装Docker报错求解答~~
    还在玩传统终端,不妨来试试全新 AI 终端 Warp
    什么时候不要采用微服务架构
    【服务器存储数据恢复】华为OceanStor某型号存储raid5数据恢复案例
    windows系统利用powershell查看系统支持那些Windows功能选项
    基于NodeJs+Express+Mysql学生社团活动管理系统
    Java基础篇:对象拷贝:clone方法 以及 序列化
  • 原文地址:https://blog.csdn.net/weixin_65950231/article/details/130903669