Home  >  Article  >  Java  >  How to implement registration service in springboot

How to implement registration service in springboot

WBOY
WBOYforward
2023-05-12 12:28:061467browse

In the process of using springboot for development, we often need to deal with such scenarios: when 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, faults can be removed and load balancing.

I have encountered two ways to register:

1. Register directly after the Spring webapplication is started;

2. After the servlet container is started, register it through the listener.

This article uses a demo to describe these two registration methods, using the traditional registration scheme with zk.

1. Register after the Spring webapplication is started.

Let’s take a look at the code first.

@SpringBootApplication
public 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("注册2: WebApplication启动完成后");
  ZkClient zkClient = context.getBean(ZkClient.class);
  zkClient.register();
  IS_REGISTRY = true;
  logger.info("注册2: 注册成功");
 }
 }
}

Here, we get the zkClient in WebApplication and register it.

One thing to note here is that we use ApplicationContext to obtain the bean of zkClient. The reason is that you cannot use Autowired to inject beans during the initialization process of webApplication, because all configurations are read during the startup process of webApplication. And the bean initialization is completed. You cannot inject the bean before initialization is completed.

The detailed code for registration will not be expanded here.

2. After the servlet container is initialized, register it through a listener.

Still write the code first.

@WebListener
public 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("注册1: Servelet容器启动成功后");
  zkClient.register();
  logger.info("注册1: 注册成功");
  }
  IS_REGISTRY = true;
 } catch (Exception e) {
  IS_REGISTRY = false;
  logger.info("注册1: 注册失败");
 }
 }
 @Override
 public void contextDestroyed(ServletContextEvent servletContextEvent) {
 if (IS_REGISTRY) {
  zkClient.stop();
 }
 }
}

You need to write a listener first, which implements the ServletContextListener interface. , and annotate it with @WebListener, which is springboot's annotated listener writing method.

After the servlet container is successfully started, the contextInitialized method of this 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 monitoring the status of the servlet container.

Then you only need to open the listener configuration in the application main class.

@ServletComponentScan
@SpringBootApplication
public class WebApplication {
}

3. Comparison of these two methods

For a web service that provides http protocol to the outside world, the registration of the servlet container will be semantically clearer, but if your spring container starts If the time is too long, there may be a gap in time when the servlet initialization is completed and registered, but the service cannot provide external access, so I usually use the first method to register.

This scenario is like this

How to implement registration service in springboot

You can see that after the servlet is successfully registered, the webapplication has not actually been started yet, and the service cannot be normal at this time. Provide access.

How to implement registration service in springboot

You can see on zk that the two registrations have been successful.

The above is the detailed content of How to implement registration service in springboot. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete