天天看點

Feign(聲明式rest調用)的使用

首先正常建立一個springboot類,建立的過程中添加eureka discovery  和 feign  ;

Feign(聲明式rest調用)的使用

建立成功之後需要做的就是:在client端添加feign 的 jar包  

<dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-openfeign</artifactId>

</dependency>
           

在feign中添加一個類,一個實作類:

package com.example.feign.controller;



import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;



@RestController

public class UserServerImpl {



    //@RequestMapping(method = RequestMethod.GET,value = "/hellofeign")   兩個都是一樣的

    @GetMapping("/hellofeign")

    public String hello(){

        return "hello world! feign";

    }

}
           

feign啟動類裡面需要預設他是被發現類

package com.example.feign;



import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;



@SpringBootApplication

@EnableEurekaClient   這個注解使這個服務啟動之後能夠被server發現,并進行注冊。

public class FeignApplication {

    public static void main(String[] args) {

        SpringApplication.run(FeignApplication.class, args);

    }

}
           

接下來就是client端進行操作了,client需要消費feign提供的方法。

先提供一個借口類,這個借口類将會調用目标feign的方法

package com.example.client.client;



import org.springframework.cloud.netflix.feign.FeignClient;

import org.springframework.web.bind.annotation.GetMapping;



@FeignClient(name = "feign")   這個位置是指調用目标伺服器,注冊名字為name指定的伺服器

public interface UserFeignClient {



    @GetMapping("/hellofeign")   這個對應到feign中的方法攔截标簽

    String hello();

}
           

client端需要進行實作這個借口,并且需要在client端口進行攔截請求。

package com.example.client.controller;



import com.example.client.client.UserFeignClient;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.RestController;



@RestController

public class UserController {



    @Autowired  通過注解将接口進行實作注入

    private UserFeignClient userFeignClient;



    @GetMapping("/hello")  這個位置攔截client端口的請求

    public String hello(){

        return "hello world ! client"+userFeignClient.hello();

        //return "hello world ! client";

    }

}
           

接下來就是在client端進行設定,使這個client可以請求feign端口

package com.example.client;



import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

import org.springframework.cloud.netflix.feign.EnableFeignClients;



@SpringBootApplication

@EnableEurekaClient

@EnableFeignClients   這個就是對feign的客戶類定義

public class ClientApplication {

    public static void main(String[] args) {

        SpringApplication.run(ClientApplication.class, args);

    }

}
           

這樣的話就可以實作client消費,feign提供服務這樣的架構了。

接下來就是傳遞參數:

傳遞參數和一般的web項目時一樣的,有get傳遞,post傳遞,也會有restful風格的傳遞。

get傳遞: 通過@RequestParam注解進行接收,需要注意的是:接口裡面的注解必須要指定接收的name。

注解:@RequestParam     @GetMapping  

tips:傳遞map類對象的時候  使用@RequestBody 進行傳遞

@GetMapping("/checkusername")

boolean checkuser(@RequestParam("userName" 這個位置必須要寫 ) String userName,@RequestParam("userPwd") String userPwd);
           

post傳遞:基本上痛get傳遞類似。

注解:@RequestParam     @PostMapping  

restful風格傳遞:

@RequestMapping(value = "get/{id}")

    public Dept get(@PathVariable("id") Long id) {

        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id,Dept.class);

    }
           

工具包:如果有共同的東西需要共同使用,例如:添加一個user實體類,client端和feign端都要使用,這個時候需要建立一個工具包(可以是Java項目,也可以是maven項目,一般用maven項目,這樣就可以直接在其他項目中添加依賴,添加依賴的時候需要注意的是作用範圍為comple範圍)。