Spring-Cloud中通过Feign调用外网HTTP网络通信方法
来源:互联网 发布:出现无法安装java 编辑:程序博客网 时间:2024/06/05 17:28
使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务,比如调用百度登http接口:
1.对于调用的http,只要能访问即可:
2、访问端,也就是spring-cloud端。
A、在工程启动类名前加入@EnableFeignClients注解:如下图:
@SpringBootApplication
@EnableFeignClients
public class VisualintercomAppServiceApplication extends SpringBootServletInitializer {
@EnableFeignClients
public class VisualintercomAppServiceApplication extends SpringBootServletInitializer {
B、加入依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
C、调用业务类:
@RestController
@RequestMapping("user")
public class SocketController {
@Autowired
private FeignService feignService;
@RequestMapping(value = "list",method = RequestMethod.GET)
public String getData() {
return feignService.getData();
}
}
@RequestMapping("user")
public class SocketController {
@Autowired
private FeignService feignService;
@RequestMapping(value = "list",method = RequestMethod.GET)
public String getData() {
return feignService.getData();
}
}
E、通过接口结合FeignClient访问外网http接口类。
@FeignClient(name = "baidu",url = "http://192.168.0.194:8082")
public interface FeignService {
@RequestMapping(value = "/feign/data", method = RequestMethod.GET)
public String getData();
}
public interface FeignService {
@RequestMapping(value = "/feign/data", method = RequestMethod.GET)
public String getData();
}
注意:外网的地址: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
svc.name=scp-videointercom-component
然后在接口中调用如:
@FeignClient(name = "${svc.name}",url = "${svc.url}")
public interface FeignService {
@RequestMapping(value = "/user/list", method = RequestMethod.GET)
public String getData();
}
public interface FeignService {
@RequestMapping(value = "/user/list", method = RequestMethod.GET)
public String getData();
}
阅读全文
0 0
- Spring-Cloud中通过Feign调用外网HTTP网络通信方法
- 【Spring Cloud】Feign接口调用
- Spring Cloud中声明式服务调用Feign
- Spring Cloud中如何优雅的使用Feign调用接口
- spring cloud feign 调用服务注意问题
- Spring Cloud 声明式服务调用 Feign
- 使用Spring Cloud Feign远程调用
- 声明式服务调用:Spring Cloud Feign
- spring cloud Feign(声明式服务调用)
- Spring Cloud之Feign调用token丢失
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- Spring Cloud中,Feign常见问题总结
- Spring Cloud中,Feign常见问题总结
- Spring Cloud中Feign的继承特性
- 第19节项目5-我的加班费
- c++用宏定义来编写函数#define
- 学习笔记8
- linux常用命令
- phpQuery和simple_html_dom DOM解析器对比
- Spring-Cloud中通过Feign调用外网HTTP网络通信方法
- Hystrix生产环境线程池自动扩容与缩容的动态资源分配经验
- 八个维度,谈谈产品经理的分类与发展方向
- Android样式 layer-list
- 杭电acm P2004成绩转换
- GUI
- 统计学习方法(二)
- Date、String、Calendar类型之间的转化
- ⚔疯狂输出⚔ StringBuffer 和 权限修饰符的作用