使用IDEA开发Spring Cloud项目(五)创建服务消费者(Feign)

概述

Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果

简而言之:

  • Feign 采用的是基于接口的注解
  • Feign 整合了ribbon,具有负载均衡的能力

本系列源码地址:https://github.com/lhmyy521125/toher-springcloud-sample

准备工作

依旧复制一份第一个工程项目,取名为 toher-springcloud-sample3-feign

  • 启动eureka-server工程
  • 启动服务提供者 service-hello,端口号为:9001
  • 修改配置文件的端口号为:9002,启动后在 Eureka 中会注册两个实例,这相当于一个小集群

创建服务消费者Feign

按照上一章节我创建service-ribbon子工程的模式,还是继续创建一个module , 取名为:service-feign;在它的pom.xml继承了父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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>cn.toher</groupId>
        <artifactId>springcloud-sample</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>cn.toher</groupId>
    <artifactId>service-feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>service-feign</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <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>
    </dependencies>

</project>

在工程的配置文件application.yml文件,指定程序名为service-feign,端口号为9004,服务注册地址为http://localhost:8761/eureka/ ,代码如下:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 9004
spring:
  application:
    name: service-feign

在程序的启动类通过 @EnableFeignClients 注解开启 Feign 功能

package cn.toher.servicerfeign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {

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

定义一个feign接口,通过@ FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了service-hello服务的“/hello”接口,代码如下:

package cn.toher.servicerfeign.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @Author: 同恒科技-李怀明
 * @Date: 2019/10/19 15:26
 */

@FeignClient(value = "service-hello")
public interface HelloFeignService {
    @RequestMapping(value = "/hello")
    String helloFeign(@RequestParam(value = "name") String name);
}

创建测试用的 Controller

package cn.toher.servicerfeign.controller;

import cn.toher.servicerfeign.service.HelloFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: 同恒科技-李怀明
 * @Date: 2019/10/19 10:58
 */
@RestController
public class HelloFeignController {

    @Autowired
    private HelloFeignService helloFeignService;

    @RequestMapping("/helloFeign")
    public String helloFeign(String name){
        return helloFeignService.helloFeign(name);
    }
}

运行该工程,查看注册中心(下图) 证明我们已经创建完成,接下来测试一下;
在这里插入图片描述

浏览器中输入:http://localhost:9004/helloFeign?name=feign , 会发现浏览器输出信息中端口号的变化,证明成功

hi feign ,this is Feign Project , i am from port:9001
hi feign ,this is Feign Project , i am from port:9002

结语:本章我们学习了如何使用Feign实现服务与服务的通讯并实现负载均衡,大家可以根据博主的代码样例,进行相关测试学习;

下一篇:使用熔断器Hystrix

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Micro麦可乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值