赞
踩
Feign是Nefix开发的声明式、模块化的Http客户端。Feign可以帮助我们更快捷、优雅地调用Http Api。
在springclouid中使用 feign非常简单 --创建一个接口,并在接口中添加一些注解,代码就完成了,Feign支持多种注解
openFeign 是springcloud对Feign进行了增强,使得Feign支持了springmvc的注解,并整合了Ribbon和Eureka,从而让Feign的使用更加方便
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <!--服务注册与发现-->
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-openfeign</artifactId>
- </dependency>
- <dependency>
- <groupId>com.sofwin</groupId>
- <artifactId>springEntity</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- </dependencies>
加入到nacos的服务中
- server:
- port: 7000
- spring:
- cloud:
- nacos:
- discovery:
- server-addr: 127.0.0.1:8848
- heart-beat-interval: 1000
- application:
- name: openFeign
- package com.sofwin;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
- import org.springframework.cloud.openfeign.EnableFeignClients;
-
- /**
- * @author : wentao
- * @version : 1.0
- */
- @SpringBootApplication
- //开启服务注册与发现
- @EnableDiscoveryClient
- //开启Feign
- @EnableFeignClients
- public class App {
- public static void main(String[]args){
- SpringApplication.run(App.class,args);
- }
- }
- package com.sofwin.service;
-
- import com.sofwin.pojo.User;
- import org.springframework.cloud.openfeign.FeignClient;
- import org.springframework.cloud.openfeign.SpringQueryMap;
- import org.springframework.web.bind.annotation.*;
-
- import java.util.List;
-
- /**
- * @author : wentao
- * @version : 1.0
- */
- //写的是服务端的名称
- @FeignClient("nacos-previder")
- public interface UserService {
-
- @GetMapping("/user/data")
- String getUserName();
-
- @GetMapping("/user/info/{msg}")
- //这里注意了 这个注意@PathVariable 一定要添加name或者value属性
- String infoResful(@PathVariable(name = "msg") String msg);
-
- @GetMapping("/user/data5")
- //这里注意了 调用远程服务必须使用@RequesParam注解 并且名字与远程服务的参数名相同
- String simpleParam(@RequestParam("userName11") String userName,@RequestParam("pwd") String pwd);
-
- //其实openFeign默认就是json的格式 这里是为了规范
- @PostMapping("user/data6")
- User getUser (@RequestBody User user);
- @DeleteMapping("user/data7")
- User getUser2 (@RequestBody User user);
- //或者我们就可以使用一个注解 将json格式改为form表单的格式发送到远程服务中
- //然后远程不用写@RequestBody
- @PostMapping("user/data8")
- User getUser3 (@SpringQueryMap User user);
-
- //集合
- @PostMapping("user/data9")
- List<User> getList(@RequestBody User user);
- }
接口中定义远程服务中的的请求
使用 @FeignClien的注解 名称是远程服务的名称
- package com.sofwin.controller;
-
- import com.sofwin.pojo.User;
- import com.sofwin.service.UserService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
-
- import java.util.List;
-
- /**
- * @author : wentao
- * @version : 1.0
- */
- @RestController
- @RequestMapping("/role")
- public class RoleController {
-
- @Autowired
- private UserService userService;
- @GetMapping("/data")
- public String data(){
- String userName = userService.getUserName();
- return userName;
- }
- //Resful风格
- @GetMapping("data1")
- public String infoResful(String msg){
- String s = userService.infoResful(msg);
- return s;
- }
- //简单类型的请求参数
- @GetMapping("/data2")
- public String simpleParam(String userName,String pwd){
- return userService.simpleParam(userName,pwd);
- }
- //自定义 json字符串 或者表单形式
- @PostMapping("/data3")
- public User getUser(User user){
- return userService.getUser(user);
- }
- @DeleteMapping("/data4")
- public User getUser2(User user){
- return userService.getUser2(user);
- }
- @PostMapping("/data5")
- public User getUser3(User user){
- return userService.getUser3(user);
- }
- //返回结果为集合类型
- @PostMapping("/data6")
- public List<User> getList(User user){
- return userService.getList(user);
- }
- }
注意:
//其实openFeign默认就是json的格式
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。