• spring could之—— 内部接口调用


    疫情期间这段时间,发现自己好久没有写文章了,今天来说说,spring could内部接口调用。
     
    目标:同个在注册中心注册的服务名进行内部系统间的调用,而无关关注具体服务提供方的实例ip,需要先完成注册中心的接入。
     
    配置
     
    1.修改配置文件[pom.xml] 添加依赖库
      <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
       </dependency>
    2.修改配置文件[src/main/resources/application.properties],根据自己需要,添加请求超时配置
     
    ribbon.ReadTimeout=120000
    ribbon.ConnectTimeout=120000
     
    代码逻辑
     
    1.新增服务调用接口类[com.xiaoniu.demo.service.DemoCallService],这里以调用自己的SimpleController为例。
     
    import org.springframework.cloud.netflix.feign.FeignClient;
    import org.springframework.http.MediaType;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
     
    /**
    * 内部服务调用的示例接口服务类
    * 注解中的demo就是在注册中心注册的服务名
    * 为了方便,这里用来自己,其实用别的名字也一样
    *
    */
    @FeignClient("demo")
    public interface DemoCallService {
     
        /**
         * 使用GET形式访问SimpleController中提供的接口
         * @param name
         * @return
         */
        @RequestMapping(value ="/demo/simple/echo", method= RequestMethod.GET)
        String callGet(@RequestParam("name") String name);
     
        /**
         * 使用POST按照urlencoded进行提交
         * @param name
         * @return
         */
        @RequestMapping(value ="/demo/simple/echo", method= RequestMethod.POST)
        String callPostParams(@RequestParam("name") String name);
     
        /**
         * 使用POST在body中提交
         * @param body
         * @return
         */
        @RequestMapping(value ="/demo/simple/postOnly", method= RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
        String callPostBody(@RequestBody String body);
    }
    2.新增测试的Controller类,InternalCallController
     
    import javax.servlet.http.HttpServletRequest;
     
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
     
    import com.xiaoniu.demo.service.DemoCallService;
     
    /**
    * 内部接口调用示例
    * @author
    *
    */
    @RestController
    public class InternalCallController {
        @Autowired
        private DemoCallService demoCallService;
     
        @RequestMapping(value="/callGet")
        public String callGet(HttpServletRequest request) {
            return demoCallService.callGet("callGet");
        }
     
        @RequestMapping(value="/callPostParams")
        public String callPostParams(HttpServletRequest request) {
            return demoCallService.callPostParams("callPostParams");
        }
     
        @RequestMapping(value="/callPostBody")
        public String callPostBody(HttpServletRequest request) {
            return demoCallService.callPostBody("{"key":"abc"}");
        }
    }
    3.修改程序入口类DemoApplication,添加@EnableFeignClients注解
     
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.cloud.netflix.feign.EnableFeignClients;
     
    /**
    * 启动类
    * @author
    *
    */
    @SpringBootApplication
    @EnableEurekaClient
    @EnableFeignClients     
    public class DemoApplication {
     
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
  • 相关阅读:
    (转载)windows下安装配置Xampp
    (转载)C# winform 在一个窗体中如何设置另一个窗体的TextBox的值
    (转载)winform图片标尺控件
    (转载)WinformGDI+入门级实例——扫雷游戏(附源码)
    (转载)c# winform 窗体起始位置 设置
    (转载)C# GDI+ 画简单的图形:直线、矩形、扇形等
    (转载)C# ListView用法详解
    (转载)C#工具箱Menustrip控件中分割线的设置方法
    计算机中英文术语对照
    [JAVA]多线程之实现Callable接口
  • 原文地址:https://www.cnblogs.com/haoliyou/p/9628915.html
Copyright © 2020-2023  润新知