zoukankan      html  css  js  c++  java
  • <Spring Cloud>入门四 Feign

    1.Feign

      之前使用的是Ribbon+RestTemplate调用,通过的是微服务的名字进行调用,实现负载均衡

      

      但是为了满足接口编程,提供了Feign

    2.实现

    2.1引入坐标

      在 ms-common-api 和 ms-consumer-dept-80-feign 引入坐标

            <!--feign 客户端负载均衡-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>

    2.2 在 ms-common-api 实现调用

    package org.maple.service;
    
    import org.maple.entity.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 org.springframework.web.bind.annotation.RequestBody;
    
    import java.util.List;
    
    /**
     * @author mapleins
     * @Date 2019-01-12 23:10
     * @Desc 通过接口和注解 面向接口编程访问微服务
     **/
    @FeignClient("ms-provider-dept")
    public interface DeptClientService {
    
        @PostMapping("/dept/add")
        boolean add(@RequestBody Dept dept);
    
        @GetMapping("/dept/get/{id}")
        Dept get(@PathVariable("id") Long id);
    
        @GetMapping("/dept/list")
        List<Dept> list();
    
    }

    2.3修改 dept-80-feign的启动类

    package org.maple;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    /**
     * @author mapleins
     * @Date 2019-01-09 20:26
     * @Desc
     **/
    @SpringBootApplication
    @EnableEurekaClient 
    @EnableFeignClients(basePackages = {"org.maple.service"}) //扫描ms-common-api中的service包
    public class App_Consumer_Dept_80_Feign {
    
        public static void main(String[] args) {
            SpringApplication.run(App_Consumer_Dept_80_Feign.class, args);
        }
    }

    2.4 controller层

    这样就相当于不是去调用微服务去编程,而是通过controller调用service层,实现接口编程,并且自带ribbon的轮询算法

    package org.maple.controller;
    
    import org.maple.entity.Dept;
    import org.maple.service.DeptClientService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    
    /**
     * @author mapleins
     * @Date 2019-01-09 20:10
     * @Desc
     **/
    @RestController
    public class DeptController_Consumer {
    
        @Autowired
        private DeptClientService service;
    
        @GetMapping("/consumer/dept/get/{id}")
        public Dept get(@PathVariable("id") Long id){
            return service.get(id);
        }
    
        @GetMapping("/consumer/dept/list")
        public List<Dept> list(){
            return service.list();
        }
    
        @PostMapping("/consumer/dept/add")
        public boolean add(Dept dept){
            return service.add(dept);
        }
    
    }
  • 相关阅读:
    Bash Shellshock(CVE-2014-6271)破壳漏洞测试
    极客时间-左耳听风-程序员攻略-分布式架构经典图书和论文
    极客时间-左耳听风-程序员攻略-分布式架构入门
    极客时间-左耳听风-程序员攻略-数据库
    极客时间-左耳听风-程序员攻略-Java底层知识
    Hackertarget:一款发现攻击面的工具
    解决tomcat was unable to start within问题
    当我们安装使用时,会出现eclipse启动不了,出现“Java was started but returned exit code=13......”的问题
    Eclipse启动报错:A java runtime Environment(JRE) or java Development……的解决办法
    eclipse svn插件安装方法
  • 原文地址:https://www.cnblogs.com/mapleins/p/10261520.html
Copyright © 2011-2022 走看看