加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Spring Cloud Feign简单使用详解

发布时间:2020-12-14 20:01:29 所属栏目:Java 来源:网络整理
导读:概述 在Spring Cloud EureKa Ribbon 服务注册-发现-调用一文中简单的介绍了在Spring Cloud中如何使用EureKa和Ribbon。文章中使用了RestTemplate去访问其他的restful微服务接口。其实在Spring Cloud还可以使用Feign来访问其他的restful微服务接口。使用起来更

概述

在Spring Cloud EureKa Ribbon 服务注册-发现-调用一文中简单的介绍了在Spring Cloud中如何使用EureKa和Ribbon。文章中使用了RestTemplate去访问其他的restful微服务接口。其实在Spring Cloud还可以使用Feign来访问其他的restful微服务接口。使用起来更加的简洁明了。

集成Feign

修改一下Spring Cloud EureKa Ribbon 服务注册-发现-调用中order service的pom配置,把Fegin引入进来即可。

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

修改OrderApplication类,删除如下代码:

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

并加上@EnableFeignClients注解。完整代码如下:

package com.springboot;

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

@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class OrderApplication {
  public static void main(String[] args) {
    SpringApplication.run(OrderApplication.class,args);
  }
}

新增接口UserService,并使用@FeignClient注解。

package com.springboot;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(name="user")
public interface UserService {
  @GetMapping(value="/getUser")
  String getUser();
}

这里的@FeignClient(name="user")中的name=user表示要访问user这个微服务。由于order这个微服务已经集成了Eureka和Ribbon。那么使用@FeignClient(name="user")访问user微服务的时候,已经自动支持客户端路由了。并且会从注册中心中找到user这个微服务。

修改OrderController,注入UserService。

package com.springboot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {

  @Autowired
  private UserService userService;

  @GetMapping("/getOrderUser")
  public String getOrderUser() {
    return userService.getUser();
  }
}

这样就无需使用

restTemplate.getForEntity("http://user/getUser",String.class).getBody();

来调用user服务中的getUser接口了。而是直接使用userService.getUser()就可以了。

启动注册中心以及user和order这两个微服务。使用http://localhost:8883/getOrderUser

访问一下。是可以返回

I am user list.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

您可能感兴趣的文章:

  • 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务的方法(推荐)
  • 详解spring cloud Feign使用中遇到的问题总结
  • spring cloud feign不支持@RequestBody+ RequestMethod.GET报错的解决方法
  • 详解springcloud Feign的Hystrix支持
  • SpringCloud之Feign示例详解
  • 使用Spring Cloud Feign上传文件的示例
  • spring cloud 之 Feign 使用HTTP请求远程服务的实现方法
  • Spring Cloud中关于Feign的常见问题总结
  • 解决Spring Cloud中Feign/Ribbon第一次请求失败的方法

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读