Ribbon简单入门

一.Ribbon简介
说起Ribbon,首先要说下负载均衡。将负载分摊到多个操作单元上进行执行就是负载均衡,简单的理解,就是将多个请求按照某种策略均匀的分配给不同的服务器处理,由此解决单台服务器的压力,减少响应速度。可以想象成以前是十件事一个人做,现在是3个人一起做这十件事,自然处理速度和压力都小的多。而Ribbon就是这样一个实现负载均衡的工具,它是通过代码层面来实现的负载均衡。与之相对的另一种方式就是通过一个独立的进程来分配请求,实现负载均衡,Nngix就是典型例子。

二.通过RestTemplate+Ribbon简单实现负载均衡

  1. 创建一个eureka-server服务,application.yml配置文件如下:
 server:
  #端口号
  port: 8761
eureka:
  instance:
    #设置主机ip以及是否提交ip信息
    prefer-ip-address: true
    hostname: localhost
    service-url:
       #服务注册地址
       defaultZone: http://${eureka.instance.hostname}:8761/eureka/
  client:
    register-with-eureka: false
    fetch-registry: false
  1. 创建一个eureka-client客户端,提供一个"/hellworld/{name}"接口,如下:
 package com.springcloud.eurekaclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/helloWorld")
public class HelloRibbonController {

    @Value("${server.port}")
    private String port;

    @GetMapping("/{name}")
    public String helloRibbon(@PathVariable("name") String name) {
        return "hello    " + name + ",this eureka port is  " + port;
    }
}

eureka-client客户端的application.yml配置文件如下:

server:
  #端口号
  port: 8763 #8762
eureka:
  client:
    service-url:
     #服务注册地址,这里填的是之前在Eureka Server配置的服务注册地址,只要有Eureka Client上线,就会向这个地址注册
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
   #服务名
    name: eureka-client

3.创建一个ribbon-client客户端,它会自动从eureka-client里获取服务注册列表,这样访问接口时才能在不同服务间轮询调用,application.yml配置文件如下:

spring:
  application:
    name: ribbon-client
server:
  port: 8764
eureka:
  client:
    service-url:
      defaultZone: http://localhosonfigt:8761/eureka/

然后新增一个RibbonConfig文件,注入RestTemplate 同时通过@LoadBalanced注解开启负载均衡:

package com.example.ribbonclient;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;


@Configuration
public class RibbonConfig {

    @Bean
    @LoadBalanced
    RestTemplate getRestTemlate() {
        return new RestTemplate();
    }
}

新增一个RibbonService文件:

package com.example.ribbonclient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;


@Service
public class RibbonService {
    @Autowired
    private RestTemplate restTemplate;

    public String helloRibbon(String name) {
        return restTemplate.getForObject("http://eureka-client/helloWorld/" + name, String.class);
    }
}

新增一个RibbonController文件:

package com.example.ribbonclient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/ribbon")
@RestController
public class RibbonController {

    @Autowired
    private RibbonService ribbonService;

    @GetMapping("/{name}")
    public String helloRibbon(@PathVariable("name") String name) {
        return ribbonService.helloRibbon(name);
    }
}
  1. 把3个服务分别启动,其中eureka-client分别用端口8762和8763多实例启动,这样就有两个eureka-client客户端提供服务,通过ribbon-client访问"/hellworld/{name}"接口时,能够轮询的调用服务,如下;
    在这里插入图片描述
    在这里插入图片描述
    这样,简单的负载均衡就实现了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值