簡單接入spring-cloud-feign

首先在pom裏引入依賴:主要是eurkea和feign


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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.3.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

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

其次,在主類聲明:@EnableFeignClients,@EnableDiscoveryClient

package com.sc.feignconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class FeignConsumerApplication {

	public static void main(String[] args) {
		SpringApplication.run(FeignConsumerApplication.class, args);
	}

}

然後,定義一個服務:主要是聲明@FeignClient,裏面的hello-service不分大小寫

package com.sc.feignconsumer.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

@Component
@FeignClient("hello-service")
public interface HelloService {

    @RequestMapping("/hello")
    String hello();

}

接着,定義一個restful api:注入剛纔提供的HelloService

package com.sc.feignconsumer.controller;

import com.sc.feignconsumer.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FeignConsumerController {

    @Autowired
    private HelloService helloService;

    @GetMapping("/feign-consumer")
    public String helloConsumer(){
        return this.helloService.hello();
    }
}

運行測試:明顯的,feign自動幫我們實現了RestTemplate以及ribbon!

觀察Eureka面板:

 

基於以上結果,擴展至帶RequestParam,RequestHeader,RequestBody的參數綁定:

首先定義實體User,注意其中的默認構造函數:

package com.sc.feignconsumer.entity;

public class User {

    private String name;
    private Integer age;

    // 要有默認構造函數,否則會報錯 Cannot construct instance of `com.sc.feignconsumer.entity.User` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
    public User(){}

    public User( String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

其次,聲明服務接口:

package com.sc.feignconsumer.service;

import com.sc.feignconsumer.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

@Component
@FeignClient("hello-service")
public interface HelloService {

    @RequestMapping("/hello")
    String hello();

    // 注意:以下三個方法,必須顯示聲明參數名稱,否則Feign會拋出IIIegalStateException
    @GetMapping("/hello1")
    String hello(@RequestParam("name") String name);

    @GetMapping("/hello2")
    User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age);

    @PostMapping("/hello3")
    String hello(@RequestBody User user);

}

在控制器中調用如下:

    @GetMapping("/feign-consumer2")
    public String helloConsumer2() {
        StringBuilder sb = new StringBuilder();
        sb.append(this.helloService.hello("daluo")).append("\n");
        sb.append(this.helloService.hello("spring", 30)).append("\n");
        sb.append(this.helloService.hello(new User("feign", 20))).append("\n");
        return sb.toString();
    }

結果:

 

 

 

 

 

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