日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

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

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

發(fā)布時(shí)間:2025/3/21 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 学习微服务服务消费者——Feign 小編覺得挺不錯(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接口,通過@ FeignClient(“服務(wù)名”),來指定調(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)目,訪問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ǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。