In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to realize the registration service in springboot". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to implement the registration service in springboot".
In the process of using springboot for development, we often need to deal with scenarios where the service status needs to be registered with the service registry (such as zk) when the service is started, so that fault removal and load balancing can be performed when the service state changes.
I have come across two ways to register:
1. Register directly after the webapplication of Spring is started.
2. After the servlet container is started, register through listener.
This article describes these two registration methods through a demo, using the traditional scheme of registering with zk.
1. Register after Spring webapplication startup is completed
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 ("Registration 2: after WebApplication startup"); ZkClient zkClient = context.getBean (ZkClient.class); zkClient.register (); IS_REGISTRY = true; logger.info ("Registration 2: registration succeeded");}
Here, we get the zkClient in WebApplication and register it.
We need to note here that we use ApplicationContext to obtain the bean of zkClient. The reason is that you cannot inject Bean as Autowired during the initialization of webApplication, because all configuration will be read and bean will be initialized during the startup of webApplication. You cannot inject bean until initialization is completed.
The detailed code for registration will not be expanded here.
2. After the initialization of the servlet container, register through listener.
Still put 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 ("Registration 1: after Servelet Container starts successfully"); zkClient.register (); logger.info ("Registration 1: registration successful");} IS_REGISTRY = true } catch (Exception e) {IS_REGISTRY = false; logger.info ("Registration 1: registration failed");} @ Override public void contextDestroyed (ServletContextEvent servletContextEvent) {if (IS_REGISTRY) {zkClient.stop ();}
You need to write a listener, which implements the ServletContextListener interface and annotates it with @ WebListener, which is springboot annotated listener writing.
After the servlet container starts successfully, the contextInitialized method of the listener is called. If the servlet container is destroyed and cannot provide services, the contextDestroyed method of the listener is called. In other words, the listener is listening for 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. The comparison of these two ways
For a web service that provides http protocol, the registration of the servlet container will be clearer semantically, but if your spring container takes too long to start, it may occur that the servlet initialization is completed and has already been registered, but the service cannot provide access to the gap time, so I usually use the first method to register.
This scenario goes like this.
As you can see, when the servlet registration is successful, the webapplication has not yet been started, and the service cannot provide access normally at this time.
As you can see on zk, both registrations have been successful.
At this point, I believe you have a deeper understanding of "how to achieve registration services in springboot". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.