1. 程式人生 > >Spring Cloud之Ribbon與Nginx區別

Spring Cloud之Ribbon與Nginx區別

客戶端負載均衡器

 

 在SpringCloud中Ribbon負載均衡客戶端,會從eureka註冊中心伺服器端上獲取服務註冊資訊列表,快取到本地。

讓後在本地實現輪訓負載均衡策略。

 

Ribbon與Nginx區別

伺服器端負載均衡Nginx

 nginx是客戶端所有請求統一交給nginx,由nginx進行實現負載均衡請求轉發,屬於伺服器端負載均衡。

 既請求有nginx伺服器端進行轉發。

客戶端負載均衡Ribbon

 Ribbon是從eureka註冊中心伺服器端上獲取服務註冊資訊列表,快取到本地,讓後在本地實現輪訓負載均衡策略。

 既在客戶端實現負載均衡。

 

 應用場景的區別:

Nginx適合於伺服器端實現負載均衡 比如Tomcat ,Ribbon適合與在微服務中RPC遠端呼叫實現本地服務負載均衡,比如Dubbo、SpringCloud中都是採用本地負載均衡。

 

 

Ribbon是Spring Cloud (本地)客戶端負載均衡器

Ribbon底層實現:

 

 

Member:

 

pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.toov5</groupId>
  <artifactId>member</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
	</parent>
	<!-- 管理依賴 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.M7</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<!-- SpringBoot整合Web元件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- SpringBoot整合eureka客戶端 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
	</dependencies>
	<!-- 注意: 這裡必須要新增, 否者各種依賴有問題 -->
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/libs-milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
  
</project>

  yml:

###服務啟動埠號
server:
  port: 8009
###服務名稱(服務註冊到eureka名稱)  
spring:
    application:
        name: app-toov5-member
###服務註冊到eureka地址
eureka:
  client:
    service-url:
    ##當前會員註冊到eureka服務  地址+埠號 
           defaultZone: http://127.0.0.1:8100/eureka
           
###因為該應用為註冊中心,不會註冊自己
    register-with-eureka: true
###是否需要從eureka上獲取註冊資訊
    fetch-registry: true

Controller類

package com.toov5.api.controller;

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

@RestController
public class MemberApiController {
    @Value("${server.port}")
    private String serverPort;
    
    @RequestMapping("/getMember")
  public String getMember() {
        return "會員服務"+serverPort;
  }
}

 

  

啟動類:

package com.toov5.api;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient   //註冊到eureka
public class AppMember {
   
    public static void main(String[] args) {
        SpringApplication.run(AppMember.class, args);
    }
    
}  

 Order

 

 Controlller

package com.toov5.api.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

//純手寫Ribbon本地負載均衡
@RestController
public class ExtRibbonController {
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private RestTemplate restTemplate;
    //定義請求數
    private int reqCount;
    
@RequestMapping("/ribbonMember")    
   public String ribbonMember() {
       //互毆去對應伺服器遠端呼叫地址
      String  instanceUrl =  getInstance()+"/getMember";
      System.out.println("instanceUrl"+instanceUrl);
        //直接使用httpclient遠端呼叫。本次使用rest方式
      String result = restTemplate.getForObject(instanceUrl, String.class);  //底層使用httpclient實現的
       return result;
   }
   private String getInstance() {
       List<ServiceInstance> instances = discoveryClient.getInstances("app-toov5-member");
       if (instances==null || instances.size()==0) {
        return null;
    }
       int instanceSize = instances.size();
       int serviceIndex = reqCount%instanceSize;
       reqCount++;
        
       return instances.get(serviceIndex).getUri().toString();
   }
}

 

 啟動類:

package com.toov5.api.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
public class AppOrder {
  public static void main(String[] args) {
    SpringApplication.run(AppOrder.class, args);
}
  //解決RestTemplate找不到問題  把restTemplate註冊到Spring Boot容器中
  @Bean
//  @LoadBalanced   手寫的 不要去實現本地負載均衡效果了
  RestTemplate restTemplate() {  
      return new RestTemplate();
  }
  
}

yml:

###服務啟動埠號
server:
  port: 8002
###服務名稱(服務註冊到eureka名稱)  
spring:
    application:
        name: app-toov5-order
###服務註冊到eureka地址
eureka:
  client:
    service-url:
           defaultZone: http://127.0.0.1:8100/eureka

           
###因為該應用為註冊中心,不會註冊自己
    register-with-eureka: true
###是否需要從eureka上獲取註冊資訊
    fetch-registry: true

  pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.toov5</groupId>
  <artifactId>order</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
	</parent>
	<!-- 管理依賴 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.M7</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<!-- SpringBoot整合Web元件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- SpringBoot整合eureka客戶端 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

	</dependencies>
	<!-- 注意: 這裡必須要新增, 否者各種依賴有問題 -->
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/libs-milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
</project>

  Eureka

 

 pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.toov5</groupId>
  <artifactId>SpringCloud-eureka-server</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
	</parent>
	<!-- 管理依賴 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.M7</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<!--SpringCloud eureka-server -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>
	</dependencies> 
	<!-- 注意: 這裡必須要新增, 否者各種依賴有問題 -->
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/libs-milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
  
</project>

  yml

###eureka 服務埠號
server:
  port: 8100
###服務註冊名稱
eureka:
  instance:
  ##註冊中心ip地址
    hostname: 127.0.0.1
###客戶端呼叫地址
  client:
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
###因為該應用為註冊中心,不會註冊自己 (叢集設為true)
    register-with-eureka: false
###因為自己為註冊中心 ,不會去在該應用中的檢測服務 
    fetch-registry: false

  

package com.toov5;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer   //開啟註冊中心
@SpringBootApplication 
public class AppEureka {
  
     public static void main(String[] args) {
           SpringApplication.run(AppEureka.class, args);
    }
    
}

啟動訪問: