springcloud?整合?openfeign的方法
一、openfeign簡介
Feign是Nefix開發(fā)的聲明式、模塊化的Http客戶端。Feign可以幫助我們更快捷、優(yōu)雅地調(diào)用Http Api。
在springclouid中使用 feign非常簡單 --創(chuàng)建一個接口,并在接口中添加一些注解,代碼就完成了,F(xiàn)eign支持多種注解
openFeign 是springcloud對Feign進(jìn)行了增強(qiáng),使得Feign支持了springmvc的注解,并整合了Ribbon和Eureka,從而讓Feign的使用更加方便
二、使用
1、依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--服務(wù)注冊與發(fā)現(xiàn)-->
<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>2、配置文件
加入到nacos的服務(wù)中
server:
port: 7000
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
heart-beat-interval: 1000
application:
name: openFeign3、啟動類
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
//開啟服務(wù)注冊與發(fā)現(xiàn)
@EnableDiscoveryClient
//開啟Feign
@EnableFeignClients
public class App {
public static void main(String[]args){
SpringApplication.run(App.class,args);
}
}4、接口
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
*/
//寫的是服務(wù)端的名稱
@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")
//這里注意了 調(diào)用遠(yuǎn)程服務(wù)必須使用@RequesParam注解 并且名字與遠(yuǎn)程服務(wù)的參數(shù)名相同
String simpleParam(@RequestParam("userName11") String userName,@RequestParam("pwd") String pwd);
//其實openFeign默認(rèn)就是json的格式 這里是為了規(guī)范
@PostMapping("user/data6")
User getUser (@RequestBody User user);
@DeleteMapping("user/data7")
User getUser2 (@RequestBody User user);
//或者我們就可以使用一個注解 將json格式改為form表單的格式發(fā)送到遠(yuǎn)程服務(wù)中
//然后遠(yuǎn)程不用寫@RequestBody
@PostMapping("user/data8")
User getUser3 (@SpringQueryMap User user);
//集合
@PostMapping("user/data9")
List<User> getList(@RequestBody User user);
}接口中定義遠(yuǎn)程服務(wù)中的的請求
使用 @FeignClien的注解 名稱是遠(yuǎn)程服務(wù)的名稱
5、controller
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風(fēng)格
@GetMapping("data1")
public String infoResful(String msg){
String s = userService.infoResful(msg);
return s;
}
//簡單類型的請求參數(shù)
@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);
}
//返回結(jié)果為集合類型
@PostMapping("/data6")
public List<User> getList(User user){
return userService.getList(user);
}
}注意:
//其實openFeign默認(rèn)就是json的格式
到此這篇關(guān)于springcloud 整合 openfeign的文章就介紹到這了,更多相關(guān)springcloud 整合 openfeign內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring如何基于Proxy及cglib實現(xiàn)動態(tài)代理
這篇文章主要介紹了Spring如何基于Proxy及cglib實現(xiàn)動態(tài)代理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
使用JavaIO流和網(wǎng)絡(luò)制作一個簡單的圖片爬蟲
這篇文章主要介紹了使用JavaIO流和網(wǎng)絡(luò)制作一個簡單的圖片爬蟲,通過關(guān)鍵字爬取百度圖片,這個和我們使用搜索引擎搜索百度圖片是一樣的,只是通過爬蟲可以學(xué)習(xí)技術(shù)的使用,需要的朋友可以參考下2023-04-04
Java通過反射機(jī)制將對象封裝成JSON和JsonArray格式
這篇文章主要介紹了Java通過反射機(jī)制將對象封裝成JSON和JsonArray格式,JAVA反射機(jī)制是在運行狀態(tài)中,對于任意一個實體類,都能夠知道這個類的所有屬性和方法,需要的朋友可以參考下2023-10-10
BufferedInputStream(緩沖輸入流)詳解_動力節(jié)點Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了BufferedInputStream緩沖輸入流的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
Java Socket聊天室編程(二)之利用socket實現(xiàn)單聊聊天室
這篇文章主要介紹了Java Socket聊天室編程(二)之利用socket實現(xiàn)單聊聊天室的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09

