欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringCloud之Feign示例详解

程序员文章站 2023-12-12 13:54:52
feign简介 feign 是一个声明web服务客户端,这便得编写web服务客户端更容易,使用feign 创建一个接口并对它进行注解,它具有可插拔的注解支持包括feign...

feign简介

feign 是一个声明web服务客户端,这便得编写web服务客户端更容易,使用feign 创建一个接口并对它进行注解,它具有可插拔的注解支持包括feign注解与jax-rs注解,feign还支持可插拔的编码器与解码器,spring cloud 增加了对 spring mvc的注解,spring web 默认使用了httpmessageconverters, spring cloud 集成 ribbon 和 eureka 提供的负载均衡的http客户端 feign.

声明式rest客户端:feign

先要启动eureka_register_service工程(注册中心)和biz-service-0工程(服务生产者)

创建一个maven工程eureka_feign_client

pom.xml

<parent>

<groupid>org.springframework.boot</groupid>

<artifactid>spring-boot-starter-parent</artifactid>

<version>1.4.3.release</version>

<relativepath/> <!-- lookup parent from repository -->

</parent>

<properties>

<project.build.sourceencoding>utf-8</project.build.sourceencoding>

<java.version>1.8</java.version>

</properties>

<dependencies>

  <dependency>

    <groupid>org.springframework.cloud</groupid>

    <artifactid>spring-cloud-starter-feign</artifactid>

  </dependency>

  <dependency>

    <groupid>org.springframework.cloud</groupid>

    <artifactid>spring-cloud-starter-eureka</artifactid>

  </dependency>

  <dependency>

    <groupid>org.springframework.boot</groupid>

    <artifactid>spring-boot-starter-web</artifactid>

  </dependency>

  <dependency>

    <groupid>org.springframework.boot</groupid>

    <artifactid>spring-boot-starter-test</artifactid>

    <scope>test</scope>

  </dependency>

</dependencies>

<dependencymanagement>

  <dependencies>

    <dependency>

  <groupid>org.springframework.cloud</groupid>

  <artifactid>spring-cloud-dependencies</artifactid>

  <version>brixton.sr5</version>

  <type>pom</type>

  <scope>import</scope>

</dependency>

  </dependencies>

</dependencymanagement> 

在应用主类中通过@enablefeignclients注解开启feign功能

启动文件feignapplication.java

@springbootapplication

@enablediscoveryclient

@enablefeignclients

public class feignapplication {

  public static void main(string[] args) {

    springapplication.run(feignapplication.class, args);

  }
}

定义服务接口类userclient.java

使用@feignclient("biz-service-0")注解来绑定该接口对应biz-service-0服务

@feignclient("biz-service-0")

public interface userclient { 

  @requestmapping(method = requestmethod.get, value = "/getuser")

  public user getuserinfo();   

  @requestmapping(method = requestmethod.get, value = "/getuser")

  public string getuserinfostr();   

  @requestmapping(method = requestmethod.get, value = "/info")

  public string info();
} 

在web层中调用上面定义的usercontroller,具体如下

@restcontroller

public class usercontroller {

  @autowired

  userclient userclient;

  @requestmapping(value = "/getuserinfo", method = requestmethod.get)

  public user getuserinfo() {

    return userclient.getuserinfo();

  }

  @requestmapping(value = "/getuserinfostr", method = requestmethod.get)

  public string getuserinfostr() {

    return userclient.getuserinfostr();
  }

  @requestmapping(value = "/info", method = requestmethod.get)

  public string info() {

    return userclient.info();

  }
} 

application.properties配置变量

spring.application.name=feign-consumer

server.port=8004

eureka.client.serviceurl.defaultzone=http://localhost:8000/eureka/ 

访问 http://127.0.0.1:8004/getuserinfo

总结:

其实通过feign封装了http调用服务方法,使得客户端像调用本地方法那样直接调用方法,类似dubbo中暴露远程服务的方式,区别在于dubbo是基于私有二进制协议,而feign本质上还是个http客户端

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

上一篇:

下一篇: