SpringCloud学习心得(二) ribbon

在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的。Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign

ribbon:Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起。Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,就是在配置文件中列出Load Balancer(简称LB)后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随即连接等)去连接这些机器。我们也很容易使用Ribbon实现自定义的负载均衡算法。

目前主流的LB方案可分成两类:一种是集中式LB, 即在服务的消费方和提供方之间使用独立的LB设施(可以是硬件,如F5, 也可以是软件,如nginx), 由该设施负责把访问请求通过某种策略转发至服务的提供方;另一种是进程内LB,将LB逻辑集成到消费方,消费方从服务注册中心获知有哪些地址可用,然后自己再从这些地址中选择出一个合适的服务器。Ribbon就属于后者,它只是一个类库,集成于消费方进程,消费方通过它来获取到服务提供方的地址。

策略介绍

   1:简单轮询负载均衡(RoundRobin)

     以轮询的方式依次将请求调度不同的服务器,即每次调度执行i = (i + 1) mod n,并选出第i台服务器。

   2:随机负载均衡 (Random)

     随机选择状态为UP的Server

   3:加权响应时间负载均衡 (WeightedResponseTime)

     根据相应时间分配一个weight,相应时间越长,weight越小,被选中的可能性越低。

   4:区域感知轮询负载均衡(ZoneAvoidanceRule)

     复合判断server所在区域的性能和server的可用性选择server

好,说了这么多直接来代码

本节承接上一篇,请大家启动eureka-server 和 eureka-server-client,同时更换eureka-server-client的配置文件中的端口号为8763,如下,再启动一个client

server:

  port: 8762

此时在eureka-server的工作台中可以看到启动了两个client



新建一个maven工程

pom文件如下:

增加一个依赖

 <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-ribbon</artifactId>
 </dependency>

配置文件:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8764
spring:
  application:
    name: service-ribbon-eric

启动类:在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean: restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。

package cn.eric.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class ServiceRibbonApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceRibbonApplication.class, args);
    }

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

服务类:请求的路径是http://EUREKA-SERVER-CLIENT1,不写端口号是为了负载均衡

package cn.eric.springcloud.service;

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

@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;
    
    public String sayHi(String name){
        return restTemplate.getForObject("http://EUREKA-SERVER-CLIENT1/hi?name="+name, String.class);
    }
}

对外接口类:

package cn.eric.springcloud.controller;

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

import cn.eric.springcloud.service.HelloService;

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;
    
    @RequestMapping(value = "/hi")
    public String sayHi(@RequestParam String name){
        
        return helloService.sayHi(name);

    }

}

启动ribbon项目,在浏览器里面输入http://localhost:8764/hi?name=eric

刷新还会出现


猜你喜欢

转载自blog.csdn.net/money9sun/article/details/80431647