SpringCloud(三):服务消费以及负载均衡(RestTemplate+Ribbon) - Go语言中文社区

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


一、什么是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://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消费方式。
————————————————

 

版权声明:本文来源博客园,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://www.cnblogs.com/2019lgg/p/11758793.html
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2019-11-16 22:49:29
  • 阅读 ( 1442 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢