栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【SpringCloud】Feign 声明式服务调用入门学习

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【SpringCloud】Feign 声明式服务调用入门学习

1、什么是 Feign

Feign 是 Spring Cloud Netflix 组件中的一个轻量级的 RESTful 的 HTTP 服务客户端,实现了负载均衡和 Rest 调用的开源框架,封装了 Ribbon 和 RestTemplate,实现了 WebService 的面向接口编程,进一步降低了项目的耦合度。

Feign 内置了 Ribbon,用来做客户端负载均衡调用服务注册中心的服务。

Feign 本身并不支持 SpringMVC 的注解,它有一套自己的注解,为了更方便的使用,SpringCloud 孵化了 OpenFeign。

Feign 是一种声明式、模板化的 HTTP 客户端(仅在 Consumer 中使用)。

Feign 支持的注解和用法请参考官方文档:Spring Cloud OpenFeign

Feign 的使用方式是:使用 Feign 的注解定义接口,调用这个接口,就可以调用服务注册中心的服务。

2、Feign 解决什么问题

Feign 旨在使编写 Java HTTP 客户端变得更加容易,Feign 简化了 RestTemplate 代码,实现了 Ribbon 负载均衡,使代码变得更加简洁,也少了客户端调用的代码,使用 Feign 实现负载均衡是首选方案。只需要你创建一个接口,然后在上面添加注解即可。

Feign 是声明式服务调用组件,其核心就是:像调用本地方法一样调用远程方法,无感知远程 HTTP 请求。

它解决了让开发者调用远程接口就跟调用本地方法一样的体验,开发者完全感知不到这是远程方法,更感知不到这是个 HTTP 请求。无需关注与远程的交互细节,更无需关注分布式环境开发。

它像 Dubbo 一样,Consumer 直接调用 Provider 接口方法,而不需要通过常规的 Http Client 构造请求再解析返回数据。

3、Feign vs OpenFeign

OpenFeign 是 SpringCloud 在 Feign 的基础上支持了 SpringMVC 的注解,如 @RequestMapping,@Pathvariable 等等。

OpenFeign 的 @FeignClient 可以解析 SpringMVC 的 @RequestMapping 注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用服务。

4、Feign 入门案例

Feign 的使用主要分为以下几个步骤:

  • 服务消费者添加 Feign 依赖
  • 创建业务层接口,添加 @FeignClient 注解声明需要调用的服务
  • 业务层抽象方法使用 SpringMVC 注解配置服务地址以及参数
  • 启动类添加 @EnableFeignClients 注解激活 Feign 组件

这里呢,我是用的环境和之前学习 Eureka 时使用的环境一样,需要的朋友还请移步:【SpringCloud】搭建高可用 Eureka 注册中心_风落_的博客-CSDN博客_搭建高可用的eureka

对于 service-consumer 要做一些修改

pom.xml




  4.0.0

  service-consumer
  1.0-SNAPSHOT

  
    org.fengluo
    eureka-demo
    1.0-SNAPSHOT
  

  service-consumer
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
  

  
    
    
      org.springframework.cloud
      spring-cloud-starter-netflix-eureka-client
    

    
    
      org.springframework.boot
      spring-boot-starter-web
    

    
      org.projectlombok
      lombok
    

    
    
      org.springframework.boot
      spring-boot-starter-test
      test
      
        
          org.junit.vintage
          junit-vintage-engine
        
      
    

    
      org.springframework.cloud
      spring-cloud-starter-openfeign
    
  

  
    
      
        
        
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-jar-plugin
          3.0.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
        
        
          maven-site-plugin
          3.7.1
        
        
          maven-project-info-reports-plugin
          3.0.0
        
      
    
  

application.yml

server:
  port: 9090 # 端口

spring:
  application:
    name: service-consumer # 注册名称

# 配置 Eureka Server 注册中心
eureka:
  client:
    registerWithEureka: false # 是否将自己注册到注册中心,默认为 true
    registryFetchIntervalSeconds: 10 # 表示 Eureka Client 间隔多久去服务器拉取信息,默认为 30 秒
    service-url: # 设置服务注册中心地址
      defaultZone: http://root:123456@localhost:8761/eureka/,http://root:123456@localhost:8762/eureka/

实体类

package org.fengluo.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Order {
    private Integer id;
    private String orderNo;
    private String orderAddress;
    private Double totalPrice;
    private List productList;
}
package org.fengluo.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product implements Serializable {

    private Integer id;
    private String productName;
    private Integer productNum;
    private Double productPrice;
}

消费服务

package org.fengluo.service;

import org.fengluo.pojo.Order;

public interface OrderService {
    Order selectOrderById(Integer id);
}
package org.fengluo.service.Impl;

import org.fengluo.pojo.Order;
import org.fengluo.service.OrderService;
import org.fengluo.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private ProductService productService;

    @Override
    public Order selectOrderById(Integer id) {
        return new Order(id, "order-001", "中国", 31994D,
                productService.selectProductList());
    }
}
package org.fengluo.service;

import org.fengluo.pojo.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

// 声明需要调用的服务
@FeignClient("service-provider")
public interface ProductService {

    @GetMapping("/product/list")
    List selectProductList();
}

控制层

package org.fengluo.controller;

import org.fengluo.pojo.Order;
import org.fengluo.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/{id}")
    public Order selectOrderById(@PathVariable("id") Integer id) {
        return orderService.selectOrderById(id);
    }
}

启动类

package org.fengluo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableFeignClients
@SpringBootApplication
public class ServiceConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceConsumerApplication.class);
    }

}

访问

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/891786.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号