Subject: Spring boot Integrated dubbo nacos druid realizes dynamic switching of data sources
I. experimental purpose
Spring boot Integrated dubbo nacos druid realizes dynamic data source switching. After changing the configuration of nacos, it can dynamically switch data sources without restarting the springboot application.
two Implementation solutions
Principle analysis: after spring boot integrates druid data source, we can
DruidDataSource master = SpringUtil.getBean(DruidDataSource.class);
1. Get the current DruidDataSource object,
2. Through @ nacosvalue, we can get the configuration changes in nacos and get the changed connection information immediately.
3. Use The restart() method provided by the DruidDataSource object can directly reset the database connection information, so as to achieve dynamic data source switching.
4. The last step is when to execute the restart() method. Time is very important and necessary. By reading the source code of nacos dubbo integration, you can find that there is an annotation for event listening
three Achieve the most important step 4.
use @ NacosConfigListener Annotation to monitor the corresponding configuration changes. At this time, you can switch the connection information of the database. The specific implementation code is as follows.
package com.study.cloud.springbootdubbo; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.nacos.api.annotation.NacosInjected; import com.alibaba.nacos.api.config.ConfigChangeEvent; import com.alibaba.nacos.api.config.annotation.NacosConfigListener; import com.alibaba.nacos.api.config.annotation.NacosValue; import com.alibaba.nacos.spring.context.event.config.NacosConfigEvent; import com.alibaba.nacos.spring.context.event.config.NacosConfigPublishedEvent; import com.purgeteam.dynamic.config.starter.event.ActionConfigEvent; import com.study.cloud.springbootdubbo.config.DruidConfiguration; import com.study.cloud.springbootdubbo.util.SpringUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.context.environment.EnvironmentChangeEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; import java.sql.SQLException; import java.util.Properties; /** * Automatically monitor the changes of druidconfig.yml in nacos, and then restart the data source */ @Slf4j @Component public class MyNacosEventListener { @Autowired DruidConfiguration druidConfiguration; @NacosValue(value = "${spring.datasource.druid.url}", autoRefreshed = true) private String url; @NacosConfigListener(dataId = "druidconfig.yml", groupId = "DEFAULT_GROUP") public void onReceived(String content) throws SQLException { log.info("onReceived(String) : {}", content); DruidDataSource master = SpringUtil.getBean(DruidDataSource.class); master.setUrl(druidConfiguration.getUrl()); master.setUsername(druidConfiguration.getUsername()); master.setPassword(druidConfiguration.getPassword()); master.setDriverClassName(druidConfiguration.getDriverClassName()); master.restart(); String msg= master.getUsername() + "<>" + master.getUrl()+"----------"+master.getPassword(); System.out.println(msg); } }
The configuration information of nacos is as follows:
4, Experimental results
When we change the connection information about the database in nacos, such as url or user name and password, the data source of the application will automatically switch to the corresponding server DB for operation.
1. postman sends a request to see the result.
Now let's switch the configuration directly in nacos:
You can see that the data source has been automatically switched without restarting the application.
Now let's try and send a request to see if it's true.
It can be seen that the direct switching is indeed successful.
five Source download address
https://gitee.com/freewsf/alibabacloud-study.git