• 微服务-实现服务重启


    我们要实现一个微服务监控平台修改配置文件的功能

    配置问文件在修改完之后就需要重启才能生效,就有了这个需要

    下面是实现的步骤:

    在注册在Eureka中的服务中:

    (1)修改启动类

    package com.googosoft.instances;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
    import org.springframework.context.ConfigurableApplicationContext;
    
    @SpringBootApplication(exclude = {SecurityAutoConfiguration.class })
    public class InstancesApplication 
    {    
         public static String[] args;
            public static ConfigurableApplicationContext context;
         
            public static void main(String[] args) {
                InstancesApplication.args = args;
                InstancesApplication.context = SpringApplication.run(InstancesApplication.class, args);
            }
    }

    (2)添加外部接口

    package com.googosoft.instances.controller;
    
    import java.util.concurrent.ArrayBlockingQueue;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.SpringApplication;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import com.googosoft.instances.InstancesApplication;
    
    /** 
    * @author songyan 
    * @version 2020年1月20日 上午9:29:29 
    * @desc
    */
    @RestController
    public class RestartController {
    
        @RequestMapping("/reStart")
        public String restart() {
            ExecutorService threadPool = new ThreadPoolExecutor(1, 1, 0,
                    TimeUnit.SECONDS, new ArrayBlockingQueue<>(1), new ThreadPoolExecutor.DiscardOldestPolicy());
            threadPool.execute(() -> {
                InstancesApplication.context.close();
                InstancesApplication.context = SpringApplication.run(InstancesApplication.class,
                        InstancesApplication.args); 
            });
            threadPool.shutdown();
            return "spring.application.name:" + name;
        }
     
    }

    这样就可以通过各个服务的reStart接口来实现服务的重启了

  • 相关阅读:
    windows10 应用商店(Microsoft store)进不去
    Java中System函数
    人生的智慧叔本华
    第2关:文本串里单词、数字和符号的识别
    C/C++语言编写PL/0编译程序的词法分析程序
    编译原理实践
    vanced 无法登录问题
    @bizresubmit
    《道德经》全文(翻译 )
    道德经第十章
  • 原文地址:https://www.cnblogs.com/excellencesy/p/12217073.html
Copyright © 2020-2023  润新知