zoukankan      html  css  js  c++  java
  • 微服务-实现服务重启

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

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

    下面是实现的步骤:

    在注册在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接口来实现服务的重启了

  • 相关阅读:
    23种设计模式
    doraemon的python Flask框架 websocket和redis
    doraemon的python Flask框架 路由和配置
    doraemon的python Flask框架 安装以及基础应用
    doraemon的python centos的入门(五)用户和用户组权限
    doraemon的python centos的入门(四)查询和压缩文件、文件夹
    doraemon的python centos的入门(三)vim
    doraemon的python centos的入门(二)文件目录操作
    doraemon的python centos的入门(一)增删改查命令
    doraemon的python CRM项目中公户与私户转换、搜索条件的应用
  • 原文地址:https://www.cnblogs.com/excellencesy/p/12217073.html
Copyright © 2011-2022 走看看