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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM