分享

Spring cloud Feign调用外网

 liang1234_ 2020-02-13

使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务,比如调用百度登http接口:

1.对于调用的http,只要能访问即可:

2、访问端,也就是spring-cloud端。

A、在工程启动类名前加入@EnableFeignClients注解:如下图:

  1. @SpringBootApplication
  2. @EnableFeignClients
  3. public class VisualintercomAppServiceApplication extends SpringBootServletInitializer {
  4. }

B、加入依赖:

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

C、调用业务类:

  1. @RestController
  2. @RequestMapping("user")
  3. public class SocketController {
  4. @Autowired
  5. private FeignService feignService;
  6. @RequestMapping(value = "list", method = RequestMethod.GET)
  7. public String getData() {
  8. return feignService.getData();
  9. }
  10. }

D、通过接口结合FeignClient访问外网http接口类。

  1. @FeignClient(name = "baidu", url = "http://192.168.0.194:8082")
  2. public interface FeignService {
  3. @RequestMapping(value = "/feign/data", method = RequestMethod.GET)
  4. public String getData();
  5. }

注意:外网的地址:http://192.168.0.194:8082//feign/data  ,当有参数时在函数体中加入参数:StringgetData(@RequestParam("q") String queryStr);。

名字和IP:端口可在application.properties中配置:

svc.url=http://192.168.0.194:9205
svc.name=scp-videointercom-component

然后在接口中调用如:

  1. @FeignClient(name = "${svc.name}", url = "${svc.url}")
  2. public interface FeignService {
  3. @RequestMapping(value = "/user/list", method = RequestMethod.GET)
  4. public String getData();
  5. }

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多