前言
大三的上学期实训被老师鄙视了,他总觉得我们现在阶段还是把java的框架和java底层学好就万事大吉了,还说男生最好还是走后端,女孩最擅长前端,问他关于java爬虫的东西,他说他不会;问他python爬虫的东西,他说他不会;问他关于docker的东西,他说他不会;问他关于安全方面的东西,他说他不会。(那你还会啥吧?)。总结起来就是他感觉我的格局很低,总是在问一些现在企业中的东西,而且还只问不做,手高眼低!以后肯定是个打工(搬砖)的!(哪个大佬或老板在还没出名之前不是个打工人啊!!!瞧不起打工人还是咋滴???!)
我又想起了刚开始学javaweb的时候,当时我感觉javaweb都这么难了,那 c 的 web 和 c++ 的 web 岂不是更难?再来个什么什么语言的 web ,程序员还活不活了???特别的听学长讲,在校学java出门就转go(虽然我不知道go到底干吗的,但是它也有web吧)。
当时我就又在想既然java可以这样写接口,那么其他的语言肯定也可以这样写接口,但是接口写多了自己的电脑或服务器暴露的端口也多啊!特别是微服务那块,这样就总感觉不安全吧。
于是吧,学完微服务我就又有这样的想法(xxx:"你tm想法真多!"):python擅长爬虫和深度学习,那我就用python写个爬虫然后交给java的微服务去控制。
于是吧,这篇博客就诞生了!!!
1. 创建一个微服务叫 sidecar
2. 添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-sidecar</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
3. 修改yml
server:
port: 9007
spring:
application:
name: sidecar
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://127.0.0.1:9001/eureka/,http://127.0.0.1:9002/eureka/
feign:
hystrix:
enabled: true
sidecar:
port: 5590 #python服务的端口
health-uri: http://localhost:${sidecar.port}/health
# hostname: localhost
# ip-address: 127.0.0.1 #python的webapi的ip,多网卡的服务器需要指明ip
ribbon:
ConnectTimeout: 50000
ReadTimeout: 50000
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 10000
4. 修改启动类
@SpringBootApplication
@EnableEurekaClient
@EnableSidecar
public class SidecarApplication {
@Bean
// @LoadBalanced //使用@LoadBalanced注解后,RestTemplate 可以直接调用服务名
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(SidecarApplication.class);
}
}
from flask import Flask, request, Response
import json
sss = Flask(__name__)
@sss.route("/health")
def health():
result = {'status': 'UP'}
return Response(json.dumps(result), mimetype='application/json')
@sss.route('/index/<int:id>', methods=['GET'])
def getId(id):
argsJson = json.loads(str(id))
result = json.dumps(argsJson, ensure_ascii=False) # 转化为字符串格式
print(result)
return result
if __name__ == "__main__":
sss.run(host='0.0.0.0', port=5590)
5. 使用RestTemplate
进行操作
@RestController
public class PythonsController {
@Autowired
private RestTemplate restTemplate;
@GetMapping(value = "/index/{id}")
public String getIdByRestTemplate(@PathVariable int id) {
return restTemplate.getForObject("http://localhost:5590/index/" + id, String.class);
}
}
访问 http://127.0.0.1:9007/index/555
![](https://img.haomeiwen.com/i20206173/1caad1c727af32b2.png)
![](https://img.haomeiwen.com/i20206173/5f6633f3738ae757.png)
6. 使用feign远程访问
6.1. 写PythonClient.java
@FeignClient(value = "sidecar", configuration = PythonClientImpl.class)
//value ="sidecar代理程序在配置文件中spring.application.name属性的值"
public interface PythonClient {
@GetMapping(value = "/index/{id}") //这个是python的那个接口
public String getId(@PathVariable int id);
}
6.2. 写PythonClientImpl.java
@Configuration
@EnableFeignClients(basePackages = "sidecar.client")
public class PythonClientImpl {
public String returnError() {
return "远程调用出现了问题,触发了熔断器";
}
}
6.3. 写Controller
@RestController
public class PythonsController {
@Autowired
private PythonClient pythonClient;
@GetMapping(value = "/getId/{id}")
public String getId(@PathVariable int id) {
return pythonClient.getId(id);
}
}
访问 http://127.0.0.1:9007/index/123456789
![](https://img.haomeiwen.com/i20206173/2beaaac18b6a6610.png)
![](https://img.haomeiwen.com/i20206173/ee80a259a0269fc5.png)
6. 其实我感觉只要把接口的参数写好,那么在任何远程的情况下都应该可以调用和使用!
网友评论