Spring Cloud Feign简单使用

SpringCloud Feign 微服务调用
本文介绍如何在SpringCloud中使用Feign简化微服务间的调用过程。通过集成Feign,替换原有的RestTemplate,实现更简洁的微服务间通信。

概述


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 客户端,它使得编写 Web 服务客户端变得更加简单。以下是 Spring Cloud Feign使用指南: ### 1. 添加依赖 在 `pom.xml` 中添加 Spring Cloud Feign 的依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` ### 2. 启用 Feign 客户端 在 Spring Boot 应用的主类上添加 `@EnableFeignClients` 注解来启用 Feign 客户端: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableFeignClients public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` ### 3. 创建 Feign 客户端接口 创建一个接口,并使用 `@FeignClient` 注解来指定要调用的服务名: ```java import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; @FeignClient(name = "your-service-name") public interface YourFeignClient { @GetMapping("/your-api-path") String yourApiMethod(); } ``` 在上述代码中,`name` 属性指定了要调用的服务名,接口中的方法使用 Spring MVC 的注解来定义要调用的 API。 ### 4. 使用 Feign 客户端 在需要调用远程服务的地方注入 Feign 客户端接口,并调用其中的方法: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class YourController { @Autowired private YourFeignClient yourFeignClient; @GetMapping("/call-remote-service") public String callRemoteService() { return yourFeignClient.yourApiMethod(); } } ``` ### 5. 与 Eureka 集成 如果使用 Eureka 进行服务发现,需要在 `pom.xml` 中添加 Eureka 客户端依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> ``` 并在 `application.properties` 或 `application.yml` 中配置 Eureka 服务器地址: ```properties eureka.client.service-url.defaultZone=http://localhost:8761/eureka/ ``` 通过上述配置和步骤,Spring Cloud 应用可以通过 Feign 客户端与 Eureka 集成,利用 Eureka 进行服务发现,同时通过 Feign 进行简化的 HTTP 远程调用,当需要调用远程服务时,不需要关心服务的具体地址和负载均衡,Spring Cloud 负责这一切的处理 [^1]。 ### 6. 与 Hystrix 结合使用 Feign 默认集成了 Hystrix,可以通过配置来启用 Hystrix 熔断机制。在 `application.properties` 或 `application.yml` 中添加以下配置: ```properties feign.hystrix.enabled=true ``` 创建一个实现了 Feign 客户端接口的 fallback 类: ```java import org.springframework.stereotype.Component; @Component public class YourFeignClientFallback implements YourFeignClient { @Override public String yourApiMethod() { return "Fallback response"; } } ``` 在 `@FeignClient` 注解中指定 fallback 类: ```java @FeignClient(name = "your-service-name", fallback = YourFeignClientFallback.class) public interface YourFeignClient { @GetMapping("/your-api-path") String yourApiMethod(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值