应用迁移中遇到一些有特殊要求的应用,比如需要通过交互生成一些新的config文件,然后启动应用需要依赖于这些文件,这样在构建镜像的时候基本上是没有办法把这些文件固定的,因为他需要根据运行环境去进行动态注册生成,目前的解决方法如下:
1.写一段支持交互的python脚本,生成配置
#!/usr/bin/python import subprocess import time import fcntl import os p = subprocess.Popen(["java","-jar","a.jar"],stdin=subprocess.PIPE,stdout=subprocess.PIPE) flags = fcntl.fcntl(p.stdout,fcntl.F_GETFL) fcntl.fcntl(p.stdout,fcntl.F_SETZFL,flags| os.O_NONBLOCK) time.sleep(2) print p.stdout.read() p.stdin.write("1 ")
开始调试时屏幕一直处于blocking状态,后来同事高手指导下加入NONBLOCK后才通过。
2.将python脚本放入tomcat运行的catalina.sh,让他在启动容器后,启动脚本一开始就运行
================================================
另外在应用迁移的时候遇到的另一个问题
代码类似
Cookie cookie = new Cookie("testCookie", "test"); cookie.setDomain(".test.com"); cookie.setPath("/"); cookie.setMaxAge(36000); resp.addCookie(cookie);
在tomcat 8.5的环境下报错
java.lang.IllegalArgumentException: An invalid domain [.test.com] was specified for this cookie at org.apache.tomcat.util.http.Rfc6265CookieProcessor.validateDomain(Rfc6265CookieProcessor.java:181) at org.apache.tomcat.util.http.Rfc6265CookieProcessor.generateHeader(Rfc6265CookieProcessor.java:123) at org.apache.catalina.connector.Response.generateCookieString(Response.java:989) at org.apache.catalina.connector.Response.addCookie(Response.java:937) at org.apache.catalina.connector.ResponseFacade.addCookie(ResponseFacade.java:386) at com.cml.mvc.controller.HelloWorld.str(HelloWorld.java:98) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85
原因是 在tomcat8.5上是使用org.apache.tomcat.util.http.Rfc6265CookieProcessor
在tomcat8.0上使用的是org.apache.tomcat.util.http.LegacyCookieProcessor
解决办法是去掉域名前面的.
Cookie cookie = new Cookie("testCookie", "test"); cookie.setDomain("test.com"); cookie.setPath("/"); cookie.setMaxAge(36000); resp.addCookie(cookie);