Article directory
1. Batch file registration as a service
1.1 Application scenarios
The jar package under the microservice architecture is usually started through the command line java -jar -Dfile.encoding=utf-8 %JAVA_OPTS% gateway.jar
. The problem is that a console window will appear, and in order to ensure that the service is available, the console window must be opened all the time. In addition, the service needs to be manually started every time it is turned on. This is not very user friendly.
You can write commands into bat
a batch file, and then register the batch file as a Windows service, and set it to start automatically at boot. In this way, the service can be started automatically at startup, and at the same time, there is no console window to prevent users from accidentally closing it.
1.2 Operation steps
1.2.1 Write batch files;
Example: Run_Gateway.bat
@echo off
echo.
echo [INFO] Startup gateway server
echo.
set JAVA_OPTS=-Xms512m -Xmx1024m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m
java -jar -Dfile.encoding=utf-8 %JAVA_OPTS% gateway.jar
pause
1.2.2 Register the batch file as a service with instsrv+srvany;
For instsrv and srvany, please refer to the bottom link 1 .
- Copy instsrv.exe and srvany.exe to
C:\Tools
the directory - Run cmd as an administrator and switch to the C:\Tools directory
- Run the command:
instsrv MyService C:\Tools\srvany.exe
Note:Myservice
It is the name of the customized service, which can be changed arbitrarily according to the application name
Run successfully!
configuration
- Open the registry: (enter in cmd:
regedit
) - ctrl+F, search
Myservice
(previously customized service name) - Right-click
Myservice
to create a new item namedParameters
- Then create a few new ones in Parameters
字符串值
- Name Application Value: The address of the program you want to run as a service.
- Name AppDirectory Value: The path to the folder where the program you want to run as a service is located.
- Name AppParameters Value: The parameters you need to start the program that you want to run as a service.
Then start the service Myservice
to run the bat file in the background
1.2.3 Set dependencies between services;
Refer to Chapter 2 to configure dependencies between services
1.2.4 Start Test
After the setting is complete, you can manually start it once in the service manager, and then it will start automatically every time the computer is turned on.
2. Configure dependencies between services
2.1 Application scenarios
There are dependencies between some services, or there is a sequential startup sequence, such as services Run_Gateway
must be started after the database service MySQL
is started. At this time, you need to Run_Gateway
add dependency configuration to the service.
2.2 Operation steps
- run cmd as administrator;
- Excuting an order
sc config "Run_Gateway" depend= "MySQL"
Indicates that the Run_Gateway service depends on the MySQL service.