Springcloud学习(六)Feign的使用-声明式调用

what is feign?

     feign是一个http调用的轻量级框架。可以以java接口的方式调用http请求。简化了传统的自己封装构造对象发送http请求调用service。通过使用注解将请求模板化当实际调用的时候,传入参数,根据参数再应用到请求上,进而转化成真正的请求,这种请求相对而言比较直观。

     github源码地址:https://github.com/Jacwo/feign-customer.git

开始

  1. 使用idea新建一个maven项目 可以使用spring Initializr
  2. 替换pom.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.yyl</groupId>
        <artifactId>feign-customer</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>feign-customer</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-parent</artifactId>
            <version>Dalston.SR5</version>
            <relativePath/>
        </parent>
    
        <dependencies>
            <!-- Hystrix,Feign是基于Hystrix的-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-hystrix</artifactId>
            </dependency>
            <!-- Eureka依赖,连接注册中心的都需要有这个依赖 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
            <!-- Feign依赖,声明式开发 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-feign</artifactId>
            </dependency>
            <!-- SpringMVC依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    

     

  3. 编写启动类
    package com.yyl.feign;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.feign.EnableFeignClients;
    
    @EnableFeignClients
    @SpringBootApplication
    public class FeignCustomerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(FeignCustomerApplication.class, args);
        }
    
    }
    

     

  4. 编写controller
    package com.yyl.feign.controller;
    
    import com.yyl.feign.api.HelloApi;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    /**
     * author:yangyuanliang Date:2019-08-20 Time:13:43
     **/
    @Controller
    @RequestMapping("feign")
    public class HelloController {
        @Autowired
        private HelloApi helloApi;
        @GetMapping("/hello")
        @ResponseBody
        public String sayHello(){
            return helloApi.hello();
        }
    }
    

     

  5. 编写接口 @FeignClient注解当扫描到该注解的时候,会为该接口生成一个动态代理类。在代理类中会根据@RequestMapping组合成请求的url路径。在加上eureka的ribbon自动负载均衡寻找真正的服务地址和url拼接。封装http发送请求。返回相应结构
    package com.yyl.feign.api;
    
    import org.springframework.cloud.netflix.feign.FeignClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    /**
     * author:yangyuanliang Date:2019-08-20 Time:13:42
     *
     * @author yangyuanliang*/
    @FeignClient(value = "hello-service")
    public interface HelloApi {
        @RequestMapping(value = "/hello",method = RequestMethod.GET)
        String hello();
    }
    

    启动测试:

  6. 1.启动注册中心eureka-server

  7. 2.启动服务提供者eureka-server-provider

  8. 3.启动feign消费端 eureka-customer

  9. 4.打开浏览器访问 http://localhost:9001/feign/hello

  10.  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值