温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringCloud的OpenFeign项目怎么创建

发布时间:2022-01-12 09:28:35 阅读:146 作者:iii 栏目:大数据
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章主要讲解了“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@EnableFeignClientspublic 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 */@RestControllerpublic 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项目怎么创建这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

原文链接:https://my.oschina.net/u/3555122/blog/4355940

AI

开发者交流群×