In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
The original text is posted on: http://www.gufeng.tech/ Valley style's personal home page.
1. Background
SpringCloud's ConfigServer is persisted using git by default. Git has its natural advantages, such as multi-version management, branch management, submission audit policy, etc., but if you do fine-grained permission control relative to the data stored in it, it will be inadequate. Of course, you can also change the way you use it to suit this feature, but what we need to do today is to migrate persistence from git to MySQL.
two。 Query configuration information
ConfigServer has an API: org.springframework.cloud.config.server.environment.EnvironmentRepository. The implementation class of this interface is used by ConfigServer to query configuration information. The method signature is as follows:
Environment findOne (String application, String profile, String label)
We can implement this interface to query MySQL in the method implementation, so we are half done, and the other half is to figure out how to store the data in MySQL. Let's solve the problem of query first. the content of this method is as follows:
Public class DatabasesEnvironmentRepository implements EnvironmentRepository {@ Autowired private ConfigService configService; @ Override public Environment findOne (String application, String profile, String label) {if (StringUtils.isEmpty (application) | | StringUtils.isEmpty (profile)) return null; ConfigItem configItem = configService.findConfig (application, profile, label) If (configItem! = null) {Environment environment = new Environment (application, StringUtils.commaDelimitedListToStringArray (profile), label, configItem.getVersion ()); Map map = new HashMap (); for (ConfigProperty configProperty: configItem.getConfigProperties ()) {map.put (configProperty.getKey (), configProperty.getValue ()) } environment.add (new PropertySource (application + "_" + profile + "_" + label, map); return environment;} return new Environment (application, StringUtils.commaDelimitedListToStringArray (profile));}}
Let's take a look at the contents of the ConfigService class:
@ Servicepublic class ConfigService {@ Autowired private ConfigDAO configDAO; public ConfigItem findConfig (String application, String profile, String label) {ConfigItem configItem = configDAO.findConfig (application, profile, label); if (null = = configItem) {return null;} List configProperties = configDAO.findConfigProperties (configItem.getId ()); configItem.setConfigProperties (configProperties); return configItem;}}
Finally, let's look at the implementation of ConfigDAO:
@ Mapperpublic interface ConfigDAO {@ Select ("select * from config_item where application = # {application} and profile = # {profile} and label = # {label}") List findConfigProperties (@ Param ("application") String application, @ Param ("profile") String profile, @ Param ("label") String label;}
Here we use the annotation method of MyBatis. For more information on the use of MyBatis annotations, please refer to the relevant documentation.
3. Database related functions
Let's first look at the configuration of the data source:
@ Configurationpublic class DataSourceConfiguration {@ Value ("${jdbc.driver}") private String driver; @ Value ("${jdbc.url}") private String url; @ Value ("${jdbc.username}") private String username; @ Value ("${jdbc.password}") private String password; @ Value ("${jdbc.maxActive}") private int maxActive; @ Value ("${jdbc.maxIdel}") private int maxIdel @ Value ("${jdbc.maxWait}") private long maxWait; @ Bean public BasicDataSource dataSource () {BasicDataSource dataSource = new BasicDataSource (); dataSource.setDriverClassName (driver); dataSource.setUrl (url); dataSource.setUsername (username); dataSource.setPassword (password); dataSource.setMaxTotal (maxActive); dataSource.setMaxIdle (maxIdel); dataSource.setMaxWaitMillis (maxWait) DataSource.setValidationQuery ("SELECT 1"); dataSource.setTestOnBorrow (true); return dataSource;}}
Next, take a look at the configuration information of MyBatis (of course, you can also use SpringJDBC to do this):
@ Configuration@EnableTransactionManagement// supports transaction public class MyBatisConfig implements TransactionManagementConfigurer {@ Autowired private DataSource dataSource; @ Override public PlatformTransactionManager annotationDrivenTransactionManager () {return new DataSourceTransactionManager (dataSource);} @ Bean (name = "sqlSessionFactory") public SqlSessionFactory sqlSessionFactoryBean () {SqlSessionFactoryBean bean = new SqlSessionFactoryBean (); bean.setDataSource (dataSource); try {return bean.getObject () } catch (Exception e) {e.printStackTrace (); throw new RuntimeException (e);} @ Bean public SqlSessionTemplate sqlSessionTemplate (SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate (sqlSessionFactory);}}
Finally, take a look at what is in ConfigProperty? At least the following contents are included: application, profile, label, key, value, and other contents can be added or decreased according to actual needs.
Here are two ideas for reference:
One: store the current configuration and historical configuration in the table corresponding to ConfigPropertity, and distinguish them by version (or status bit). The advantage of using status bits is that the overall storage quantity is less, and the advantage of using version is that you can look up the data of a certain historical version without analysis.
Second: it is divided into two tables, one palm stores the configuration information currently in use, and the other table is used to store historical configuration information. Each change is written into two tables at the same time. The history table is appended and the current table is updated.
This is one way to change the persistent storage of ConfigServer from git to MySQL.
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.