一.简介
Ribbon的主要作用是进行Eureka中服务调用,微服务注册到Eureka中后,Ribbon通过服务名称即可进行服务调用,屏蔽了多主机微服务的ip地址和端口。
使用Ribbon需要引入spring-cloud-starter-netflix-ribbon依赖,但是spring-cloud-starter-netflix-eureka-client已经添加了ribbon依赖,因此不需要引入。
二.修改服务提供者
1.service-user模块增加UserController.java
package com.vincent.user.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/user")
public class UserController {
@GetMapping("/{id}")
public Object getUser(@PathVariable Integer id, HttpServletRequest request){
Map<Object,Object> rst = new HashMap<>();
rst.put("id",id);
rst.put("port",request.getServerPort());
rst.put("timestamp",System.currentTimeMillis());
return rst;
}
}
三.定义服务消费端
1.建立子模块consumer-user调用service-user
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.配置application.yml
server:
port: 8030
spring:
application:
name: consumer-user
eureka:
client:
service-url:
defaultZone: http://admin:admin@localhost:8010/eureka
3.编写启动类
package com.vincent;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
public class ConsumerApp {
public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class,args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
@LoadBalanced 使RestTemplate具有负载均衡功能,在使用RestTemplate调用服务时虚拟主机名将会被映射为服务的网络地址。
4.消费端TestController.java
package com.vincent.consumer.controller;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class TestController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/getUserById")
public Object getUserById(Integer id){
return this.restTemplate.getForObject("http://service-user/user/{id}",JSONObject.class,id);
}
}
5.启动消费端并访问:http://localhost:8030/getUserById?id=9
四.Ribbon负载均衡
所有的微服务都注册到Eureka注册中心,这样消费端就可以使用Ribbon与Eureka注册中心的服务集群实现负载均衡调用。Ribbon提供的是客户端负载均衡。
1.8021端口也运行service-user实例
java -jar target\service-user-0.0.1-SNAPSHOT.jar --server.port=8021
3.访问消费端:http://localhost:8030/getUserById?id=9,将可以看到端口变化
五.总结
默认情况下Ribbon采用服务列表中的顺序模式实现负载均衡处理。
1.IRule:Ribbon的负载均衡策略接口,默认采用ZoneAvoidanceRule策略(能在多区域环境下选择最佳区域服务访问),继承结构如下:
2.IPing:Ribbon的实例检查策略接口,默认采用NoOpPing 子类实现,即默认所有服务实例都可用
3.ILoadBalancer:负载均衡器,默认采用ZoneAwareLoadBalancer,具备区域感知能力