SpringCloud服务消费者:restTemplate和feignClient

快来打我* 2023-03-13 12:28 75阅读 0赞

1、概述

在springCloud微服务架构下,各个业务会被拆分为独立的微服务。那么我们如何解决服务间调用的问题,springCloud默认提供了两种方式:restTemplate和feignClient

2、两者的区别

restTemplate:使用起来较为麻烦,需要自己指定ribbon的负载均衡,但参数较灵活,请求的路径可以使用程序灵活控制。

feignClient:手机简单,默认集成了ribbon负载均衡,无需自己配置,但参数不灵活,适用于api固定的接口。

3、使用示例

3.1、restTemplate

添加依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
  4. </dependency>

向容器中注入一个restTemplate实例,使用@LoadBalanced开启负载均衡

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. @EnableDiscoveryClient
  4. public class DemoApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run( ServiceRibbonApplication.class, args );
  7. }
  8. @Bean
  9. @LoadBalanced
  10. RestTemplate restTemplate() {
  11. return new RestTemplate();
  12. }
  13. }

在要使用的地方引入

  1. @Autowired
  2. private RestTemplate restTemplate;

get请求:

  1. // 不带参数
  2. public String getDemo() {
  3. ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://demo-service/hello?name={name}", String.class);
  4. String body = responseEntity.getBody();
  5. System.out.println(body);
  6. }
  7. // 带参数
  8. public String getDemo() {
  9. Map<String, String> params = new HashMap<>(16);
  10. params.put("name", "Tony");
  11. ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://demo-service/hello?name={name}", String.class, params);
  12. String body = responseEntity.getBody();
  13. System.out.println(body);
  14. }

post请求

  1. // 带参数
  2. public String postDemo() {
  3. Map<String, String> params = new HashMap<>(16);
  4. params.put("name", "Tony");
  5. ResponseEntity<String> responseEntity = restTemplate.postForObject("http://demo-service/hello", params, String.class);
  6. String body = responseEntity.getBody();
  7. System.out.println(body);
  8. }

put请求

  1. public String putDemo() {
  2. restTemplate.put("http://demo-service/hello/{1}", "Tony");
  3. }

delete请求

  1. // 带参数
  2. public String deleteDemo() {
  3. restTemplate.delete("http://demo-service/hello/{1}", "Tony");
  4. }

3.2、feignClient

1、pom文件添加依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-openfeign</artifactId>
  4. </dependency>

2、在springBoot启动类上添加注解@EnableFeignClients开启feign功能

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. @EnableDiscoveryClient
  4. @EnableFeignClients
  5. public class DemoApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run( ServiceFeignApplication.class, args );
  8. }
  9. }

3、使用时,定义feign的接口

  1. // value指定服务名 fallback指定失败回调类
  2. @FeignClient(value = "demo-service", fallback = DemoServiceFallback.class)
  3. public interface DemoService {
  4. @RequestMapping(value = "/hello",method = RequestMethod.GET)
  5. // 需要注意的是,@RequestParam注解的value是必须的
  6. String sayHello(@RequestParam(value = "name") String name);
  7. }
  8. // fallback
  9. @Component
  10. public class DemoServiceFallback implements DemoService {
  11. @Override
  12. public String sayHello(String name){
  13. return "feign failed!";
  14. }
  15. }

feign的请求方式

feign消费服务时,以GET方式请求的条件:
如果想让服务消费者采用GET方式调用服务提供者,那么需要:
1.服务消费者这边feign调用时,在所有参数前加上@RequestParam注解。
2.服务消费者这边feign调用时,指明为GET方式(注:如果不指明method,那么在条件1满足的情况下,采用的是默认的GET方式)。
注:这里条件1和条件2,是“且”的关系(都满足时,才为GET)。

feign消费服务时,以POST方式请求的条件:
如果想让服务消费者采用POST方式调用服务提供者,那么只需要:
1.服务消费者这边feign调用时,在所有参数前加上@RequestParam注解,并指明feign消费服务的方式为POST。
2.服务消费者这边feign调用时,有且只有一个参数前为@RequestBody或什么也没有(如果有多个参数,那么其余参数前必须有@RequestParam)。
注:这里条件1和条件2,是“或”的关系(当至少一个满足时,即为POST)。
注:在服务消费者中,使用feign消费服务时,如果参数前什么也不写,那么默认是由@RequestBody指明的。
即:只要不满足GET方式请求,那么POST方式请求是一定支持的。

作者:0慕念0
链接:https://www.jianshu.com/p/17a10c8b32cb
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

发表评论

表情:
评论列表 (有 0 条评论,75人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Springcloud03服务消费者

        服务提供者和消费者在注册中心中并没有绝对的界限,上一章中提到服务的提供者其实在项目的应用中,很多情况都会涉及的服务间的相互调用。在一般的微服务架构中服务与服务之间的通讯