客户端负载均衡器
在SpringCloud中Ribbon负载均衡客户端,会从eureka注册中心服务器端上获取服务注册信息列表,缓存到本地。
让后在本地实现轮训负载均衡策略。
Ribbon与Nginx区别
1.服务器端负载均衡Nginx
nginx是客户端所有请求统一交给nginx,由nginx进行实现负载均衡请求转发,属于服务器端负载均衡。
既请求有nginx服务器端进行转发。
2.客户端负载均衡Ribbon
Ribbon是从eureka注册中心服务器端上获取服务注册信息列表,缓存到本地,让后在本地实现轮训负载均衡策略。
既在客户端实现负载均衡。
应用场景的区别:
Nginx适合于服务器端实现负载均衡 比如Tomcat ,Ribbon适合与在微服务中RPC远程调用实现本地服务负载均衡,比如Dubbo、SpringCloud中都是采用本地负载均衡。
Ribbon是Spring Cloud (本地)客户端负载均衡器
Ribbon底层实现:
Member:
pom:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | < 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | ###服务启动端口号 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | ###服务启动端口号 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | < 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | < 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | ###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);
}
}
启动访问: