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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

学习微服务服务消费者——Feign

發(fā)布時(shí)間:2025/3/21 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 学习微服务服务消费者——Feign 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

微服務(wù)之間的調(diào)用除了restTemplate+ribbon,還有一種方式就是Feign

1.新建一個(gè)feign服務(wù)

build.gradle文件

buildscript {ext {springBootVersion = '2.0.4.RELEASE'}repositories {mavenCentral()}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")} }apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management'group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8repositories {mavenCentral() }ext {springCloudVersion = 'Finchley.SR1' }dependencies {compile('org.springframework.boot:spring-boot-starter-web')compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')compile('org.springframework.cloud:spring-cloud-starter-openfeign')testCompile('org.springframework.boot:spring-boot-starter-test') }dependencyManagement {imports {mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"} }

application.yml文件

server:port: 8795spring:application:name: service-feigneureka:client:service-url:defaultZone: http://localhost:8791/eureka/

啟動(dòng)主方法

package com.example.servicefeign;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients;@EnableFeignClients @EnableEurekaClient @SpringBootApplication public class ServiceFeignApplication {public static void main(String[] args) {SpringApplication.run(ServiceFeignApplication.class, args);} }

定義一個(gè)feign接口,通過(guò)@ FeignClient(“服務(wù)名”),來(lái)指定調(diào)用哪個(gè)服務(wù)。比如在代碼中調(diào)用了service-hi服務(wù)的“/hi”接口,

SayHiService.java文件

package com.example.servicefeign;import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod;@FeignClient("eureka-client-say-hi") //要調(diào)用的服務(wù)名 public interface SayHiService {@RequestMapping(value = "/hi", method = RequestMethod.GET) //要調(diào)用的服務(wù)的接口路由,String sayHiFromClient(); //括號(hào)里面,可填充要調(diào)用的接口的參數(shù)}

HelloController.java文件

package com.example.servicefeign;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RestController public class HelloController {@AutowiredSayHiService sayHiService;@RequestMapping("/hi")public String sayHi(){return sayHiService.sayHiFromClient();} }

啟動(dòng)該項(xiàng)目,訪問(wèn)http://localhost:8795/hi

?

此時(shí)發(fā)現(xiàn)feign采用基于接口的注解,并且具有負(fù)載均衡的功能

?

轉(zhuǎn)載于:https://my.oschina.net/u/2263272/blog/1925828

總結(jié)

以上是生活随笔為你收集整理的学习微服务服务消费者——Feign的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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