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

歡迎訪問 生活随笔!

生活随笔

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

javascript

Eclipse(STS) 初次搭建Spring Cloud项目之声明式REST调用+负载均衡实现Feign(四)

發布時間:2024/4/17 javascript 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Eclipse(STS) 初次搭建Spring Cloud项目之声明式REST调用+负载均衡实现Feign(四) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、什么是Feign

  • Feign是一個http請求調用的輕量級框架,可以以Java接口注解的方式調用Http請求,而不用像Java中通過封裝HTTP請求報文的方式直接調用。Feign通過處理注解,將請求模板化,當實際調用的時候,傳入參數,根據參數再應用到請求上,進而轉化成真正的請求,這種請求相對而言比較直觀。
  • Feign被廣泛應用在Spring Cloud 的解決方案中,是學習基于Spring Cloud 微服務架構不可或缺的重要組件。
  • Feign具有可插拔的注解特性,可使用Feign注解和JAX-RS注解。
  • Feign默認集成了Ribbon,并和Eureka結合,默認實現了負載均衡的效果。
  • 二、下面來看看具體實現

    首先創建Feign模塊,cloud-demo-feign

    下面是項目結構

    pom文件如下

    <?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><modelVersion>4.0.0</modelVersion><parent><groupId>com.hewl</groupId><artifactId>cloud-demo-parent</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>cloud-demo-feign</artifactId><name>cloud-demo-feign</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency></dependencies> </project> 復制代碼

    application.yml文件如下

    server:port: 8811 # 你的端口 spring:application:name: feign eureka:client:service-url:defaultZone: http://admin:admin@server1:8761/eureka/,http://admin:admin@server2:8762/eureka/ 復制代碼

    啟動類內容如下,加上@EnableFeignClients注解開啟Feign的功能

    package com.hewl;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication @EnableEurekaClient @EnableFeignClients public class FeignApplication {public static void main(String[] args) {SpringApplication.run(FeignApplication.class, args);} } 復制代碼

    定義Feign接口,SchedualCilentName

    package com.hewl.feign;import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam;@FeignClient(value="service-eureka-client") public interface SchedualCilentName {@GetMapping(value = "/test")public String testFromClientOne(@RequestParam("name") String name);} 復制代碼

    定義對外的controller

    package com.hewl.controller;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;import com.hewl.feign.SchedualCilentName;@RestController public class TestController {@Autowiredpublic SchedualCilentName schedualCilentName;@GetMapping(value="/test")public String test (String name) {return schedualCilentName.testFromClientOne(name);} }復制代碼

    按順序啟動項目

  • 啟動eureka-server
  • 修改端口分別啟動兩次eureka-client
  • 啟動feign客戶端
  • 訪問localhost:8811/test?name=張三,訪問成功并且已經成功啟動負載均衡

    轉載于:https://juejin.im/post/5d01e679f265da1b5f265021

    總結

    以上是生活随笔為你收集整理的Eclipse(STS) 初次搭建Spring Cloud项目之声明式REST调用+负载均衡实现Feign(四)的全部內容,希望文章能夠幫你解決所遇到的問題。

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