SpringCloud Feign如何实现超详细深入解析?

2026-05-26 01:102阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计938个文字,预计阅读时间需要4分钟。

SpringCloud Feign如何实现超详细深入解析?

目录

一、什么是Feign?

二、Feign能干些什么?

三、Feign的使用步骤

1. 新建一个module 2. 配置Pom.xml 3. 配置application.yaml 4. 配置configBean 5. 配置Controller类 6. 配置启动类 7. 修改API引言:引入Feign

目录
  • 一、什么是Feign
  • 二、Feign能干什么
  • 三、Feign的使用步骤
    • 1、新建一个module
    • 2、配置Pom.xml
    • 3、配置applicatin.yaml
    • 4、配置configBean
    • 5、配置Controller类
    • 6、配置启动类
    • 7、改动API
      • 1)引入Feign依赖
      • 2)配置Service
      • 3)注意
  • 四、结果

    一、什么是Feign

    Feign是声明式Web Service客户端,它让微服务之间的调用变得更简单,类似controller调用service。SpringCloud集成了Ribbon和Eureka,可以使用Feigin提供负载均衡的maven.apache.org/POM/4.0.0" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="maven.apache.org/POM/4.0.0 maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springcloud-demo2</artifactId> <groupId>com.you</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-eureka-7001</artifactId> <dependencies> <!-- mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server --> <!--Eureka Server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!--热部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>

    3、配置applicatin.yaml

    server:
    port: 801

    eureka:
    client:
    register-with-eureka: false #不向eureka注册自己
    service-url:
    defaultZone: localhost:7001/eureka/
    ribbon:
    eureka:
    enabled: true

    4、配置configBean

    package com.you.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class ConfigBean { @Bean @LoadBalanced //ribbon /*配置负载均衡实现RestTemplate*/ /*IRule*/ /*RoundRobinRule 轮询 */ /*RandomRule 随机*/ /*AvailabilityFilteringRule 优先过滤掉跳闸、访问故障的服务,对剩下的进行轮询 */ public RestTemplate getRestTemplate() { return new RestTemplate(); } }

    5、配置Controller类

    package com.you.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class ConfigBean { @Bean @LoadBalanced //ribbon /*配置负载均衡实现RestTemplate*/ /*IRule*/ /*RoundRobinRule 轮询 */ /*RandomRule 随机*/ /*AvailabilityFilteringRule 优先过滤掉跳闸、访问故障的服务,对剩下的进行轮询 */ public RestTemplate getRestTemplate() { return new RestTemplate(); } }

    6、配置启动类

    package com.you; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.ribbon.RibbonClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @EnableEurekaClient @EnableFeignClients(basePackages = { "com.you"}) public class FeignDeptConsumer_80 { public static void main(String[] args) { SpringApplication.run(FeignDeptConsumer_80.class,args); } }

    SpringCloud Feign如何实现超详细深入解析?

    7、改动API

    1)引入Feign依赖

    <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> <version>1.4.6.RELEASE</version> </dependency>

    2)配置Service

    package com.you.service; import com.you.pojo.Dept; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @Component @FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT") public interface DeptClientService { @GetMapping("/dept/aDept/{id}") public Dept getDeptOfId(@PathVariable("id") Long id); }

    3)注意

    服务名字要写对GetMapper中的内容要和提供者一致,否则报错(找了一下午)

    下面是提供者的内容

    四、结果

    这样即可获取到数据,而且负载平衡的默认算法,仍然是轮询!

    到此这篇关于SpringCloud Feign超详细讲解的文章就介绍到这了,更多相关SpringCloud Feign内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

    本文共计938个文字,预计阅读时间需要4分钟。

    SpringCloud Feign如何实现超详细深入解析?

    目录

    一、什么是Feign?

    二、Feign能干些什么?

    三、Feign的使用步骤

    1. 新建一个module 2. 配置Pom.xml 3. 配置application.yaml 4. 配置configBean 5. 配置Controller类 6. 配置启动类 7. 修改API引言:引入Feign

    目录
    • 一、什么是Feign
    • 二、Feign能干什么
    • 三、Feign的使用步骤
      • 1、新建一个module
      • 2、配置Pom.xml
      • 3、配置applicatin.yaml
      • 4、配置configBean
      • 5、配置Controller类
      • 6、配置启动类
      • 7、改动API
        • 1)引入Feign依赖
        • 2)配置Service
        • 3)注意
    • 四、结果

      一、什么是Feign

      Feign是声明式Web Service客户端,它让微服务之间的调用变得更简单,类似controller调用service。SpringCloud集成了Ribbon和Eureka,可以使用Feigin提供负载均衡的maven.apache.org/POM/4.0.0" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="maven.apache.org/POM/4.0.0 maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springcloud-demo2</artifactId> <groupId>com.you</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-eureka-7001</artifactId> <dependencies> <!-- mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server --> <!--Eureka Server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!--热部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>

      3、配置applicatin.yaml

      server:
      port: 801

      eureka:
      client:
      register-with-eureka: false #不向eureka注册自己
      service-url:
      defaultZone: localhost:7001/eureka/
      ribbon:
      eureka:
      enabled: true

      4、配置configBean

      package com.you.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class ConfigBean { @Bean @LoadBalanced //ribbon /*配置负载均衡实现RestTemplate*/ /*IRule*/ /*RoundRobinRule 轮询 */ /*RandomRule 随机*/ /*AvailabilityFilteringRule 优先过滤掉跳闸、访问故障的服务,对剩下的进行轮询 */ public RestTemplate getRestTemplate() { return new RestTemplate(); } }

      5、配置Controller类

      package com.you.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class ConfigBean { @Bean @LoadBalanced //ribbon /*配置负载均衡实现RestTemplate*/ /*IRule*/ /*RoundRobinRule 轮询 */ /*RandomRule 随机*/ /*AvailabilityFilteringRule 优先过滤掉跳闸、访问故障的服务,对剩下的进行轮询 */ public RestTemplate getRestTemplate() { return new RestTemplate(); } }

      6、配置启动类

      package com.you; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.ribbon.RibbonClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @EnableEurekaClient @EnableFeignClients(basePackages = { "com.you"}) public class FeignDeptConsumer_80 { public static void main(String[] args) { SpringApplication.run(FeignDeptConsumer_80.class,args); } }

      SpringCloud Feign如何实现超详细深入解析?

      7、改动API

      1)引入Feign依赖

      <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> <version>1.4.6.RELEASE</version> </dependency>

      2)配置Service

      package com.you.service; import com.you.pojo.Dept; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @Component @FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT") public interface DeptClientService { @GetMapping("/dept/aDept/{id}") public Dept getDeptOfId(@PathVariable("id") Long id); }

      3)注意

      服务名字要写对GetMapper中的内容要和提供者一致,否则报错(找了一下午)

      下面是提供者的内容

      四、结果

      这样即可获取到数据,而且负载平衡的默认算法,仍然是轮询!

      到此这篇关于SpringCloud Feign超详细讲解的文章就介绍到这了,更多相关SpringCloud Feign内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!