FeginClent中接收的格式需要和调用方法所返回的格式保持一致。测试的过程中,可以通过填写Url参数进行制定FeginClent的接口调用地址,进行方法的调用。反正用着就是简单。
@FeginClent()参数说明
注入定义的FeginClent调用方法
@RestController
@RequestMapping('/test')
public class TestCoreController {
@Autowired
FeginClientTest feginClientTest;
@RequestMapping('/test2')
public Map test2(){
Map map=new HashMap<>();
map.put('test1','test');
map.put('test2','test2');
Map test = feginClientTest.getTest(map);
return test;
}
}
编写FeginClecnt方法调用的接口
name:指定FeignClient的名称,eurake中注册的名称url:url一般用于调试,可以手动指定@FeignClient调用的地址decode404:当发生http404错误时,如果该字段位true,会调用decoder进行解码,否则抛出FeignExceptionconfiguration:Feign配置类,可以自定义Feign的Encoder、Decoder、LogLevel、Contractfallback:定义容错的处理类,当调用远程接口失败或超时时,会调用对应接口的容错逻辑,fallback指定的类必须实现@FeignClient标记的接口fallbackFactory:工厂类,用于生成fallback类示例,通过这个属性我们可以实现每个接口通用的容错逻辑,减少重复的代码path:定义当前FeignClient的统一前缀
@FeignClient(name = 'osback')
public interface FeginClientTest {
@RequestMapping('/backtest/test3')
Map getTest(@RequestBody Map map);
}
启动类开启FeginClent
@EnableFeignClients
public class CoreApplication {
public static void main(String[] args) {
SpringApplication.run(CoreApplication.class, args);
}
}
在微服务架构的项目开发中,模块之间调用方法,除了之前的RestTemplate方式,FeginClient也是一种模块间方法调用的方式。在SpringCloudFeign的实现下,我们只需创建一个接口并用注解的方式来配置它,即可完成对服务提供方的接口绑定,简化了在使用SpringCloudRibbon时自行封装服务调用客户端的开发量。调用的方法所涉及的服务必须在Eurake中注册。
引入Maven依赖
org.springframework.cloud
spring-cloud-starter-openfeign
需要调用的接口是OsBack模块中的如下的方法
@RequestMapping('/backtest')
@RestController
public class TestController extends BaseController {
@RequestMapping('/test3')
public Map getTest(@RequestBody Map map){
return map;
}
}
文章为作者独立观点,不代表股票交易接口观点