千家信息网

SpringCloud的OpenFeign项目怎么创建

发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,这篇文章主要讲解了"SpringCloud的OpenFeign项目怎么创建",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"SpringCloud的Ope
千家信息网最后更新 2024年09月22日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文件导入依赖


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">

cloud-demo-20f
com.lby
1.0-SNAPSHOT

4.0.0

cloud-openfeign-8806




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




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




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




org.springframework.boot
spring-boot-starter-test
test



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);
}

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

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服务

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

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

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

0