我们要实现一个微服务监控平台修改配置文件的功能
配置问文件在修改完之后就需要重启才能生效,就有了这个需要
下面是实现的步骤:
在注册在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接口来实现服务的重启了