OpenFeign入门神级篇,字里行间透露出一种睥(pi)睨天下的气势,你还不首当其冲?

Posted IT挖掘机y

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenFeign入门神级篇,字里行间透露出一种睥(pi)睨天下的气势,你还不首当其冲?相关的知识,希望对你有一定的参考价值。


#Feign简介

1、Feign是什么?

Feign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用Feign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求。

2、Feign有什么用?

Feign是一个声明式的Web Service客户端。它的出现使开发Web Service客户端变得很简单。使用Feign只需要创建一个接口加上对应的注解,比如:FeignClient注解。Feign有可插拔的注解,包括Feign注解和JAX-RS注解。Feign也支持编码器和解码器。

3、OpenFeign和Feign有什么区别?

OpenFeign对Feign进行增强支持Spring MVC注解,可以像Spring Web一样使用HttpMessageConverters等

集成了Ribbon,可以实现客户端的负载均衡

1、框架搭建

使用Eureka作为注册中心 (当然nacos、consul、zookeeper也可以);至于为什么要使用Eureka,你懂的

在这里插入图片描述
不是Eureka的代码量少,只是因为我有现成的代码

首先创建一个父工程,然后创建四个子模块,两个server作为注册中心,provider模块作为服务提供模块,consumer只作为模拟消费模块,不注册进Eureka的注册中心

在这里插入图片描述

2、导入依赖

1、父pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hao</groupId>
    <artifactId>cloud-eureka-demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>cloud-eureka-server</module>
        <module>cloud-eureka-server02</module>
        <module>service-provider</module>
        <module>service-consumer-feign</module>
    </modules>

    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.4.3</version>
    </parent>
    <properties>
        <spring-cloud.version>2020.0.2</spring-cloud.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

2、两个server服务模块pom(相同)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud-eureka-demo</artifactId>
        <groupId>com.hao</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-eureka-server02</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>

</project>

3、provider模块

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud-eureka-demo</artifactId>
        <groupId>com.hao</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>service-provider</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

4、consumer模块

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud-eureka-demo</artifactId>
        <groupId>com.hao</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.feign</groupId>
    <artifactId>service-consumer-feign</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
</project>

3、代码编写

1、两个server模块除了配置文件稍微不一样外,其他都一样
在这里插入图片描述

server:
  port: 8080

spring:
  application:
    name: cloud-eureka-server
  security:
    user:
      name: root
      password: root
eureka:
  instance:
    hostname: eureka01
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
  client:
    service-url:
      defaultZone: http://root:root@127.0.0.1:8081/eureka/
server:
  port: 8081

spring:
  application:
    name: cloud-eureka-server02
  security:
    user:
      name: root
      password: root
eureka:
  instance:
    hostname: eureka02
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}

  client:
    service-url:
      defaultZone: http://root:root@127.0.0.1:8080/eureka/

2、product模块代码编写,框架如下
在这里插入图片描述
product实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product {
    private Integer id;
    private String productName;
    private Integer productNum;
    private Double productPrice;
}

service

@Service
public class ProductServiceImpl implements ProductService {
    @Override
    public List<Product> selectProductList() {
        return Arrays.asList(
                new Product(1, "HuaWei nova3", 100, 2999.0),
                new Product(2, "xiaomi", 99, 1999.0),
                new Product(3, "vivo", 102, 2999.0)
        );
    }
}

application.yml

server:
  port: 7070
spring:
  application:
    name: service-provider

eureka:
  instance:
    hostname: provider
    prefer-ip-address: true
    instance-id: http://${spring.cloud.client.ip-address}:${server.port}

  client:
    service-url:
      defaultZone: http://root:root@127.0.0.1:8080/eureka/,http://root:root@127.0.0.1:8081/eureka/

4、分析OpenFeign使用步骤

1、导入OpenFeign依赖
2、创建接口,添加@FeignClient注解声明调用的服务
3、激活Feign组件,在启动类上添加@EnableFeignClients即可
在这里插入图片描述

1、导入依赖已经完成

2、接口创建

@FeignClient(value = "service-provider")
public interface ProductService {

    @GetMapping(value = "/product/list")
    List<Product> selectProductList();
}
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private ProductService productService;

    @Override
    public Order selectOrderById(Integer id) {
        return new Order(id, "one", "china", 199D, productService.selectProductList());
    }
}
@RestController
public class OrderController {

    @Resource
    private OrderService orderService;

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

3、激活

在这里插入图片描述

4、其他代码

application.yml

server:
  port: 9091

spring:
  application:
    name: service-consumer-feign

eureka:
  client:
    register-with-eureka: false      #是否是将自己注册到注册中心
    registry-fetch-interval-seconds: 20 #Client多久去服务器拉去注册信息 默认30s
    service-url:
      defaultZone: http://root:root@127.0.0.1:8080/eureka/,http://root:root@127.0.0.1:8081/eureka/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Order {
    private Integer id;
    private String orderNo;
    private String orderAddress;
    private Double totalPrice;
    private List<Product> productList;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product {
    private Integer id;
    private String productName;
    private Integer productNum;
    private Double productPrice;
}

5、测试

在这里插入图片描述

在这里插入图片描述
成功!

6、总结

使用OpenFeign进行远程调用还是非常符合我们程序员的习惯的,完全感知不到像在远程调用,帮我们省掉了很多重复的代码。

以上是关于OpenFeign入门神级篇,字里行间透露出一种睥(pi)睨天下的气势,你还不首当其冲?的主要内容,如果未能解决你的问题,请参考以下文章

最适合新手入门的SpringCloud教程 7—OpenFeign「F版本」

Python神级入门书籍推荐,还在绕弯路自学的一定要看看

OpenFeign快速入门

Ribbon为什么要加入点对点直连的功能?如何操作?两句话玩转!

springCloud之openfeign使用入门,Get/Post请求,Feign扩展连接池

Spring Cloud Alibaba入门七:openFeign日志配置