SpringCloud之旅(三)之声明式服务调用Feign

  • Feign的作用:项目中会有很多的服务间的调用,对于服务的调用不可能是一处。所以我们针对各个服务自行封装一些客户端类来包装这些依赖服务的调用,SpringCloud Feign就在这个基础上做了封装,他可以帮助我们定义接口。开发时候只需要定义接口并添加相应的注解,大大的简化了使用ribbon的开发量
  • 项目开始搭建,开始由上一篇文章的项目作为基本开发。
  • 环境搭建添加依赖
 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</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>

创建项目的时候可以直接由SpringCloud提示中去创建
1、确定
在这里插入图片描述
2、点击next
在这里插入图片描述
3、修改包名称和相关信息
4、在查询按钮里面直接查找到需要的东西
在这里插入图片描述
如上提示在这里插入图片描述

  • 现在开始对项目的编辑
  • 首先是配置文件的编辑application.properties
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
server.port=7001
spring.application.name=feign-service

如果没有代码提示,说明你有些依赖没有写入,需要去添加,这里本人踩过的许多坑,提示一下

  • 启动的FeignApplication类
package com.example.feign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class FeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(FeignApplication.class, args);
    }

}

  • 新建一个servicepackage,创建ClientService接口
package com.example.feign.Service;

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

@FeignClient(value = "client1")
public interface ClientService {

    @RequestMapping(value = "/user/findById",method = RequestMethod.GET)
    String findById(@RequestParam("id") String id);
}

`
  • 创建一个controller package,创建一个ClientController 类
package com.example.feign.controller;

import com.example.feign.Service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class ClientController {
    @Autowired
    ClientService clientService;

    @RequestMapping(value = "/user/findById",method = RequestMethod.GET)
    public String findById(@RequestParam("id") String id) {
        return clientService.findById(id);
    }
}


  • 依次运行Eureka,两个client,和刚刚创建的Feigen
  • 访问
http://localhost:7001//user/findById?id=wl
  • 显示
    在这里插入图片描述
  • 执行着之前的策略,默认是随机的策略

  • 开启Feign里面的断路器,作用在于,当访问某个client的时候出了异常,能够及时断开,执行处理操作。
  • 配置文件中,修改如下
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
server.port=7001
spring.application.name=feign-service

feign.hystrix.enabled=true
  • 创建Service的实现类,记得@Component添加,不加会报错,加载不到这个类,因为没有放进容器里面
package com.example.feign.Service;

import org.springframework.stereotype.Component;

@Component
public class ClientFallback implements ClientService{

    @Override
    public String findById(String id) {
        return "调用Client1服务超时,调用方法findById(id)-根据id查询所有记录"+id;
    }
}

  • service接口修改如下,添加fallback
package com.example.feign.Service;

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

@FeignClient(value = "client1",fallback = ClientFallback.class)
public interface ClientService {
    @GetMapping(value = "/user/findById")
    String findById(@RequestParam("id") String id);
}


-接着重启Feign

  • 输入
http://localhost:7001//user/findById?id=wl

在这里插入图片描述

  • 当停掉客户端1时。
    在这里插入图片描述
    断路机制,直接显现出来。
    附上本次项目的GitHub地址

SpringCloudFeign

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值