zoukankan      html  css  js  c++  java
  • SpringCloud之Feign

    以我个人写的博客系统为例,请求其它微服务API。

    一、添加Maven依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>

    二、启动类增加@EnableFeignClients

    package com.springcloud.blog;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    @SpringBootApplication
    @EnableEurekaClient
    @EnableFeignClients
    public class BlogAdminApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(BlogRibbonClientApplication.class, args);
        }
    
       
    }

    三、编写服务接口类

    package com.springcloud.blog.example;
    
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    @FeignClient("blog-api")
    public interface FeignTestService {
    
    
         @GetMapping("/juhe/getIpInfo")
         String getIpInfo(@RequestParam String ip);
    
    }

    特别注意:

    @FeignClient(“blog-api”),其中的blog-api必须是在Eureka Server注册的服务实例。

    而对应的@RequestMapping的请求类型必须要写准确,Get请求就写Get,Post请求就写Post,这是其一。

    还有就是请求参数是地址栏的形式还是请求体的形式也必须写清楚(记住必须要与对应的微服务URL保持一致,否则请求会出现问题),使用@RequestParam或@RequestBody或@PathVariable的形式写清楚,这是其二。

    四、编写Controller

    package com.springcloud.blog.example;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping(value = "/test")
    public class TestController {
    
    
        @Autowired
        FeignTestService feignTestService;
    
        @GetMapping(value="/getIp")
        public String getIp(String ip){
    
            return feignTestService.getIpInfo(ip);
        }
    }

    五、测试

  • 相关阅读:
    JAVA 解压压缩包中指定文件或实现压缩文件的预览及下载单个或多个指定的文件
    java 解压zip java.lang.IllegalArgumentException: MALFORMED 错误
    Java文件管理系统
    Java实现用汉明距离进行图片相似度检测的
    url 编码(percentcode 百分号编码)
    nodejs进程管理
    nodejs网络编程
    nodejs内存溢出 FATAL ERROR: CALL_AND_RETRY_0 Allocation failed – process out of memory
    connect ECONNREFUSED 127.0.0.1:80错误解决
    nuxtjs如何在单独的js文件中引入store和router
  • 原文地址:https://www.cnblogs.com/youcong/p/13066686.html
Copyright © 2011-2022 走看看