• SpringCloud04-Feign 负载均衡


    Feign 负载均衡

    简介

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

    只需要创建一个接口,然后添加注解即可!

    feign主要是社区,大家都习惯面向接口编程,这个是很多开发人员的规范,调用微服务访问的两种方法

    ​ 1.微服务名字【Ribbon】

    ​ 2.接口和注释【feign】

    Feign能干什么?

    • Feign旨在使编写java Http客户端变得更加容易
    • 前面使用Ribbon+RestTemplate时,利用RestTemplate对Http请求的封装处理,形成了一套模板化的调用方法。但是在实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端来包装这些依赖服务的调用。所以,Feign在此基础上做了一进一步封装,有他来帮助我们定义和实现依赖服务接口的定义,在Feign的实现下,我们只需要创建一个接口并使用注解的方式来配置它(类似于Dao层接口上标注Mapper注解,现在是一个微服务接口上标注一个Feign注解即可)即可完成对服务调用提供方的接口绑定,简化了使用SpringCloud Ribbon时,自动封装服务调用客户端的开发量。(但是性能变低了,只是可读性变好了)

    ​ 1)导入依赖

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

    ​ 2)添加feign服务(接口)

    package com.mjh.springcloud.service;
    
    import com.mjh.springcloud.pojo.Dept;
    import org.springframework.cloud.openfeign.FeignClient;
    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;
    
    @FeignClient(value = "SPRINGCLOUD-PRIVIDER-DEPT")
    public interface DeptClientService {
        @GetMapping("/dept/add")
        public Dept addDept(Dept dept);
        @GetMapping("/dept/queryById/{id}")
        public Dept queryById(@PathVariable("id") Long id);
        @PostMapping("/dept/list")
        public Dept queryAll();
    }
    

    ​ 3)让客户端调用feign接口

    package com.mjh.springcloud.controller;
    
    import com.mjh.springcloud.pojo.Dept;
    import com.mjh.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 org.springframework.web.client.RestTemplate;
    
    import java.util.List;
    
    @RestController
    public class DeptConsumerController {
    
        @Autowired
        private DeptClientService Service=null;
    
       @RequestMapping("/consumer/dept/add")
       public boolean add(Dept dept){
           return  this.Service.addDept(dept);
       }
    
        @RequestMapping("/consumer/dept/queryById/{id}")
        public Dept queryById(@PathVariable("id") Long id){
           //服务端给你什么方法你就调用什么方法
            return  this.Service.queryById(id);
        }
    
        @RequestMapping("/consumer/dept/list")
        public List<Dept> list(){
            return this.Service.queryAll();
        }
    }
    

    ​ 4)把刚才写的feign扫进来

    @SpringBootApplication
    @EnableEurekaClient  //Eureka客户端
    @EnableFeignClients(basePackages={"com.mjh.springcloud"})
    @ComponentScan("com.mjh.springcloud")
    public class FeignDeptConsumer_80 {
        public static void main(String[] args) {
            SpringApplication.run(FeignDeptConsumer_80.class,args);
        }
    }
    
  • 相关阅读:
    Winform Treeview 的按需加载
    Dynamic CRM 2013学习笔记(十)客户端几种查询数据方式比较
    Dynamic CRM 2013学习笔记(九)CrmFetchKit.js介绍:Fetchxml、多表联合查询, 批量更新
    Dynamic CRM 2013学习笔记(八)过滤查找控件 (类似省市联动)
    iOS Programming
    Hello Socket
    解决ARC的循环引用问题
    解决Eclipse下不自动拷贝apk到模拟器问题( The connection to adb is down, and a severe error has occured)
    解决Android NDK 报jxxx编译找不到
    做一个创建cocos2d-x新项目的shell脚本
  • 原文地址:https://www.cnblogs.com/mjjh/p/13392935.html
Copyright © 2020-2023  润新知