03.SpringCloud之Ribbon负载均衡组件

一.简介

Ribbon的主要作用是进行Eureka中服务调用,微服务注册到Eureka中后,Ribbon通过服务名称即可进行服务调用,屏蔽了多主机微服务的ip地址和端口。

使用Ribbon需要引入如下依赖:

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

二.定义微服务

1.编写配置文件application.yml

server:
  port: 8001
  context-path: /service-user

spring:
  application:
    name: service-user

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/service-eureka/eureka

2.编写启动类

package com.vincent;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ServiceApp {
    public static void main(String[] args) {
        SpringApplication.run(ServiceApp.class,args);
    }
}

3.建立微服务UserController.java

package com.vincent.controller;


import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
public class UserController {

    @GetMapping("/detail")
    public Object detail(Integer id, HttpServletRequest request){
        JSONObject rst = new JSONObject();
        rst.put("date",new Date());
        rst.put("name",22);
        rst.put("port",request.getLocalPort());
        return rst;
    }

}

三.定义服务消费端

1.引入依赖

 <dependency>
	<groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2.配置application.yml

server:
  port: 9001

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/service-eureka/eureka
    register-with-eureka: false

3.编写启动类

package com.vincent;



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 ConsumerApp {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApp.class,args);
    }

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

4.定义消费端Controller

package com.vincent.controller;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class UserController {
    public static final String SERVICE_USER = "http://SERVICE-USER/service-user";

    @Autowired
    private RestTemplate restTemplate;


    @GetMapping("/detail")
    public Object detail(Integer id){
        return this.restTemplate.getForObject(SERVICE_USER+"/detail?id="+id,String.class);
    }
}

5.启动消费端并访问:http://localhost:9001/detail?id=1
在这里插入图片描述

四.Ribbon负载均衡

所有的微服务都注册到Eureka注册中心,这样消费端就可以使用Ribbon与Eureka注册中心中的服务集群实现负载均衡调用。Ribbon提供的是客户端负载均衡。

1.增加微服务配置文件application-8002.yml

server:
  port: 8002

2.分别启动8001、8002端口微服务
在这里插入图片描述

3.访问消费端:http://localhost:9001/detail?id=1,将可以看到端口变化

五.总结

默认情况下Ribbon采用服务列表中的顺序模式实现负载均衡处理。

1.IRule:Ribbon的负载均衡策略接口,默认采用ZoneAvoidanceRule策略(能在多区域环境下选择最佳区域服务访问),继承结构如下:
在这里插入图片描述

2.IPing:Ribbon的实力检查策略接口,默认采用NoOpPing 子类实现,即默认所有服务实例都可用

3.ILoadBalancer:负载均衡器,默认采用ZoneAwareLoadBalancer,具备区域感知能力

猜你喜欢

转载自blog.csdn.net/Zllvincent/article/details/108423395