1、Ribbon 是什么?
Spring Cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具,它基于Netflix Ribbon实现。通过Spring Cloud的封装,可以让我们轻松地将面向服务的REST模版请求自动转换成客户端负载均衡的服务调用。Spring Cloud Ribbon虽然只是一个工具类框架,它不像服务注册中心、配置中心、API网关那样需要独立部署,但是它几乎存在于每一个Spring Cloud构建的微服务和基础设施中。因为微服务间的调用,API网关的请求转发等内容,实际上都是通过Ribbon来实现的。
2、pom.xml配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
3、application.yml
eureka:
client:
register-with-eureka: false # 不注册
serviceUrl:
defaultZone: http://admin:admin@eurekaserver-7001.com:7001/eureka,http://admin:admin@eurekaserver-7002.com:7002/eureka,http://admin:admin@eurekaserver-7003.com:7003/eureka
4、RestTemplate的bean配置
package com.zemel.consumer.config;
import java.nio.charset.Charset;
import java.util.Base64;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestConfig {
@Bean
public HttpHeaders getHeaders(){
// 进行一个Http头信息配置
HttpHeaders headers = new HttpHeaders();
String auth = "wendy:wendy";
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(Charset.forName("US-ASCII")));
// 加密字符串要有空格
String authHeader = "Basic " + new String(encodedAuth);
headers.set("Authorization", authHeader);
return headers;
}
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
6、Rest调用微服务
package com.zemel.consumer.controller;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.zemel.consumer.constant.ServiceNameConst;
import com.zemel.vo.Dept;
@RestController
@RequestMapping("/consumer/dept")
public class DeptController {
static final String DEPT_GET_URL = ServiceNameConst.DEPT + "dept/get/";
static final String DEPT_LIST_URL = ServiceNameConst.DEPT + "dept/list/";
static final String DEPT_ADD_URL = ServiceNameConst.DEPT + "dept/add/";
@Resource
private RestTemplate restTemplate;
@Resource
private HttpHeaders headers;
@GetMapping("/get")
public Object getDept(long id){
return this.restTemplate.exchange(DEPT_GET_URL+id, HttpMethod.GET, new HttpEntity(headers), Dept.class);
// return this.restTemplate.getForEntity(DEPT_GET_URL+id, Dept.class);
}
@GetMapping("/list")
public Object list(){
return this.restTemplate.exchange(DEPT_LIST_URL, HttpMethod.GET, new HttpEntity(headers), List.class);
// return this.restTemplate.getForObject(DEPT_LIST_URL, List.class);
}
@GetMapping("/add")
public Object add(Dept dept){
return this.restTemplate.exchange(DEPT_ADD_URL, HttpMethod.POST, new HttpEntity(dept,headers), Boolean.class);
// return this.restTemplate.postForObject(DEPT_ADD_URL, dept, Boolean.class);
}
}
package com.zemel.consumer.constant;
public interface ServiceNameConst {
String DEPT = "http://DEPT-8001/";
}
package com.zemel.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableEurekaClient
public class Consumer80App {
public static void main(String[] args) {
SpringApplication.run(Consumer80App.class, args);
}
}
注意:Ribbon使用一定要使用负载均衡注解@LoadBalanced,若未给RestTemplate的bean配置该注解,
则可能发生异常:unkonwnHostException
7、Ribbon的作用
总所周知的作用:微服务调用端通过Ribbon调用微服务;
Ribbon内部通过服务名称查找服务对应的IP及端口,并且负载轮询同一服务名称的不同服务