Feign简单介绍

1、Feign简介

Feign是一个声明式的Web服务客户端,使用Feign可使得Web服务客户端的写入更加方便。
它具有可插拔注释支持,包括Feign注解和JAX-RS注解、Feign还支持可插拔编码器和解码器、Spring Cloud增加了对Spring MVC注释的支持,并HttpMessageConverters在Spring Web中使用了默认使用的相同方式。Spring Cloud集成了Ribbon和Eureka,在使用Feign时提供负载平衡的http客户端。

2、Feign简单使用

使用Feign.builder封装SDK

Feign提供了Feign.builder()客户端的构造方法,可以轻松的访问远程的URL,不依赖其他服务。这种方式的用法是将远程的URL,封装成纯接口SDK,供其他用户或系统使用。
1.添加mvn依赖,只使用Feign的依赖即可

<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-core</artifactId>
    <version>9.5.0</version>
</dependency>

2.定义FeignClient接口调用RESTFUL接口

public interface FeignClient {
    @RequestLine("GET /user/login")
    public String login(@Param("username") String username, @Param("password") String password);
}

3.封装SDK

public class SDK{
    public static void login(String username,String password){
      FeignClient  feignClient = Feign.builder()
                .target(FeignClient.class, "http://localhost");
      feignClient.login(username,password);
     }
    /** 测试*/
    public static void main(String[] args) {
        login("lsz","lsz");
    }
}

使用SpringCloud及Eureka服务管理

这种方式是需要添加SpringCloud组件的一些依赖及注解,还需要依赖Spring的容器管理。
1.添加依赖

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2.定义UserFeignClient 接口
与SDK不同的是,接口上需要有FeignClient及Component的注解。@Component的含义是将该组件注册到SpringCloud中,接受Spring容器的管理,使用依赖注入得到该接口的实体;@FeignClient注解中可以指定“name”与“url”,如果程序为Eureka客户端,指定name为服务注册中心的名,会去服务注册中心去查找访问的host地址

@Component //springboot中注册该组件
@FeignClient(name="user-server",url = "http://localhost:8011")
public interface UserFeignClient {
    @RequestMapping("/{id}")
    public User findByIdFeign(@RequestParam("id") Long id);
}

3.定义访问UserFeignClient的controller

@Component
public class UserServiceImpl implements UserService {

    @Autowired
    private UserFeignClient userFeignClient;
    @Override
    public User findByIdFeign(String id) {
        User object = this.userFeignClient.findByIdFeign("1111");
        return object;
    }
}

Feign的这两种用法,都是不错的。

3、Feign与Hystrix的关系:

feign默认是集成Hystrix,如果想要解除Hystrix,feign提供了两种方式 :

(1)、是通过application.yml中增加这个:

feign.hystrix.enabled: false ## 索性禁用feign的hystrix支持;这样就全部禁止掉Hystrix

(2)、feign提供了对单个Client的禁止,通过配置类来实现,看下面代码:

package com.itmuch.cloud.feign;

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

import com.itmuch.config.Configuration2;

@FeignClient(name = "xxxx", url = "http://localhost:8761/", configuration = Configuration2.class, fallback = FeignClient2Fallback.class)
public interface FeignClient2 {
  @RequestMapping(value = "/eureka/apps/{serviceName}")
  public String findServiceInfoFromEurekaByServiceName(@PathVariable("serviceName") String serviceName);
}

对应的Configuration2类如下:

package com.itmuch.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import feign.Feign;
import feign.auth.BasicAuthRequestInterceptor;

@Configuration
public class Configuration2 {
  @Bean
  public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
    return new BasicAuthRequestInterceptor("user", "password123");
  }

  @Bean
  @Scope("prototype")
  public Feign.Builder feignBuilder() {
    return Feign.builder();
  }
}

说明:Feign.builder默认的是HystrixFeignBuilder,需要改成Builder,就禁止掉Hystrix

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
你想了解关于 Feign 和 Nacos 的集成吗?Feign 是一个轻量级的 HTTP 客户端框架,而 Nacos 是一个服务发现和配置管理平台。在 Spring Cloud 中,Feign 通常用于声明式的 REST 客户端调用,而 Nacos 则用于服务的注册和发现。 要在项目中使用 Feign 和 Nacos,首先需要在 pom.xml 文件中添加相关依赖。例如: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> ``` 接着,在应用程序主类上添加 `@EnableFeignClients` 和 `@EnableDiscoveryClient` 注解,以启用 Feign 和 Nacos 的集成。例如: ``` @SpringBootApplication @EnableFeignClients @EnableDiscoveryClient public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } } ``` 然后,定义一个 Feign 客户端接口,并使用 `@FeignClient` 注解指定要调用的服务的名称。例如: ``` @FeignClient(name = "service-provider") public interface MyFeignClient { @GetMapping("/hello") String hello(); } ``` 最后,通过调用 Feign 客户端接口中的方法来发起 REST 请求。例如: ``` @RestController public class MyController { @Autowired private MyFeignClient myFeignClient; @GetMapping("/test") public String test() { return myFeignClient.hello(); } } ``` 以上就是关于 Feign 和 Nacos 的集成的简单介绍。希望能够对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

garagong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值