美文网首页
Feign入门

Feign入门

作者: 账号已被注销 | 来源:发表于2019-07-19 11:49 被阅读0次

    1、maven依赖

    需要引入spring-cloud-starter-openfeign
    (根据所使用springclould版本不同,名字可能不同,我这里使用的版本是Greenwich.SR1)

    <properties>
      <java.version>1.8</java.version>
      <spring-cloud.version>Greenwich.SR1</spring-cloud.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-openfeign</artifactId>
      </dependency>
    </dependencies>
    

    2、主启动类

    需要在启动类上加入@EnableFeignClients表示这是一个Feign客户端

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

    3、接口定义

    需要在发起远程调用的接口加上@FeignClient(name="xxx")其中name为远程服务的在注册中心中的名字

    (!注意:@FeignClient注解中name的值不能使用下划线"_",原因是FeginClient不支持下划线)
    /**
    *   @FeignClient  注解中name的值不能使用下划线"_"
    *   @FeignClient  只能标记在interface上
    */
    @FeignClient(name = "my-application")
    public interface CenterRestService {
        // 接口中的方法需要加上  @RequestMapping  其中value为被调用方的 uri ,且必须指定请求方式,如GET或POST
        @RequestMapping(value = "hello",method = RequestMethod.POST)
        void doSomeThing();
    }
    

    4、参数传递

    相关文章

      网友评论

          本文标题:Feign入门

          本文链接:https://www.haomeiwen.com/subject/rmjjlctx.html