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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

远程调用 Spring Cloud Feign

發(fā)布時(shí)間:2023/12/3 javascript 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 远程调用 Spring Cloud Feign 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、 Feign簡介

Feign [fe?n] 譯文 偽裝。Feign是一個(gè)聲明式WebService客戶端.使用Feign能讓編寫WebService客戶端更加簡單,它的使用方法是定義一個(gè)接口,然后在上面添加注解。不再需要拼接URL,參數(shù)等操作。項(xiàng)目主頁:https://github.com/OpenFeign/feign 。

  • 集成Ribbon的負(fù)載均衡功能
  • 集成了Hystrix的熔斷器功能
  • 支持請(qǐng)求壓縮
  • 大大簡化了遠(yuǎn)程調(diào)用的代碼,同時(shí)功能還增強(qiáng)啦
  • Feign以更加優(yōu)雅的方式編寫遠(yuǎn)程調(diào)用代碼,并簡化重復(fù)代碼

快速入門

實(shí)現(xiàn)步驟:

  • 導(dǎo)入feign依賴
  • 編寫Feign客戶端接口
  • 消費(fèi)者啟動(dòng)引導(dǎo)類開啟Feign功能注解
  • 訪問接口測試
  • 1. 導(dǎo)入feign依賴

    <!--配置feign--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>

    1.1創(chuàng)建實(shí)體類User

    package com.william.domain;import java.util.Date;/*** @author :lijunxuan* @date :Created in 2019/6/30 18:20* @description :* @version: 1.0*/ public class User {private Integer id;//主鍵idprivate String username;//用戶名private String password;//密碼private String name;//姓名private Integer age;//年齡private Integer sex;//性別 1男性,2女性private Date birthday; //出生日期private Date created; //創(chuàng)建時(shí)間private Date updated; //更新時(shí)間private String note;//備注@Overridepublic String toString() {return "User{" +"id=" + id +", username='" + username + '\'' +", password='" + password + '\'' +", name='" + name + '\'' +", age=" + age +", sex=" + sex +", birthday=" + birthday +", created=" + created +", updated=" + updated +", note='" + note + '\'' +'}';}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Integer getSex() {return sex;}public void setSex(Integer sex) {this.sex = sex;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public Date getCreated() {return created;}public void setCreated(Date created) {this.created = created;}public Date getUpdated() {return updated;}public void setUpdated(Date updated) {this.updated = updated;}public String getNote() {return note;}public void setNote(String note) {this.note = note;} }

    2. 編寫Feign客戶端接口

    package com.william.service;import com.william.domain.User; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam;@FeignClient("user-service") public interface UserClient {/***RequestMapping* spring mvc 里面的注解* RequestMapping 反向映射,生成請(qǐng)求地址。http請(qǐng)求* @RequestParam("id") 是必須要進(jìn)行請(qǐng)求地址參數(shù)的綁定* @param id* @return*/@RequestMapping("/user/findById")User findById(@RequestParam("id") Integer id); }

    3. 消費(fèi)者啟動(dòng)引導(dǎo)類DemoApplication開啟Feign功能注解

    @EnableFeignClients//開啟feign客戶端支持

    3.1編寫ConsumerFeignController,使用UserClient訪問

    package com.william.controller;import com.william.domain.User; import com.william.service.UserClient; import org.springframework.stereotype.Repository; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;/*** @author :lijunxuan* @date :Created in 2019/6/30 18:33* @description :* @version: 1.0*/ @RestController public class FeigConsumerController {@ResourceUserClient userClient;//注入FeignCLient@RequestMapping("/findByIdFeign")public User findByIdFeign(Integer id){return userClient.findById(id);} }

    4. 訪問接口測試


    總結(jié)

    以上是生活随笔為你收集整理的远程调用 Spring Cloud Feign的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。