Original Published: Http://www.gufeng.tech/
1. Background
The default for Springcloud Configserver is to persist using Git. Git has its natural advantages, such as multi-versioning, branch management, submitting audit policies, and so on, but it is not enough to do fine-grained permissions control over the data stored there. Of course, you can change the way you use it to suit that, but what we're going to do today is move the persistence from git to MySQL.
2. Querying configuration information
Configserver has an interface: Org.springframework.cloud.config.server.environment.EnvironmentRepository, the implementation class of this interface is configserver to query the configuration letter The method signature is as follows:
Environment FindOne (string application, string profile, string label);
We can implement this interface and query MySQL in the implementation of the method, so we have half the success, and the other half is to figure out how to save the data to MySQL. We still solve the query problem first, we implement the method content 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); }}
Next we'll 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 are using the MyBatis annotation method, please refer to the relevant documentation for the details of the MyBatis annotation use.
3. Database-related functions
Let's look at the configuration of the data source first:
@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 look at the configuration information for the MyBatis (and of course, you can use SPRINGJDBC):
@Configuration @enabletransactionmanagement//Support Transactions
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, what do you see in Configproperty? include at least the following: application, profile, label, key, value, other content can be increased or decreased according to actual needs.
Here are two ideas for reference:
One: In the configpropertity corresponding table stores the current configuration and historical configuration, through the version (or status bit) to differentiate, using the status bit of the advantage is that the overall amount of storage will be less, the advantage of using the version is to be able to find a historical version of the data without the need for analysis;
Second: Two tables, one palm storage currently in use in the configuration information, the other table is used to store the historical configuration information, each time there are changes to write two tables at the same time, the history table by appending, the current table in the updated way.
The above is a way to change Configserver's persistent storage from git to MySQL.
This article is from the "Valley Wind" blog, please be sure to keep this source http://1202955.blog.51cto.com/1192955/1920987
Change Springcloud configserver persistent storage to MySQL