当前位置: 移动技术网 > IT编程>软件设计>架构 > SpringCloud(三):服务消费以及负载均衡(RestTemplate+Ribbon)

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

2019年10月30日  | 移动技术网IT编程  | 我要评论
一、什么是Ribbon: Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法。 将Netflix的中间层服务连接在一起。Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,就是在配置文件中列出Load Balancer(简称LB)后面所有的机器, ...

一、什么是ribbon:

ribbon是netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法。

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

  • 负载均衡
  • 容错
  • 多协议(http,tcp,udp)支持异步和反应模型
  • 缓存和批处理

二、eureka服务提供者集群

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

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

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

三、resttemplate+ribbon消费者:  新建一个maven服务

 

 

 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/spring.application.name=hello-consumer-ribbon
server.port=8021
eureka.client.serviceurl.defaultzone=http://localhost:8001/eureka/

启动类:

package cn.demo;
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;

    //获取服务实例,作用为之后console显示效果;"http://user-service/hello?name= 标识注册的服务。user-service在eureka注册的服务名
    @requestmapping("hello")
    public responseentity<string> hello (string name) {
        return resttemplate.getforentity("http://user-service/hello?name=" + name, string.class);
    }

}

四、测试
测试服务消费
http://www.lhsxpumps.com/_localhost:8021/hello?name=ribbon

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

使用resttemplate+ribbon必须指定调用服务名称,如上面的hello-service,为了方便使用,springcloud还集成了feign消费方式。
————————————————

 

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网