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);
    }
}

 

posted @ 2020-06-19 20:51  徐徐图之  阅读(1308)  评论(0编辑  收藏  举报