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.  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章