日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring-Cloud | openfeign使用细节

發布時間:2025/3/20 javascript 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring-Cloud | openfeign使用细节 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

寫在前面的話

各位,下午好!

我比較喜歡用 fegin 來實現微服務之間的調用,但是feign使用的那些細節,是get到了嗎?本節我將使用Spring Boot 2.0.5.RELEASE + Spring Cloud SR1 + openfeign并結合實際的使用,教你使用feign的姿勢。

項目架構

我們先對測試架構一番,看圖

簡單來說,就是服務模塊化分為:model層、API層、service層,其他服務就可以依賴API層。

另外,我們看一下,Spring官網提供的一段關于Feign Inheritance Support代碼:

下面我們就動手寫例子。

測試實例

1、先看一下完成后的目錄截圖

我們看 api、 model、 service、 feign-use之間的依賴關系。 api依賴model service依賴api,實現api接口 feign-use依賴api,client繼承api,并注入spring bean

2、使用公益eureka,這樣我們就省略構建服務注冊中心了

eureka:client:service-url:defaultZone: http://eureka.fengwenyi.com/eureka/

3、關于項目多模塊化,看這里:https://github.com/fengwenyi/multi-module。

4、model中的代碼:

import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors;/*** @author Wenyi Feng* @since 2018-09-15*/ @Data @EqualsAndHashCode(callSuper = false) @Accessors(chain = true) public class User {/** 標識 */private String uuid;/** 姓名 */private String name;/** 年齡 */private Integer age; }

5、API接口

import com.fengwenyi.data.model.User; import org.springframework.web.bind.annotation.*;import java.util.List;/*** @author Wenyi Feng* @since 2018-09-15*/ public interface API {/*** 獲取users* @return*/@GetMapping("/getUsers")List<User> getUsers();/*** 根據用戶ID獲取user* @param uuid 用戶ID* @return*/@GetMapping("/getUserById/{uuid}")User getUserById(@PathVariable("uuid") String uuid);/*** 添加用戶* @param user 用戶對象* @return*/@PostMapping("/addUser")boolean addUser(@RequestBody User user);/*** 根據用戶ID修改用戶信息* @param uuid 用戶ID* @param user* @return*/@PostMapping("/updateUserById/{uuid}")boolean updateUserById(@PathVariable("uuid") String uuid, @RequestBody User user);/*** 根據用戶ID修改用戶信息* @param uuid 用戶ID* @param age 用戶年齡* @return*/@PostMapping("/updateById/{uuid}")boolean updateUserAgeById(@PathVariable("uuid") String uuid, @RequestBody Integer age);/*** 根據用戶ID刪除用戶* @param uuid 用戶ID* @return*/@DeleteMapping("/deleteUserById/{uuid}")boolean deleteUserById(@PathVariable("uuid") String uuid); }

6、API實現

import com.fengwenyi.data.API; import com.fengwenyi.data.model.User; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;@SpringBootApplication @EnableDiscoveryClient @RestController @Slf4j public class FeignAPIApplication implements API {public static void main(String[] args) {SpringApplication.run(FeignAPIApplication.class, args);// ApplicationContextAware // BeanUtils}Map<String, User> userMap = new HashMap<>();@Overridepublic List<User> getUsers() {if (userMap == null || userMap.isEmpty())return null;List<User> userList = new ArrayList<>();for (String uuid : userMap.keySet()) {userList.add(userMap.get(uuid));}return userList;}@Overridepublic User getUserById(@PathVariable String uuid) {if (userMap == null || userMap.isEmpty() || StringUtils.isEmpty(uuid))return null;return userMap.get(uuid);}@Overridepublic boolean addUser(@RequestBody User user) {if (user == null)return false;String uuid = user.getUuid();if (uuid == null)return false;if (userMap.get(uuid) != null)return false;User lastUser = userMap.put(uuid, user);if (lastUser != null)log.warn("uuid對應的user已被替換,uuid={}, lastUser={}, user={}", uuid, lastUser, user);return true;}@Overridepublic boolean updateUserById(@PathVariable String uuid, @RequestBody User user) {if (user == null || uuid == null)return false;if (userMap.get(uuid) == null)return false;User lastUser = userMap.put(uuid, user);if (lastUser != null)log.warn("uuid對應的user已被替換,uuid={}, lastUser={}, user={}", uuid, lastUser, user);return true;}@Overridepublic boolean updateUserAgeById(@PathVariable String uuid, @RequestBody Integer age) {if (age == null || uuid == null || age < -1)return false;User user = userMap.get(uuid);if (user == null)return false;User lastUser = userMap.put(uuid, user.setAge(age));if (lastUser != null)log.warn("uuid對應的user已被替換,uuid={}, lastUser={}, user={}", uuid, lastUser, user);return true;}@Overridepublic boolean deleteUserById(@PathVariable String uuid) {if (uuid == null)return false;if (userMap.get(uuid) == null)return false;User lastUser = userMap.remove(uuid);if (lastUser != null)log.warn("uuid對應的user已被刪除,uuid={}, lastUser={}", uuid, lastUser);return true;} }

7、API繼承

import com.fengwenyi.data.API; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component;/*** @author Wenyi Feng* @since 2018-09-15*/ @FeignClient("feignapi") public interface APIClient extends API { }

8、寫調用測試代碼

import com.fengwenyi.data.model.User; import com.fengwenyi.javalib.util.StringUtil; import com.fengwenyi.javalib.util.Utils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController;@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients @RestController public class FeignUseApplication {public static void main(String[] args) {SpringApplication.run(FeignUseApplication.class, args);}@Autowiredprivate APIClient apiClient;@GetMapping("/add/{name}/{age}")public Object add(@PathVariable("name") String name, @PathVariable("age") Integer age) {if (StringUtil.isEmpty(name)|| age == null|| age < 0)return false;return apiClient.addUser(new User().setUuid(Utils.getUUID()).setName("張三").setAge(20));}@GetMapping("/updateUser/{uuid}")public Object updateUser(@PathVariable("uuid") String uuid) {if (StringUtil.isEmpty(uuid))return false;User user = apiClient.getUserById(uuid);if (user == null)return false;return apiClient.updateUserById(uuid,user.setName("張三 - Zhangsan").setAge(21));}@GetMapping("/update/{uuid}")public Object update(@PathVariable("uuid") String uuid) {if (StringUtil.isEmpty(uuid))return false;User user = apiClient.getUserById(uuid);if (user == null)return false;return apiClient.updateUserAgeById(uuid, 23);}@GetMapping("/delete/{uuid}")public Object delete(@PathVariable("uuid") String uuid) {if (StringUtil.isEmpty(uuid))return false;User user = apiClient.getUserById(uuid);if (user == null)return false;return apiClient.deleteUserById(uuid);}@GetMapping("gets")public Object gets() {return apiClient.getUsers();}@GetMapping("/get/{uuid}")public Object get(@PathVariable("uuid") String uuid) {if (StringUtil.isEmpty(uuid))return null;return apiClient.getUserById(uuid);} }

關于測試

1、添加操作

http://localhost:8080/add/張三/19 http://localhost:8080/add/李四/18 http://localhost:8080/add/王五/17

2、查詢

我們通過這個接口,看一下添加的情況:

http://localhost:8080/gets

響應 不好意思,上面代碼有點問題。修改了下。

[{"uuid":"fddde49a35fe4947950571a93ebfaa1d","name":"張三","age":19},{"uuid":"e136860677a7463d8bcc3c88e0801931","name":"王五","age":17},{"uuid":"b440ebdf36964b62aea2025549409d4a","name":"李四","age":18} ]

單個查詢

http://localhost:8080/get/e136860677a7463d8bcc3c88e0801931

響應

{"uuid":"e136860677a7463d8bcc3c88e0801931","name":"王五","age":17 }

3、修改操作

http://localhost:8080/updateUser/e136860677a7463d8bcc3c88e0801931

修改之后,數據是這樣子的

[{"uuid":"fddde49a35fe4947950571a93ebfaa1d","name":"張三","age":19},{"uuid":"e136860677a7463d8bcc3c88e0801931","name":"張三 - Zhangsan","age":21},{"uuid":"b440ebdf36964b62aea2025549409d4a","name":"李四","age":18} ]

4、刪除

http://localhost:8080/delete/b440ebdf36964b62aea2025549409d4a

刪除之后,數據是這樣子的

[{"uuid":"fddde49a35fe4947950571a93ebfaa1d","name":"張三","age":19},{"uuid":"e136860677a7463d8bcc3c88e0801931","name":"張三 - Zhangsan","age":21} ]

5、看一下控制臺

2018-09-15 13:54:34.304 WARN 9732 --- [qtp489047267-34] c.fengwenyi.service.FeignAPIApplication : uuid對應的user已被替換,uuid=e136860677a7463d8bcc3c88e0801931, lastUser=User(uuid=e136860677a7463d8bcc3c88e0801931, name=王五, age=17), user=User(uuid=e136860677a7463d8bcc3c88e0801931, name=張三 - Zhangsan, age=21)2018-09-15 13:56:18.367 WARN 9732 --- [qtp489047267-35] c.fengwenyi.service.FeignAPIApplication : uuid對應的user已被刪除,uuid=b440ebdf36964b62aea2025549409d4a, lastUser=User(uuid=b440ebdf36964b62aea2025549409d4a, name=李四, age=18)

測試代碼

點擊這里,查看本節測試代碼。

大抵就是這樣子,感謝。

轉載于:https://my.oschina.net/fengwenyi/blog/2051769

總結

以上是生活随笔為你收集整理的Spring-Cloud | openfeign使用细节的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。