JAVA服务优雅停服务

      
package com.dst.user.index;

import com.dst.steed.common.util.DstSpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.StopWatch;

import java.text.DecimalFormat;

import static com.dst.user.index.DSTUserIndexApplication.PACKAGE_NAME;

/**
 * @Description: 服务启动
 * @author: liu wei ping
 * @date: 2023/07/25
 */
@Slf4j
@SpringBootApplication(scanBasePackages = {PACKAGE_NAME})
@EnableFeignClients(basePackages = PACKAGE_NAME + ".infrastructure.acl")
@MapperScan(basePackages = {PACKAGE_NAME + ".infrastructure.biz.**.mapper", PACKAGE_NAME + ".modules.business.**.mapper"})
public class UserIndexApplication implements CommandLineRunner, ApplicationContextAware {

    public static final String PACKAGE_NAME = "com.dst.user.index";

    @Autowired
    private ConfigurableApplicationContext applicationContext;

    public static void main(String[] args) {
        System.setProperty("nacos.logging.default.config.enabled","false");
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        SpringApplication.run(DSTUserIndexApplication.class, args);
        stopWatch.stop();
        log.info("【服务:" + DstSpringUtil.getAppName() +
                ";环境:" + DstSpringUtil.getActiveProfile() +
                "】启动成功,耗时:" +
                new DecimalFormat("#.##").format(stopWatch.getTotalTimeSeconds()) + " 秒。");
    }

    /**
     * kill -15时执行改钩子函数
     */
    @Override
    public void run(String... args) {
        log.info("启动成功!");
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            log.info("服务关闭");
            applicationContext.close();
        }));
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        if (applicationContext instanceof ConfigurableApplicationContext) {
            this.applicationContext = (ConfigurableApplicationContext) 
            applicationContext;
        }
    }
}


    

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值