springCloud负载均衡Ribbon和Feign的区别

1、什么是负载均衡:

   负载均衡(Load Balance)是分布式系统架构设计中必须考虑的因素之一,它通常是指,将请求/数据【均匀】分摊到多个操作单元上执行,负载均衡的关键在于【均匀】。

2、常见的负载均衡方案:

    1、正向代理:

   正向代理 是一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容返回给客户端。客户端必须要进行一些特别的设置才能使用正向代理。

          比如: 我是一个用户,我访问不了某网站,但是我能访问一个代理服务器,这个代理服务器呢,他能访问那个我不能访问的网站,于是我先连上代理服务器,告诉他我需要那个无法访问网站的内容,代理服务器去取回来,然后返回给我。从网站的角度,只在代理服务器来取内容的时候有一次记录,有时候并不知道是用户的请求,也隐藏了用户的资料,这取决于代理告不告诉网站。

  用途:

    (1)访问原来无法访问的资源,如google(VPN)

    (2) 可以做缓存,加速访问资源

 (3)对客户端访问授权,上网进行认证

 (4)代理可以记录用户访问记录(上网行为管理),对外隐藏用户信息

  2、反代理:

      反向代理(Reverse Proxy)实际运行方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。

    比如:我要访问localhost:8080/xxx/view这个页面,但是xxx对应的服务器并没有view这个资源,它是从另一台服务器上调用这个资源,这样的vies对应的那个服务器就使用了反向代理服务器,具体请求是由服务器统一处理的,用户并不需要知道。

  用途:

(1)保证内网的安全,可以使用反向代理提供WAF功能,阻止web攻击

         大型网站,通常将反向代理作为公网访问地址,Web服务器是内网。

(2)负载均衡,通过反向代理服务器来优化网站的负载

3、spring cloud的 Netflix ribbon 和 feign 的区别:

      Ribbon:Ribbon 是一个基于 HTTP 和 TCP 客户端的负载均衡器它可以在客户端配置 ribbonServerList(服务端列表),然后轮询请求以实现均衡负载它在联合 Eureka 使用时ribbonServerList 会被 DiscoveryEnabledNIWSServerList 重写,扩展成从 Eureka 注册中心获取服务端列表同时它也会用 NIWSDiscoveryPing 来取代 IPing,它将职责委托给 Eureka 来确定服务端是否已经启动。 使用 HttpClient 或 RestTemplate 模拟http请求,步骤相当繁琐。

     用法: 

 RestTemplate注入到容器中,@LoadBalanced注解使用默认负载均衡算法(可以使用自定义)

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class HelloworldApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloworldApplication.class, args);
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

     用 REST_URL_PREFIX 指定请求地址 , 使用 restTemplate 模拟 http 请求。@RestController

public class DeptController_Consumer {
    
//注册中心请求的地址
private static final String REST_URL_PREFIX = "http://MICROSERVICECLOUD-DEPT"; @Autowired private RestTemplate restTemplate; @RequestMapping(value = "/consumer/dept/add") public boolean add(Dept dept) {
return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class); } @RequestMapping(value = "/consumer/dept/get/{id}") public Dept get(@PathVariable("id") Long id) { return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class); } @RequestMapping(value = "/consumer/dept/list") public List<Dept> list() { return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class); } }

     Feign:

Feign 是在 Ribbon的基础上进行了一次改进,是一个使用起来更加方便的 HTTP 客户端。采用接口的方式, 只需要创建一个接口,面向接口;然后在上面添加注解即可 ,将需要调用的其他服务的方法定义成抽象方法即可, 不需要自己构建http请求。然后就像是调用自身工程的方法调用,而感觉不到是调用远程方法,使得编写 客户端变得非常容易。类似于 mybatis 的 @Mapper注解 。

      用法:

      在 api 工程 定义一个接口API,
     a) 添加@FeignClient 注解,指定微服务名称 MICROSERVICECLOUD-DEPT
   
  b) 指定请求地址 @RequestMapping

@FeignClient(value = "MICROSERVICECLOUD-DEPT")
public interface DeptClientService {
    
    @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
    public Dept get(@PathVariable("id") long id);

    @RequestMapping(value = "/dept/list", method = RequestMethod.GET)
    public List<Dept> list();

    @RequestMapping(value = "/dept/add", method = RequestMethod.POST)
    public boolean add(Dept dept);
}

在 客户端的工程 引入 DeptClientService 接口,调用对应的方法。DeptClientService 类似于 mybatis的@Mappper 注解

@RestController
public class DeptController_Consumer {

    @Autowired
    private DeptClientService service = null;

    @RequestMapping(value = "/consumer/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id) {
        return this.service.get(id);
    }

    @RequestMapping(value = "/consumer/dept/list")
    public List<Dept> list() {
        return this.service.list();
    }

    @RequestMapping(value = "/consumer/dept/add")
    public Object add(Dept dept) {
        return this.service.add(dept);
    }
}

application.yml

server:
  port: 80
  
eureka:
  client:
    register-with-eureka: false
    service-url: 
      defaultZone: http://eurekaHostIP1:7001/eureka/,http://eurekaHostIP2:7002/eureka/,http://eurekaHostIP3:7003/eureka/  

猜你喜欢

转载自www.cnblogs.com/xiaofeng-fu/p/12119125.html