Feign(声明式rest调用)的使用
首先正常创建一个springboot类,创建的过程中添加eureka discovery 和 feign ;
|
创建成功之后需要做的就是:在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范围)。