Spring Cloud Feign组件

Feign简介

Feign是一个声明式的Web服务客户端,使得它写Web服务变得简单。使用Feign,只需要创建一个接口并注解,它具有可插拔的注解特性。Feign基于Netflix Feign实现,整合了Spring Cloud Ribbon与Spring Cloud Hystrix,除了提供这两者的强大功能之外,它还提供了一种声明式的Web服务客户端定义方式。下面我们来看一下如何使用。

前期准备

使用Feign之前,我们需要有一个Eureka服务注册中心,端口号:9003,参考:Spring Cloud 微服务Eureka服务注册与发现

构建项目

要想使用Feign调用服务提供者,首先我们要有服务提供者的项目,创建一个SpringBoot项目,勾选 Web和Eureka Discovery:
在这里插入图片描述
pom.xml中有如下两个依赖说明ok

 <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-client</artifactId>
</dependency>

启动类加注解: @EnableEurekaClient

@SpringBootApplication
@EnableEurekaClient
public class TestPdrApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestPdrApplication.class, args);
    }

}

编写配置文件,我这里采用的是YML格式:

spring:
  application:
    # 设置服务名称
    name: test-pdr
eureka:
  client:
    service-url:
      # 设置注册中心的地址
      defaultZone: http://admin:123456@114.116.49.12:9003/eureka/
      # admin是我们设置的用户名,123456为密码,
server:
  port: 9009

然后新建一个service包,创建一个TestService类:

package com.test.pdr1.testpdr.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: 马旭辉
 * @Date: 2019/5/12 17:23
 * @Description:
 */
@RestController
public class TestService {
    private static final Logger log = LoggerFactory.getLogger(TestService.class);

    /**
     * 创建一个接口实现,供服务消费者调用,
     * 如果传的参数为对象的话,使用@RequestBody注解
     *
     * @param text
     * @return
     */
    @GetMapping("/hello")
    public String hello(@RequestParam(name = "text") String text) {
        return "这里是服务提供者的接口实现: 参数为:" + text;
    }
}

然后我们测试一下,将服务提供者注册到服务注册中心,启动项目,访问服务注册中心查看:
在这里插入图片描述
可以看到服务提供者已经成功注册到服务注册中心了,也就是说已经这个服务已经在注册中心进行了登记。
服务提供者创建编写完成之后创建服务消费者,调用服务提供者的服务,创建一个新的SpringBoot项目,勾选 Web,Eureka Discovery,Feign依赖:

在这里插入图片描述
pom.xml中的三个依赖如下:

<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-client</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

下一步编写application.yml配置文件:

spring:
  application:
    # 设置服务名称
    name: test-csm
eureka:
  client:
    service-url:
      # 设置注册中心的地址
      defaultZone: http://admin:123456@127.0.0.1:9003/eureka/
      # admin是我们设置的用户名,123456为密码,
server:
  port: 9010

在启动类加入注解 @EnableEurekaClient@EnableFeignClients

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class TestCsmApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestCsmApplication.class, args);
    }

}

然后新建一个service包,包里创建一个接口:

package com.test.csm.testcsm.service;

import com.test.csm.testcsm.TestCsmApplication;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @Auther: 马旭辉
 * @Date: 2019/5/12 17:52
 * @Description:
 */
 //name是要调用的服务提供者在服务注册中心注册的名字
@FeignClient(name = "TEST-PDR", fallback = TestCsmApplication.class)
public interface TestService {
	//直接复制服务提供者的service方法即可,不需要实现,
    @GetMapping("/hello")
    public String hello(@RequestParam(name = "text") String text);
}

接着再新建一个controller包,新建一个controller类:

package com.test.csm.testcsm.controller;

import com.test.csm.testcsm.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: 马旭辉
 * @Date: 2019/5/12 17:57
 * @Description:
 */
@RestController
public class TestController {

    @Autowired
    private TestService testService;

    //编写一个service方法
    @GetMapping("/hello")
    public String hello(String text) {
        //调用service
        return this.testService.hello(text);
    }
}

启动该项目,然后到服务注册中心查看是否注册成功:
在这里插入图片描述
提示有两个服务,说明服务消费者也注册成功了,那么接下来进行测试:
在这里插入图片描述
我们可以看到测试结果是非常成功的,参数也成功的传递过来了,这样就实现了服务的远程调用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值