[JAVA] SpringCloudFeign超详细讲解

2094 0
Honkers 2022-11-8 17:06:43 | 显示全部楼层 |阅读模式
目录

    一、什么是Feign二、Feign能干什么三、Feign的使用步骤
      1、新建一个module2、配置Pom.xml3、配置applicatin.yaml4、配置configBean5、配置Controller类6、配置启动类7、改动API
        1)引入Feign依赖2)配置Service3)注意

    四、结果


一、什么是Feign

Feign是声明式Web Service客户端,它让微服务之间的调用变得更简单,类似controller调用service。SpringCloud集成了Ribbon和Eureka,可以使用Feigin提供负载均衡的http客户端。Feign是通过接口和注释来实现负载均衡的。

二、Feign能干什么

(摘抄自狂神说JAVA)
Feign能干什么?
Feign旨在使编写Java Http客户端变得更容易
前面使用Ribbon + RestTemplate时,利用RestTemplate对Http请求的封装处理,形成了一套模板化的调用方法。但是在实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一个客户端类来包装这些依赖服务的调用。所以,Feign在此基础上做了进一步的封装,由他来帮助我们定义和实现依赖服务接口的定义,在Feign的实现下,我们只需要创建一个接口并使用注解的方式来配置它 (类似以前Dao接口上标注Mapper注解,现在是一个微服务接口上面标注一个Feign注解),即可完成对服务提供方的接口绑定,简化了使用Spring Cloud Ribbon 时,自动封装服务调用客户端的开发量。
Feign默认集成了Ribbon
利用Ribbon维护了MicroServiceCloud-Dept的服务列表信息,并且通过轮询实现了客户端的负载均衡,而与Ribbon不同的是,通过Feign只需要定义服务绑定接口且以声明式的方法,优雅而简单的实现了服务调用。

三、Feign的使用步骤


1、新建一个module




2、配置Pom.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <parent>
  6.         <artifactId>springcloud-demo2</artifactId>
  7.         <groupId>com.you</groupId>
  8.         <version>1.0-SNAPSHOT</version>
  9.     </parent>
  10.     <modelVersion>4.0.0</modelVersion>
  11.     <artifactId>springcloud-eureka-7001</artifactId>
  12.     <dependencies>
  13.         <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
  14.         <!--Eureka Server-->
  15.         <dependency>
  16.             <groupId>org.springframework.cloud</groupId>
  17.             <artifactId>spring-cloud-starter-eureka-server</artifactId>
  18.             <version>1.4.6.RELEASE</version>
  19.         </dependency>
  20.         <!--热部署-->
  21.         <dependency>
  22.             <groupId>org.springframework.boot</groupId>
  23.             <artifactId>spring-boot-devtools</artifactId>
  24.         </dependency>
  25.     </dependencies>
  26. </project>
复制代码
3、配置applicatin.yaml

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

4、配置configBean
  1. package com.you.config;
  2. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.web.client.RestTemplate;
  6. @Configuration
  7. public class ConfigBean {
  8.     @Bean
  9.     @LoadBalanced  //ribbon
  10.     /*配置负载均衡实现RestTemplate*/
  11.     /*IRule*/
  12.     /*RoundRobinRule 轮询 */
  13.     /*RandomRule 随机*/
  14.     /*AvailabilityFilteringRule 优先过滤掉跳闸、访问故障的服务,对剩下的进行轮询 */
  15.     public RestTemplate getRestTemplate() {
  16.         return new RestTemplate();
  17.     }
  18. }
复制代码
5、配置Controller类
  1. package com.you.config;
  2. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.web.client.RestTemplate;
  6. @Configuration
  7. public class ConfigBean {
  8.     @Bean
  9.     @LoadBalanced  //ribbon
  10.     /*配置负载均衡实现RestTemplate*/
  11.     /*IRule*/
  12.     /*RoundRobinRule 轮询 */
  13.     /*RandomRule 随机*/
  14.     /*AvailabilityFilteringRule 优先过滤掉跳闸、访问故障的服务,对剩下的进行轮询 */
  15.     public RestTemplate getRestTemplate() {
  16.         return new RestTemplate();
  17.     }
  18. }
复制代码
6、配置启动类
  1. package com.you;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. import org.springframework.cloud.netflix.ribbon.RibbonClient;
  6. import org.springframework.cloud.openfeign.EnableFeignClients;
  7. import org.springframework.context.annotation.ComponentScan;
  8. @SpringBootApplication
  9. @EnableEurekaClient
  10. @EnableFeignClients(basePackages = {
  11.         "com.you"})
  12. public class FeignDeptConsumer_80 {
  13.     public static void main(String[] args) {
  14.         SpringApplication.run(FeignDeptConsumer_80.class,args);
  15.     }
  16. }
复制代码
7、改动API


1)引入Feign依赖


  1. <dependency>
  2.             <groupId>org.springframework.cloud</groupId>
  3.             <artifactId>spring-cloud-starter-feign</artifactId>
  4.             <version>1.4.6.RELEASE</version>
  5.         </dependency>
复制代码
2)配置Service


  1. package com.you.service;
  2. import com.you.pojo.Dept;
  3. import org.springframework.cloud.openfeign.FeignClient;
  4. import org.springframework.stereotype.Component;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.PathVariable;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. @Component
  9. @FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT")
  10. public interface DeptClientService {
  11.     @GetMapping("/dept/aDept/{id}")
  12.     public Dept getDeptOfId(@PathVariable("id") Long id);
  13. }
复制代码
3)注意



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



四、结果

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


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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

荣誉红客

关注
  • 4005
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行