SpringBoot开发之采用Feign进行第三方网络请求

1、在maven项目中添加依赖

1
2
3
4
5
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>

2、编写接口,放置在service层

这里的decisionEngine.url 是配置在properties中的 是ip地址和端口号
decisionEngine.url=http://10.2.1.148:3333/decision/person 是接口名字

1
2
3
4
5
6
@FeignClient(url = "${decisionEngine.url}",name="engine")
public interface DecisionEngineService {
  @RequestMapping(value="/decision/person",method= RequestMethod.POST)
  public JSONObject getEngineMesasge(@RequestParam("uid") String uid,@RequestParam("productCode") String productCode);

}

3、在Java的启动类上加上@EnableFeignClients

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@EnableFeignClients //参见此处
@EnableDiscoveryClient
@SpringBootApplication
@EnableResourceServer
public class Application implements CommandLineRunner {
private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
@Autowired
private AppMetricsExporter appMetricsExporter;

@Autowired
private AddMonitorUnitService addMonitorUnitService;

public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}

4、在代码中调用接口即可

1
2
3
4
@Autowired
private DecisionEngineService decisionEngineService ;
// ...
decisionEngineService.getEngineMesasge("uid" , "productCode");

本文为作者原创 转载时请注明出处 谢谢

乱码三千 – 点滴积累 ,欢迎来到乱码三千技术博客站

0%