SpringCloud(三):服务消费以及负载均衡(RestTemplate+Ribbon)

一、什么是Ribbon

Ribbon是Netflix公司开源的一个负载均衡的项目,是一个客户端负载均衡器,运行在客户端上。它是一个经过了云端测试的IPC库,可以很好地控制HTTP和TCP客户端的一些行为。Feign已经默认使用了Ribbon。

  • 负载均衡
  • 容错
  • 多协议(HTTP,TCP,UDP)支持异步和反应模型
  • 缓存和批处理
二、Eureka服务提供者集群

先启动上篇文章中的注册中心eureka-server:8001, 以及hello-service:8011,接着修改hello-service项目配置文件中的端口,改成8012,再次运行用户服务项目。

如果是IDEA用户,需要修改Application.java的执行方式,默认是单实例运行,所以你在运行8011的项目,修改端口无法再次运行。

这里写图片描述

执行成功,查看Eureka注册中心,可以看到有用户服务应用有两个端口对应。

这里写图片描述

三、RestTemplate+Ribbon消费者

这里写图片描述

pom.xml

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

application.properties

spring.application.name=hello-consumer-ribbon
server.port=8021

eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/

启动类

package cn.saytime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class HelloConsumerRibbonApplication {

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

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

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("hello")
    public ResponseEntity<String> hello (String name) {
        return restTemplate.getForEntity("http://HELLO-SERVICE/hello?name=" + name, String.class);
    }

}

http://HELLO-SERVICE/hello 这里的HELLO-SERVICE为注册到Eureka注册中心的服务名

四、测试
  1. 测试服务消费

http://localhost:8021/hello?name=ribbon

hello, ribbon
  1. 测试负载均衡

我们在hello-service hello方法中加上日志打印,然后再分别启动 hello-service:8012,8002,然后多次访问 http://localhost:8021/hello?name=ribbon,可以看到两个hello-service项目的控制台都有日志输出,表示实现了负载均衡。

使用RestTemplate+Ribbon必须指定调用服务名称,如上面的HELLO-SERVICE,为了方便使用,SpringCloud还集成了Feign消费方式。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值