SpringCloud服务消费者:restTemplate和feignClient

1、概述

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

2、两者的区别

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

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

3、使用示例

3.1、restTemplate

添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>

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

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class DemoApplication {

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

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

在要使用的地方引入

    @Autowired
    private RestTemplate restTemplate;

get请求:

// 不带参数
public String getDemo() {
    ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://demo-service/hello?name={name}", String.class);
    String body = responseEntity.getBody();
    System.out.println(body);
}

// 带参数
public String getDemo() {
    Map<String, String> params = new HashMap<>(16);
    params.put("name", "Tony");
    ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://demo-service/hello?name={name}", String.class, params);
    String body = responseEntity.getBody();
    System.out.println(body);
}

post请求

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

put请求

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

delete请求

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

3.2、feignClient

1、pom文件添加依赖

<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-openfeign</artifactId>
 </dependency>

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

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class DemoApplication {

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

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

// value指定服务名   fallback指定失败回调类
@FeignClient(value = "demo-service", fallback = DemoServiceFallback.class)
public interface DemoService {
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    // 需要注意的是,@RequestParam注解的value是必须的
    String sayHello(@RequestParam(value = "name") String name);
}


// fallback
@Component
public class DemoServiceFallback implements DemoService {
    @Override
    public String sayHello(String name){
        return "feign failed!";
    }
}

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方式请求是一定支持的。



原文链接:https://www.jianshu.com/p/17a10c8b32cb

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值