在使用springboot框架編寫java代碼時(shí)蝉娜,有時(shí)需要引用到python腳本唱较,相關(guān)操作如下:
步驟
一、將.py文件放到項(xiàng)目的resources目錄下
二召川、在application-xxx.properties中寫文件路徑
python.create.yaml=/opt/create_yaml.py
python.run.test=/opt/run_test.py
三南缓、constants文件定義路徑變量
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class PythonConstants {
@Value("${python.create.yaml}")
public String pythonCreateYaml;
@Value("${python.run.test}")
public String pythonRunTest;
}
四、service層進(jìn)行引用
@Resource
private PythonConstants pythonConstants;
String s = pythonConstants.pythonCreateYaml;
五荧呐、在應(yīng)用部署的時(shí)候搭建python環(huán)境汉形,并將腳本放到應(yīng)用運(yùn)行路徑下
# Dockerfile文件中內(nèi)容
FROM 100000000/java:latest
ARG variable=project_dev
ENV CLUSTER_NAME=$variable
RUN cd /opt/
ADD application* /opt/
ADD project* /opt/
ADD init.sh /opt/
ADD create_yaml.py /opt/
ADD run_test.py /opt/
ADD health_check.sh /opt/
RUN yum install python3 -y
RUN pip3 install xxx==x.x.x
HEALTHCHECK --interval=10s --timeout=3s --retries=5 CMD /bin/bash /opt/health_check.sh
CMD ["/bin/bash","/opt/init.sh"]
六、應(yīng)用部署成功后進(jìn)入應(yīng)用容器中
cd /opt
該路徑下有create_yaml.py倍阐、run_test.py文件概疆,且容器中配置了python3環(huán)境并安裝了相關(guān)的包。