客户端负载均衡器
在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:
4.0.0 com.toov5 member 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.0.1.RELEASE org.springframework.cloud spring-cloud-dependencies Finchley.M7 pom import org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client spring-milestones Spring Milestones https://repo.spring.io/libs-milestone false
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;@RestControllerpublic 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 //注册到eurekapublic 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本地负载均衡@RestControllerpublic 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() { Listinstances = 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@EnableEurekaClientpublic 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
4.0.0 com.toov5 order 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.0.1.RELEASE org.springframework.cloud spring-cloud-dependencies Finchley.M7 pom import org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client spring-milestones Spring Milestones https://repo.spring.io/libs-milestone false
Eureka
pom
4.0.0 com.toov5 SpringCloud-eureka-server 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.0.1.RELEASE org.springframework.cloud spring-cloud-dependencies Finchley.M7 pom import org.springframework.cloud spring-cloud-starter-netflix-eureka-server spring-milestones Spring Milestones https://repo.spring.io/libs-milestone false
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); } }
启动访问: