SpringCloud-Feign使用接口方式调用服务、负载均衡

前言

什么是Feign

Feign是声明式的web sevice客户端,它让微服务之间的调用变得更加简单了,类似于controller调用service。SpringCloud集成了Ribbon和Eureka,可在使用Feign提供负载均衡的http客户端。

调用微服务访问有两种方法:

  1. 微服务名字(Ribbon)
    我们在Ribbon中,客户端从注册中心获取服务就是通过访问微服务的名字来获取的。
    在这里插入图片描述
  2. 接口和注解(Feign)
    我们只需要创建一个接口并使用注解的方式来配置它(类似于以前Dao接口上标注Mapper注解,现在是一个微服务接口上面标注一个Feign注解)
  3. Feign集成了Ribbon

使用Feign接口方式调用服务

导包

在接口服务中导入feign的依赖

  <!--feign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

在接口服务中编写Service层

在这里插入图片描述

DeptClientService

package com.cjh.springcloud.service;

import com.cjh.springcloud.pojo.Dept;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@Component
@FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT")
public interface DeptClientService {

    @GetMapping("/dept/get/{id}")
    public Dept queryById(@PathVariable("id") Long id);

    @GetMapping("/dept/list")
    public List<Dept> queryAll();

    @PostMapping("/dept/add")
    public boolean addDept(Dept dept);
}

这里我们使用Feign的注解即可完成调用。
使用@Component让它可以被Spring调用
使用@FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT")让他被Feign识别。

消费者调用服务

我们创建一个新的module
springcloud-consumer-dept-feign
在这里插入图片描述
用这个来区别之前用RestTemplate的方式调用服务。
我们只需要修改Controller的内容
DeptConsumerController

package com.cjh.springcloud.controller;

import com.cjh.springcloud.pojo.Dept;
import com.cjh.springcloud.service.DeptClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;

@RestController
public class DeptConsumerController {


    @Autowired
    private DeptClientService deptClientServic = null;


    @RequestMapping("/consumer/dept/add")
    public boolean add(Dept dept) {
        return this.deptClientServic.addDept(dept);
    }

    @RequestMapping("/consumer/dept/list")
    public List<Dept> list() {
        return this.deptClientServic.queryAll();
    }


    @RequestMapping("/consumer/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id) {
       return  this.deptClientServic.queryById(id);
    }

}

这样就是面向接口编程了,非常的简洁明了。

对比之前的:

package com.cjh.springcloud.controller;

import com.cjh.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class DeptConsumerController {

    //理解:消费者,不应该有service层
    //RestTemplate
    //(url,实体:Map,Class<T> responseType)
    @Autowired
    private RestTemplate restTemplate; //提供多种便捷访问远程http服务的方法,简单的restful服务模板

    //Ribbon,这里的地址应该是一个变量,我们应该通过服务名来访问
    //private static final String REST_URL_PREFIX= "http://localhost:8081";
    private static final String REST_URL_PREFIX= "http://SPRINGCLOUD-PROVIDER-DEPT";


    @RequestMapping("/consumer/dept/add")
    public boolean add(Dept dept){
        return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add",dept,Boolean.class);
    }

    @RequestMapping("/consumer/dept/list")
    public List<Dept> list(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list",List.class);
    }


    @RequestMapping("/consumer/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id,Dept.class);
    }
}

很明显,使用Feign接口方式来调用服务更加简介、简单,这也是开发人员的规范。

启动类配置

我们要把Feign开启,只需要添加一个注解@EnableFeignClients即可,需要配置要扫描的包
在这里插入图片描述

package com.cjh.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"com.cjh.springcloud"})
public class FeignDeptConsumer_80 {
    public static void main(String[] args) {
        SpringApplication.run(FeignDeptConsumer_80.class,args);
    }
}

注意:import org.springframework.cloud.openfeign.EnableFeignClients;
导入的路径是这一个。

总结

使用Feign之后,代码可读性变高了,但是性能变低了,原因封装多了一层东西。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

叫我菜菜就好

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

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

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

打赏作者

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

抵扣说明:

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

余额充值