从实战的角度谈微服务(三):基于Feign服务间的调用

一、简介

Feign 是一个声明web服务客户端,使用Feign 创建一个接口并对它进行注解,具有可插拔的注解支持包括Feign注解与JAX-RS注解,Feign还支持可插拔的编码器与解码器,

Spring Cloud 增加了对 Spring MVC的注解,Spring Web 默认使用了HttpMessageConverters, Spring Cloud 集成 Ribbon 和 Eureka 提供的负载均衡的HTTP客户端 Feign。

二、配置步骤

主要分三步:

  • 依赖包引入
  • 配置文件修改
  • 启动类添加注解

三、依赖包引入

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

</dependency>

四、配置文件修改

server:
  port:8765

spring:
  application:
    name:service-feign

eureka:

  client:

    serviceUrl:

      defaultZone:http://localhost:8761/eureka/

五、修改项目启动类

application启动类添加注解

@EnableEurekaClient
@EnableDiscoveryClient(或者@EnableFeignClients)

六、创建接口调用

创建一个service接口,通过@FeignClient("服务名"),以及@RequestMapping(value="服务提供url",method=url类型(RequestMethod.GET等))。

当前controller调用service接口类,即可实现远程服务调用。

至此,Feign的服务间调用基础配置完成

猜你喜欢

转载自www.cnblogs.com/lovechengyu/p/9474934.html
今日推荐