centos7--制作java微服务为系统服务!
Posted FindSoul
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos7--制作java微服务为系统服务!相关的知识,希望对你有一定的参考价值。
背景
公司项目使用java构建,服务器使用linux--centos7,项目中使用了众多微服务快速成型,随着时间推移,xxx.jar 每次通过命令启动比较繁琐,本着轻松懒的方式,将这些服务通通制作成系统级别的服务,也就是"systemctl start service_name" ,本人使用python语言,通过python批量制作所有java服务
代码
#coding:utf-8 #author:fei #date:2020-5-26 import os import sys import subprocess class JavaSystemServices(object): def __init__(self, params): self.__dict__.update(params) if params else None self.config = dict(self.__dict__) self.system_service_path = "/usr/lib/systemd/system" if self.config and len(self.config) is 5: self.service_name = self.config.get("service_name", "") self.platform_name = self.config.get("platform_name", "") self.sh_path = self.config.get("sh_path", "") self.java_path = self.config.get("java_path", "") self.java_run_command = self.config.get("java_run_command", "") else: sys.exit("not args or not enough kwargs") def service_pre_start(self): command = "systemctl start {}.service".format(self.service_name) try: subprocess.check_call(command, shell=True) status = True except Exception: status = False return status def run(self): if self.service_pre_start(): print "{}启动成功".format(self.service_name) else: try: self.create_service() self.create_sh() os.system("systemctl daemon-reload") os.system("systemctl enable {}.service".format(self.service_name)) print "{} 服务创建成功,请测试!!!".format(self.service_name) except Exception as e: print e def create_service(self): full_path = os.path.join(self.system_service_path, "{}.service".format(self.service_name)) with open(full_path, ‘w‘) as f: f.write("#!/bin/sh ") f.write("[Unit] ") f.write("Description={}-service {} ".format(self.platform_name, self.service_name)) f.write("After=syslog.target network.target remote-fs.target nss-lookup.target ") f.write("[Service] ") f.write("Type=forking ") f.write("ExecStart={}/{}-service/{}/{}-start.sh ".format(self.sh_path, self.platform_name, self.service_name,self.service_name)) f.write("ExecStop={}/{}-service/{}/{}-stop.sh ".format(self.sh_path, self.platform_name, self.service_name,self.service_name)) f.write("PrivateTmp=true ") f.write("[Install] ") f.write("WantedBy=multi-user.target") def create_sh(self): if not os.path.exists(self.sh_path): os.makedirs(self.sh_path) sh_full_path = os.path.join(self.sh_path, "{}-service".format(self.platform_name), self.service_name) if not os.path.exists(sh_full_path): os.chdir(self.sh_path) os.makedirs("{}/{}-service/{}".format(self.sh_path,self.platform_name,self.service_name)) start_sh_path = os.path.join(sh_full_path, "{}-start.sh".format(self.service_name)) stop_sh_path = os.path.join(sh_full_path, "{}-stop.sh".format(self.service_name)) service_pid_path = os.path.join(sh_full_path, "{}-service.pid".format(self.service_name)) with open(start_sh_path, "w") as f: f.write("#!/bin/sh ") f.write("export JAVA_HOME={} ".format(self.java_path)) f.write("export PATH=$JAVA_HOME/bin:$PATH ") f.write(self.java_run_command + " ") with open(stop_sh_path, "w") as f: f.write("#!/bin/sh ") f.write("PID=$(cat {}) ".format(service_pid_path)) f.write("kill -9 $PID") with open(service_pid_path,"w") as f: pass os.system("chmod +x {}".format(start_sh_path)) os.system("chmod +x {}".format(stop_sh_path)) def yilian_main(java_path): java_services_info = [ { "service_name": "config-manager", "platform_name": "yilian", "sh_path": "/usr/local/apps", "java_path": java_path, "java_run_command": "nohup java -Dnutz.boot.configure.properties.dir=/opt/jar/configmanager/manager -Dnutz.profiles.active=pro -jar /opt/jar/configmanager/configmanager.jar> /dev/null 2>&1 &" }, { "service_name": "gateway-api", "platform_name": "yilian", "sh_path": "/usr/local/apps", "java_path": java_path, "java_run_command": "nohup java -jar -Xms1024m -Xmx1024m -Dspring.config.location=/opt/jar/gateway-api/application-gateway-api.yml /opt/jar/gateway-api/gateway-api-server.jar >/dev/null 2>&1 &" }, { "service_name": "realtime-dataview", "platform_name": "yilian", "sh_path": "/usr/local/apps", "java_path": java_path, "java_run_command": "nohup java -jar -Xms900m -Xmx900m -Dspring.config.location=/opt/jar/realtime-dataview/application-dataview.properties /opt/jar/realtime-dataview/realtime-dataview.jar >/dev/null 2>&1 &" }, { "service_name": "realtime", "platform_name": "yilian", "sh_path": "/usr/local/apps", "java_path": java_path, "java_run_command": "nohup java -jar -Xms1024m -Xmx1024m -Dspring.config.location=/opt/jar/realtime/application-realtime.properties /opt/jar/realtime/realtime.jar >/dev/null 2>&1 &" }, { "service_name": "login", "platform_name": "yilian", "sh_path": "/usr/local/apps", "java_path": java_path, "java_run_command": "nohup java -jar -Xms900m -Xmx900m -Dspring.config.location=/opt/jar/login/application-login.properties /opt/jar/login/login.jar>/dev/null 2>&1 &" }, { "service_name": "alert-strategy", "platform_name": "yilian", "sh_path": "/usr/local/apps", "java_path": java_path, "java_run_command": "nohup java -jar -Xms1024m -Xmx1024m -Dspring.config.location=/opt/jar/alertStrategy/application-strategy.yaml /opt/jar/alertStrategy/alertstrategy.jar >/dev/null 2>&1 &", }, { "service_name": "alert-pusher", "platform_name": "yilian", "sh_path": "/usr/local/apps", "java_path": java_path, "java_run_command": "nohup java -jar -Xms1024m -Xmx1024m -Dspring.config.location=/opt/jar/alertPusher/application-pusher.yaml /opt/jar/alertPusher/alertpusher.jar >/dev/null 2>&1 &", }, { "service_name": "alert-engine", "platform_name": "yilian", "sh_path": "/usr/local/apps", "java_path": java_path, "java_run_command": "nohup java -jar -Xms1024m -Xmx1024m -Dspring.config.location=/opt/jar/alertEngine/application-engine.yaml /opt/jar/alertEngine/alertengine.jar >/dev/null 2>&1 &" }, { "service_name": "background", "platform_name": "yilian", "sh_path": "/usr/local/apps", "java_path": java_path, "java_run_command": "nohup java -jar -Xms1024m -Xmx1024m -Dspring.config.location=/opt/jar/background/application-background-interface.yaml /opt/jar/background/background.jar>/dev/null 2>&1 &" }, { "service_name": "realtime", "platform_name": "yilian", "sh_path": "/usr/local/apps", "java_path": java_path, "java_run_command": "nohup java -jar -Xms1024m -Xmx1024m -Dspring.config.location=/opt/jar/realtime/application-realtime.properties /opt/jar/realtime/realtime.jar >/dev/null 2>&1 &" }, ] return java_services_info if __name__ == ‘__main__‘: sh_path = "/usr/local/apps" test_server8_java_path = "usr/local/java" # 172.16.16.8测试服务器 java 安装环境 cloud_server_java_path = "usr/lib/jvm/java" # 公有云服务器 java 安装环境 guowang_test_java_path = "/usr/local/java/jdk1.8" # 私有云 java 安装环境 for item in yilian_main(guowang_test_java_path): jss = JavaSystemServices(item) jss.run()
以上是关于centos7--制作java微服务为系统服务!的主要内容,如果未能解决你的问题,请参考以下文章
架构大迁移:从Java Spring到ReactJS +API微服务架构