zoukankan      html  css  js  c++  java
  • springcloud 使用feign

    一,被调用方 web-test

    spring:
      application:
        name: web-test

    二,web-test准备接口

    package com.tenyears.webTest.controller;
     
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
     
    @RestController
    @RequestMapping("api/test")
    public class TestController {
     
     
        @RequestMapping(value = "test1", method = RequestMethod.POST)
        public String test1() {
            return "hello";
        }
     
    }

    三,调用方web-hello

    POM

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

    四,Feign

    @FeignClient(value="服务名")

    package com.tenyears.webAD.feign;
     
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
     
    /**
     * @description :
     * @auther Tyler
     * @date 2021/6/15
     */
     
    @FeignClient(value = "web-test")
    public interface WebTestFeign {
        @RequestMapping(value = "/api/test/test1", method = RequestMethod.POST)
        String test1();
    }

    Main方法添加 @EnableFeignClients

    @EnableFeignClients
    public class WebADApp {
        public static void main( String[] args )
        {
            SpringApplication.run(WebADApp.class, args);
     
        }
    }

    五,测试接口

    package com.tenyears.webAD.controller;
     
    import com.tenyears.webAD.feign.WebTestFeign;
    import com.tenyears.webAD.service.service.AdScheduleService;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
     
    /**
     * @author Tyler
     * @date 2019/4/16
     */
     
    @RestController
    @RequestMapping("api/test")
    public class TestController {
        Logger logger = LoggerFactory.getLogger(TestController.class);
     
        @Autowired
        WebTestFeign webTestFeign;
     
     
        @RequestMapping(value = "test1", method = RequestMethod.POST)
        public String test1() {
            String str=webTestFeign.test1();
            return str;
        }
     
    }
  • 相关阅读:
    dategrid快速录入一行数据的一波操作
    shiro权限控制入门
    Activiti工作流小序曲
    在线HTML文档编辑器使用入门之图片上传与图片管理的实现
    ActiveMQ整合spring结合项目开发流程(生产者和消费者)总结
    缓存框架EhCache的简单使用
    关于特征工程入门中的一些基本知识
    SQL SERVER常用系统表和常用函数
    TestOne
    JS 代码调试经验总结
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/14886309.html
Copyright © 2011-2022 走看看