前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【微服务~远程调用】整合RestTemplate、WebClient、Feign

【微服务~远程调用】整合RestTemplate、WebClient、Feign

作者头像
陶然同学
发布2023-02-27 11:28:17
9520
发布2023-02-27 11:28:17
举报
文章被收录于专栏:陶然同学博客

🔎这里是【微服务~远程调用】,关注我学习微服务不迷路 👍如果对你有帮助,给博主一个免费的点赞以示鼓励 欢迎各位🔎点赞👍评论收藏⭐️

👀专栏介绍

【微服务~远程调用】 目前主要更新微服务,一起学习一起进步。

👀本期介绍

本期主要介绍远程调用整合整合RestTemplate、WebClient、Feign

文章目录

整合RestTemplate

整合WebClient

WebClient和RestTemplate

响应式IO模型

WebClient入门

API详解

整合Feign

概述

整合Feign

整合RestTemplate

对RestTemplate进行增强,支持负载均衡

代码语言:javascript
复制
package com.czxy.nacos.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;


@Component
public class RestTemplateConfig {
    @LoadBalanced   //负载均衡
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

通过服务名调用服务提供者

代码语言:javascript
复制
package com.czxy.nacos.controller;

import com.czxy.nacos.feign.TestFeign;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;


@RestController
public class TestController {
    @Resource
    private RestTemplate restTemplate;

    @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
    public String echo(@PathVariable String str) {
        return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
    }
}

整合WebClient

WebClient和RestTemplate

  • RestTemplate 是 spring 3.0 引入的,底层IO模型是阻塞IO模型 Http客户端。
  • WebClient 是 spring 5.0 引入的,作为非阻塞式Reactive Http客户端,用于取代RestTemplate。

响应式IO模型

  • SpringMVC或Struct等框架都是基于Servlet的,其底层IO模型是阻塞IO模型。
  • Spring社区为了解决SpringMVC的阻塞模型在高并发场景下的性能瓶颈,推出了Spring WebFlux,WebFlux底层实现是久经考验的Netty非阻塞IO通信框架。
  • 其实WebClient处理单个HTTP请求的响应时长并不比RestTemplate更快,但是它处理==并发==的能力更强。 所以响应式非阻塞IO模型的核心意义在于,提高了单位时间内有限资源下的服务请求的并发处理能力,而不是缩短了单个服务请求的响应时长。
  • 总结:WebClient --> Spring WebFlux --> Netty

WebClient入门

添加 webflux 依赖

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

编写配置类

代码语言:javascript
复制
package com.czxy.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;


@Component
public class WebClientConfig {

    @Bean
    @LoadBalanced
    public WebClient.Builder webClientBuilder() {
        return WebClient.builder();
    }
}

 编写测试类

代码语言:javascript
复制
package com.czxy.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 org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;


@RestController
@RequestMapping("/client")
public class TestClientController {

    @Resource
    private WebClient.Builder webClientBuilder;

    @GetMapping("/echo/{str}")
    public Mono<String> echo(@PathVariable("str") String str) {
        return webClientBuilder.build()     // 创建WebClient实例
                .get()                      // 请求方式
                .uri("http://service-provider/echo/{1}", str) // 请求url
                .retrieve()                 // 获取响应结果
                .bodyToMono(String.class);  // 将结果转换为指定类型
    }

}

 测试

http://localhost:8071/client/echo/123

API详解

请求方式

方法

描述

等效

build().get()

get请求

build().method(HttpMethod.GET)

build().post()

post请求

build().method(HttpMethod.POST)

build().put()

put请求

build().method(HttpMethod.PUT)

build().delete()

delete请求

build().method(HttpMethod.DELETE)

响应类型

类型

描述

方法

Mono

包含0个或1个元素

bodyToMono(String.class)

Flux

包含1个或多个元素

.bodyToFlux(String.class)

整合Feign

概述

  • RestTemplate和WebClient都是Spring自己封装的工具
  • Feign 是 Spring Cloud 的成员
  • Spring Cloud Alibaba 支持对Feign的调用

整合Feign

添加坐标

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

编写feign

代码语言:javascript
复制
package com.czxy.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;


// @FeignClient(value = "服务名", path = "controller配置的路径" )
@FeignClient(value = "service-provider")
public interface EchoFeign {

    // 与 nacos-provider-2.1>EchoController声明的方法的完全一致
    @GetMapping("/echo/{string}")
    public String echo(@PathVariable String string);
}

 编写测试类

代码语言:javascript
复制
package com.czxy.controller;

import com.czxy.feign.EchoFeign;
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.annotation.Resource;


@RestController
@RequestMapping("/feign")
public class TestFeignController {
    @Resource
    private EchoFeign echoFeign;

    @GetMapping("/echo/{str}")
    public String echo(@PathVariable String str) {
        return echoFeign.echo(str);
    }
}

 修改启动类

代码语言:javascript
复制
package com.czxy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;


@SpringBootApplication
@EnableDiscoveryClient      //服务发现
@EnableFeignClients         //远程调用
public class TestNacosConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestNacosConsumerApplication.class, args );
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-11-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 👀专栏介绍
  • 👀本期介绍
  • 文章目录
  • 整合RestTemplate
  • 整合WebClient
    • WebClient和RestTemplate
      • 响应式IO模型
        • WebClient入门
          • API详解
          • 整合Feign
            • 概述
              • 整合Feign
              相关产品与服务
              负载均衡
              负载均衡(Cloud Load Balancer,CLB)提供安全快捷的流量分发服务,访问流量经由 CLB 可以自动分配到云中的多台后端服务器上,扩展系统的服务能力并消除单点故障。负载均衡支持亿级连接和千万级并发,可轻松应对大流量访问,满足业务需求。
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档