Spring Cloud(Greenwich.SR1) - 服务负载均衡ribbon

前言

Spring Cloud的服务调用使用RESTFUL接口,Spring Cloud就是基于HTTP的restTemplate调用的,ribbon就是一个负载均衡,ribbon本意是轮训,意味着Spring Cloud的默认负载均衡是轮训策略。

1. ribbon service provider

要负载均衡需要服务提供方至少2个或以上的provider,所以我们要建2个提供方,建立ribbon-service的Spring Boot服务,并注册给eureka server。负载均衡是通过注册中心实现的,体现了中心化的思想。pom如下:

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <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>
    </dependencies>

main方法,@EnableDiscoveryClient

package com.spring.cloud.ribbon.service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

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

application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8082/eureka/
server:
  port: 8202
spring:
  profiles: r1
  application:
    name: ribbon-service
my:
  mark: ---------ribbon 1111111111111111----------

---

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8082/eureka/
server:
  port: 8203
spring:
  profiles: r2
  application:
    name: ribbon-service
my:
  mark: ---------ribbon 2222222222222222----------

新建controller

package com.spring.cloud.ribbon.service.controller;

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

import java.util.HashMap;
import java.util.Map;

@RestController
public class RibbonServiceController {

    @Value("${my.mark}")
    private String mark;

    @RequestMapping(value = "/mark", method = RequestMethod.GET)
    public Map<String, String> getRobbinService(){
        Map<String, String> map = new HashMap<>();
        map.put("myMark", mark);
        return map;
    }
}

使用JVM参数-Dspring.profiles.active=r2,-Dspring.profiles.active=r1分别启动

访问我们上一章的注册中心:

服务已经注册成功,注册application栏,这就是负载均衡的精髓。

2. 建一个ribbon的消费端

建一个模拟消费,创建方法跟上面类似,pom依赖相同。

application.yml文件 

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8102/eureka/
server:
  port: 8205
spring:
  application:
    name: ribbon-consumer

新建restTemplate,使用@LoadBalanced注解,此处可注明负载均衡模式,默认ribbon模式。

package com.spring.cloud.ribbon.consumer.config;

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 RestTemplateConfig {
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

新建service bean

package com.spring.cloud.ribbon.consumer.service;

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

import java.util.Map;

@Service
public class ConsumerService {

    @Autowired
    private RestTemplate restTemplate;

    public Map consumerMark() {
        return restTemplate.getForObject("http://RIBBON-SERVICE/mark",Map.class);
    }
}

注意:服务调用在Spring Cloud中,使用application name表示,比如示例:RIBBON-SERVICE

新建controller,测试

package com.spring.cloud.ribbon.consumer.controller;

import com.spring.cloud.ribbon.consumer.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class ConsumerController {

    @Autowired
    private ConsumerService consumerService;

    @RequestMapping(value = "/consumer", method = RequestMethod.GET)
    public Map consumerService(){
        return consumerService.consumerMark();
    }
}

启动main方法,去eureka server查看,服务均已注册。

访问localhost:8205/consumer/,可以看到服务调用的轮训机制

总结

ribbon的负载均衡是实现的策略模式,可以根据用户配置选择不同的负载均衡算法。服务在发起调用的时候,会去eureka server拿application name对应的ip 端口 应用名等信息的列表,然后缓存下来,根据负载均衡算法,实现负载均衡。 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值