SpringCloud-Ribbon客户端负载均衡(三)

Ribbon实现负载均衡的原理:客户端从eureka注册中心获取对应的注册信息列表,拿到一个List集合,通过负载均衡算法得到服务器地址进行远程调用
负载均衡算法:实际服务器位置下标=总请求数%服务器集群数

以订单服务调用会员服务举例:
在这里插入图片描述

Ribbon实现

  1. 在消费提供者项目(订单服务)中引入依赖

    		<dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            </dependency>
    
  2. 配置文件

    server.port=8765
    spring.application.name=order-service
    
    eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
    
  3. 实现类

    package com.example.orderservice.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.web.client.RestTemplate;
    
    import java.util.List;
    
    @Service
    public class MermberOrderService {
    
        @Autowired
        private RestTemplate restTemplate;
    
        public List<String> getOrderUserAll(){
        //mermber-service是注册的会员服务名称
           return restTemplate.getForObject("http://mermber-service/getMermberAll",List.class);
    
        }
    }
    
  4. 控制层

    package com.example.orderservice.controller;
    
    import com.example.orderservice.service.MermberOrderService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import java.util.List;
    @RestController
    public class OrderController {
    
        @Autowired
        private MermberOrderService mermberOrderService;
    
        @RequestMapping(value = "/getOrderUserAll",produces= "application/json;charset=UTF-8")
        public List<String> getOrderUserAll(){
            System.out.println("订单服务开始调用会员服务");
            return  mermberOrderService.getOrderUserAll();
        }
    }
    
    
  5. 启动类加上@LoadBalanced即可开启负载均衡

    package com.example.orderservice;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    
    @SpringBootApplication
    @EnableEurekaClient
    public class OrderServiceApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(OrderServiceApplication.class, args);
        }
    
        @Bean
        @LoadBalanced  //支持负载均衡
        RestTemplate restTemplate(){
            return new RestTemplate();
        }
    }
    
  6. 运行项目
    启动多个会员服务,看效果

在这里插入图片描述
在这里插入图片描述
可以看到两次端口号不一样,实现了负载均衡

注意⚠️:
SpringCloud中支持两种客户端调用工具

  1. RestTemplate:这里加@LoadBalanced注解实现负载均衡方式就是rest调用,在实际项目中基本不用
  2. Feign客户端调用工具:Feign是一个声明式的http客户端调用工具,采用接口+注解方式实现,易读性比较强,springcloud就是使用feign,在实际项目中也用得最多,feign是默认开启支持ribbon,所以不用引入ribbon的依赖
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值