「Fバージョンを」均衡SpringCloudチュートリアル6-リボン負荷初心者の初心者のために最も適し

SpringCloudバージョン:Finchley.SR2 SpringBootバージョン:2.0.3.RELEASEソースアドレスします。https://gitee.com/bingqilinpeishenme/Java-Tutorials

序文

ヶ月以上ブログを書く、断続的なアップデート、パートナー今日は、私のシェアが有意義であることをとても幸せに更新少しリマインダーを持っています。

それ以来、更新、また、シリーズの途中に「最も適切なエントリSpringCloudチュートリアル」名前の子を変更

以下のようにコード内の以前の記事では、3つのプロジェクト、すなわち、2つのレジストリとクライアントを持っています。

今日は、このコードに基づいて次のようになります。

  1. ユーレカ・クライアント-8803サービス・プロバイダーとして、
  2. IDEAによってユーレカ・クライアント-8803は、8803と8804ポートで起動します、アナログサービスプロバイダクラスタ
  3. そして、サービスコンシューマユーレカ・消費者8805を作成します
  4. サービスコールと負荷分散コールサービスを通じてサービスプロバイダへの消費者を許可します。

ヒント:の使用を知るために必要RestTemplate SpringBoot、17-ガイドを使用し始めるためにグラフィックチュートリアルでは、「」リクエストヘッダの設定「のポストを取得します」RestTemplateます

サービスコンシューマを作成し、実行しているサービスプロバイダクラスタ

サービスプロバイダは、コントローラインタフェースを書きます

クラスTestControllerに-8803ユーレカ・クライアントでのサービスプロバイダを書きます

package com.lby.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
* @author luxiaoyang
* @create 2020-04-02-20:13
*/
@RestController
public class TestController {

/**
* @Value("${server.port}") 读取application配置文件中的值
* 赋值到成员变量上
*
* ${server.port} 参数为 application配置文件中的key
*/
@Value("${server.port}")
private String port;

/**
* @RequestParam 获取Request参数的 用于RestFul风格中
* @PathVariable 获取路径中的参数
*/
@GetMapping("showImgById")
public String showImgById(@RequestParam("id") Integer id){
return "查询到id为:"+id+"的信息,使用端口号为:"+port;
}

}

复制代码

サービスプロバイダ「アナログトランキング」を起動するポート番号によって8803と8804でIDEA

IDEAは、プロジェクトが唯一のポート番号に一度起動することができ、単一の場合にはデフォルトで起動プロジェクトです。しかし、実際には、IDEAは、プロジェクトの複数のインスタンスがポート番号を変更することによって、何度も起動することができますサポートしています。

ユーレカ・クライアント-8803には、例えば

1.変更ユーレカ・クライアント-8803は、IDEAを開始するために提供されます

IDEAの異なるバージョンは、また、図1に示した構成に表示されます。

8803 2.プロジェクトを開始するポート番号

3.不要关闭 8803 这个服务,然后直接修改yml中的端口号为8804,再次通过启动类启动

通过以上步骤,就启动了两个服务提供者,用来模拟集群,效果如下

创建服务消费者 eureka-consumer-8805

根据之前教程中的步骤,再创建一个客户端eureka-consumer-8805作为消费者

pom配置

 <dependencies>
<!-- Eureka 客户端的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<!-- web的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- 测试的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
复制代码

application配置文件

server:
port: 8805

#指定当前服务的名称 会注册到注册中心
spring:
application:
name: eureka-consumer-8805

# 指定 服务注册中心的地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8801/eureka,http://localhost:8800/eureka
复制代码

启动类

package com.lby;

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

/**
* @author luxiaoyang
* @create 2020-04-02-20:43
*/
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaConsumer8805 {
public static void main(String[] args) {
SpringApplication.run(EurekaConsumer8805.class,args);
}
}

复制代码

服务调用和负载均衡

Ribbon负载均衡

Ribbon是一个基于HTTP和TCP的客户端负载均衡工具。

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

关于Ribbon的简介,有一个名词需要进行解释,客户端负载均衡? 负载均衡是一种非常常见的技术,例如:Nginx,F5。

对于Nginx来说,在Nginx中存储一份服务端的清单,用户的请求到达Nginx之后,Nginx会根据负载均衡策略从服务清单中选择一台服务器去处理客户端的请求。 服务清单存储在负载均衡服务中,这就是服务端负载均衡。 而客户端负责均衡,例如Ribbon,本身是不存储可用服务清单的,需要服务清单的时候通过服务节点找注册中心获取。

服务消费者 eureka-consumer-8805 中通过RestTemplate+Ribbon调用服务提供者

RestTemplate+Ribbon的配置

1.在服务消费者 eureka-consumer-8805中导入Ribbon的依赖

<!--ribbon-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
复制代码

2.在启动类中写RestTemplate+Ribbon的配置

演示Ribbon负载均衡的效果

1.在消费者中创建接口 通过RestTemplate调用服务提供者

package com.lby.controller;

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

/**
* @author luxiaoyang
* @create 2020-04-02-20:49
*/
@RestController
public class ConsumerController {

@Autowired
private RestTemplate restTemplate;

/**
* 调用服务提供者
*/
@RequestMapping("/consumer/showConsumer")
public String showConsumer(){
/**
* 通过Ribbon 发送服务调用 用的是RestTemplate
* RestTemplate 本身没有负载均衡的能力
*
* 注意:RestTemplate请求地址中写的不是 ip+端口号 而是被调用服务的服务名称
*/
String object = restTemplate.getForObject("http://eureka-client-8803/showImgById?id=1", String.class);

return "查询到服务提供者的数据,"+object;
}
}

复制代码

注意:RestTemplate请求地址中写的不是 ip+端口号 而是被调用服务的服务名称

2.重启所有的服务,两个服务提供者,一个服务消费者

3.访问服务消费者的接口

请求地址:http://localhost:8805/consumer/showConsumer 可以看到每次请求端口号不一样

总结

以上就是RestTemplate+Ribbon的负载均衡的基本使用

  • RestTemplate负责服务调用
  • Ribbon实现负载均衡

源码地址:https://gitee.com/bingqilinpeishenme/Java-Tutorials

恭喜你完成了本章的学习,为你鼓掌!如果本文对你有帮助,请帮忙点赞,评论,转发,这对作者很重要,谢谢。

要掌握SpringCloud更多的用法,请持续关注本系列教程。

求关注,求点赞,求转发

欢迎关注本人公众号:鹿老师的Java笔记,将在长期更新Java技术图文教程和视频教程,Java学习经验,Java面试经验以及Java实战开发经验。

おすすめ

転載: juejin.im/post/5e85e165f265da47c35d7096
おすすめ