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

    假设提供者有如下服务接口方法

    @RestController
    @RequestMapping("/person")
    public class PersonController {
        @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
        public Person get(@PathVariable Integer id) {
            Person p = new Person();
            p.setId(id);
            p.setName("name" + id);
            return p;
        }
    }

    服务调用者端 pom.xml加入依赖

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

    在服务调用者端启动类开启feign

    @SpringBootApplication
    @EnableEurekaClient
    @EnableFeignClients
    public class Application {
        public static void main(String[] args) {
            new SpringApplicationBuilder(Application.class).web(true).run(args);
        }
    }

    在服务调用者端编写一个PersonClient.java

    //声明调用的服务名称
    @FeignClient("my-provider") 
    public interface PersonClient {    
        @RequestMapping(method = RequestMethod.GET, value = "/person/get/{personId}")
        Person getPerson(@PathVariable("personId") Integer id);
    }

    在服务调用者端增加一个测试TestController调用提供者

    @Autowired
    private PersonClient personClient;
    
    @GetMapping("/personGetFeignTest")
    @ResponseBody
    public Person personGetFeignTest() {
        return personClient.getPerson(100);
    }

    启动注册中心 、提供者、调用者

    访问调用者的personGetFeignTest url 测试是否可以成功调用提供者服务

    转载于:https://www.cnblogs.com/zengnansheng/p/10389838.html

  • 相关阅读:
    Array的应用
    事物的操作
    定义集合
    wxWidgets 在 Linux 下开发环境配置
    Emacs 中 GDB 的使用
    wxWidgets 在 Windows 下开发环境配置
    Ubuntu14.04终端主机名+用户名修改配色方案
    S5PV210之内外存学习
    Ubuntu14.04进行配置符号链接arm2009q3.tar.bz2
    Ubuntu14.041+VMware12.0NET方式网卡连接虚拟机联网问题解决方法
  • 原文地址:https://www.cnblogs.com/twodog/p/12135177.html
Copyright © 2011-2022 走看看