SpringCloud的OpenFeign项目怎么创建

这篇文章主要讲解了“SpringCloud的OpenFeign项目怎么创建”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“SpringCloud的OpenFeign项目怎么创建”吧!

什么是OpenFeign

OpenFeign是声明式的Http客户端,通过OpenFeign发送Http请求非常的简单

  • 注解式开发,接口+注解的方式
  • OpenFeign支持多种的对象的序列化 和 反序列化的工具
  • OpenFeign默认集成了Ribbon,可以直接进行负载均衡

Feign 和 OpenFeign是两个技术,都是作为服务调用存在的,OpenFeign 是SpringCloud在Feign的基础上进行封装得到的,支持SpringMvc的注解。

创建OpenFeign的项目

1.创建新Module项目 cloud-openfeign-8806

2.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-demo-20f</artifactId>
       <groupId>com.lby</groupId>
       <version>1.0-SNAPSHOT</version>
   </parent>
   <modelVersion>4.0.0</modelVersion>

   <artifactId>cloud-openfeign-8806</artifactId>

   <dependencies>
<!--        openfeign-->
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-openfeign</artifactId>
       </dependency>

       <!--        Eureka 客户端的依赖-->
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
       </dependency>

       <!--        web的依赖-->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>

       <!--        测试的依赖-->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-test</artifactId>
           <scope>test</scope>
       </dependency>
   </dependencies>
</project>
 

3.启动类

package com.lby;

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

/**
* @EnableFeignClients OpenFeign的注解
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class OpenFeignRun8806 {
   public static void main(String[] args) {
       SpringApplication.run(OpenFeignRun8806.class,args);
   }

}

 

4.配置文件

server:
 port: 8806

#指定当前服务的名称  会注册到注册中心
spring:
 application:
   name: eureka-openfeign-8806

#  指定 服务注册中心的地址
eureka:
 client:
   serviceUrl:
     defaultZone: http://localhost:8801/eureka,http://localhost:8800/eureka
 

通过四步我们就拥有了一个最初步的项目,接下来,我们会通过接口+注解的方式开发OpenFeign的服务调用。

 
OpenFeign接口的开发

OpenFeign的开发方式:接口+注解,微服务调用的接口+@FeignClient 类似于Dao接口的开发方式(Mybatis接口式开发),之前我们开发Dao接口,在接口上添加@Mapper的注解就可以获取一个Mybatis的接口

1.业务逻辑接口+@FeignClient创建OpenFeign服务调用

package com.lby.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
* @author luxiaoyang
* @create 2020-04-05-19:41
*
* @FeignClient 参数是要请求服务的服务名称
*/
@Component
@FeignClient(value = "eureka-client-8803")
public interface ConsumerFeignService {

   /**
    * 接口中的方法是被调用服务的Controller接口
    */
   @GetMapping("showImgById")
   String showImgById(@RequestParam("id") Integer id);
}

 

接口的书写规则如下图所示:

SpringCloud的OpenFeign项目怎么创建

2.在消费者中创建一个ConsumerController使用OpenFeign接口

package com.lby.controller;

import com.lby.service.ConsumerFeignService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
* @author luxiaoyang
* @create 2020-04-05-19:47
*/
@RestController
public class ConsumerController {
   /**
    * 直接装配OpenFeign的服务调用接口
    */
   @Resource
   private ConsumerFeignService consumerFeignService;

   @GetMapping("testOpenFeign")
   public String testOpenFeign(){
       String s = consumerFeignService.showImgById(1);
       return "Feign服务调用的结果为:"+s;
   }
}

   

启动项目

启动注册中心,服务提供者(两个),以及Feign服务

SpringCloud的OpenFeign项目怎么创建

 

请求eureka-openfeign-8806的Controller接口:http://127.0.0.1:8806/testOpenFeign

可以看到两次请求都能够获取服务提供者的响应,并且能够负载均衡

SpringCloud的OpenFeign项目怎么创建

 

SpringCloud的OpenFeign项目怎么创建

    

感谢各位的阅读,以上就是“SpringCloud的OpenFeign项目怎么创建”的内容了,经过本文的学习后,相信大家对SpringCloud的OpenFeign项目怎么创建这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程之家,小编将为大家推送更多相关知识点的文章,欢迎关注!

相关文章

Nacos 中的参数有很多,如:命名空间、分组名、服务名、保护...
Nacos 支持两种 HTTP 服务请求,一个是 REST Template,另一...
Nacos 是 Spring Cloud Alibaba 中一个重要的组成部分,它提...
Spring Cloud Alibaba 是阿里巴巴提供的一站式微服务开发解决...
在 Nacos 的路由策略中有 3 个比较重要的内容:权重、保护阈...
前两天遇到了一个问题,Nacos 中的永久服务删除不了,折腾了...