In the process of developing using springboot, we often need to deal with a scenario where the service is started, the service status needs to be registered with the service registration center (such as zk), so that when the service status changes, failure removal and load balancing can be performed.
I've encountered two ways to register:
1. Register directly after Spring's webapplication is started;
2. After the servlet container is started, register through listener.
This article uses a demo to describe these two registration methods, using the traditional registration scheme with zk.
1. Register after Spring webapplication is started
Let's take a look at the code first
@SpringBootApplicationpublic class WebApplication { private static final Logger logger = LoggerFactory.getLogger(WebApplication.class); private static volatile boolean IS_REGISTRY = false; public static void main(String[] args) { ApplicationContext context = run(WebApplication.class, args); if (IS_REGISTRY) { logger.info("Register 2: After WebApplication starts up"); ZkClient zkClient = context.getBean(ZkClient.class); zkClient.register(); IS_REGISTRY = true; logger.info("Register 2: Registration was successful"); } }}Here, we get the zkClient in WebApplication and register.
It should be noted here that we use ApplicationContext to get the bean of zkClient. The reason is that you cannot inject the bean in the Autowired way during the initialization of the webApplication, because all configurations will be read and the bean is initialized during the startup of the webApplication. You cannot inject the bean before the initialization is completed.
The detailed code for registration will not be expanded here.
2. After the servlet container is initialized, register through listener
Or start the code first
@WebListenerpublic class RegisterListener implements ServletContextListener { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); private static volatile boolean IS_REGISTRY = false; @Autowired private ZkClient zkClient; @Override public void contextInitialized(ServletContextEvent servletContextEvent) { try { if (!IS_REGISTRY) { logger.info("Register 1: After the Servlet container is started successfully"); zkClient.register(); logger.info("Register 1: Register Successfully"); } IS_REGISTRY = true; } catch (Exception e) { IS_REGISTRY = false; logger.info("Register 1: Registration failed"); } } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { if (IS_REGISTRY) { zkClient.stop(); } }}You need to write a listener first. This listener implements the ServletContextListener interface and annotates it with @WebListener. This is the springboot annotation listening method.
After the servlet container is successfully started, the contextInitialized method of the listener will be called. If the servlet container is destroyed and cannot provide services, the contextDestroyed method of the listener will be called. In other words, this listener is listening to the state of the servlet container.
Then you just need to open the listener configuration in the application main class.
@ServletComponentScan@SpringBootApplicationpublic class WebApplication {}3. Comparison of these two methods
For a web service that provides the http protocol externally, the registration of the servlet container will be clearer in semantics. However, if your spring container starts too long, the servlet initialization may be completed and registered, but the service cannot provide access to the gap time externally, so I generally use the first method to register.
This scenario is like this
It can be seen that after the servlet registration is successful, the webapplication has not been started yet, and the service cannot provide access normally at this time.
You can see on zk that both registrations have been successful.
Summarize
The above is a detailed explanation of the springboot registration service registration center (zk) introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!