当前位置:网站首页>Interviewer: if you are asked to write a distributed configuration center, are you flustered

Interviewer: if you are asked to write a distributed configuration center, are you flustered

2020-12-07 10:28:23 Yin Jihuan

Preface

A reader friend gave me feedback , Can you write a relatively complete article about the configuration center . I was recently asked by the interviewer during the interview : How to design a configuration center ? This topic , Because I have not actually used the configuration center in my work , So there is no concept of how to design .

Today, I'll write an article for you to consider when going to the configuration center , I'm not a participant in configuration center open source projects , So the writing is for your reference only .

Is it necessary to make wheels again ?

When the interviewer asks you : If you're asked to write a configuration center , Talk about your design ideas ? First of all, we have to have our own ideas , Although it's a question in the interview process . We can also ask , There are several excellent open source configuration centers on the market , We can use it directly , Is it necessary to rebuild the wheel ?

If the interviewer says it's just a survey of your design and understanding of this piece , Then you can go on to explain your ideas . If the interviewer says that many of our frameworks are self-developed , Capricious , That's the need , Then you have to go on , Can't hide , ha-ha .

If you want to rebuild , We can also transform it based on open source , Let me talk about how to design a configuration center , What's the whole idea of it , What technical points need to be used , And then start your show .

Configuration storage selection

First of all, let's look at the selection of storage , The configuration center needs to store all configuration content , There must be storage . At present, the mainstream open source configuration centers all adopt Mysql Storage for configuration , Of course, you can also use other , such as MongoDB It's also great for .

The design of table structure with different databases will be different , such as Mysql May want 10 Tables ,MongoDB After simplification, it may be 5 A watch is enough (Mysql More than a table ,MongoDB Embedded documents ).

Mysql More than a table

 picture

MongoDB Embedded documents

 picture

These tables are stored in addition to the basic configuration content , There are also some auxiliary tables , For example, user information , Authority information, etc .

In addition to the design of the underlying structure , We also need to consider the availability of storage .Mysql You can be the master and the follower , Sub warehouse, sub table, etc ,MongoDB Naturally distributed databases , And there's no single problem , In terms of usability, it's all about OK Of .

In addition, on the design level , For configuration information, you can add a local cache , When the database or service is not available, it can also provide service capability temporarily , It's usually in client Level to do .Apollo and Nacos Will cache configuration information locally .

Configure isolation

Configuration isolation is also a very important point in the configuration center , Different environments, different configuration information , This is the most basic . Before using the configuration center, a configuration file is usually maintained for each environment in the project , Then switch through the command to use the file .

 picture

Except for the isolation of the environment , There is also isolation at the access level , For example, the namespace , Different spaces are isolated from each other , Can't visit each other .

There are many ways to isolate at the bottom , The first is to add a field to distinguish the environment when storing , Data is stored together , But it can be distinguished from , The advantage of this approach is that a set of configuration centers can be provided to all environments for use .

 picture

The second is isolation directly at the deployment level , In other words, the test environment deploys a set of independent configuration centers , A separate configuration center is also deployed online , You don't need to be isolated by fields in storage .

 picture

The third is isolation when deploying , The difference is Web Only one set of backstage management is deployed , The services corresponding to the configuration information can be deployed according to the environment , Each set has its own independent database ,Apollo It's in this way .

 picture

Configure push refresh

After modification, the configuration can be pushed to the application for updating in real time , This is the most important function , The user experience is also very good . Before using the configuration center , Useful Mysql For configuration storage , To improve performance , Reduce the pressure on the database , After the configuration information is read, it will be put into the cache , Background will start a timed thread to update , such as 1 Minutes at a time .

The problem is that after the configuration changes, the client needs to wait for a certain period of time to update , There's nothing wrong with the general scene , For some special scenarios, it needs to be changed immediately , In order to avoid business losses as much as possible .

There are two ways to implement configuration modification and update in time : Push and pull .

Pull mode was mentioned earlier , There's a time gap problem , Even if it's set up quickly , such as 1 Seconds at a time , Too high frequency will lead to too much pressure on the server .

Push mode is a better way , When there is a change in the server, push the changed information to the client , It is timely and can reduce the frequency of timing pull .

Push can use Spring DeferredResult The pattern that suspends the request implements , Please refer to my article for details :

https://mp.weixin.qq.com/s/h8JrfgLn2NMUXNDFg05qxQ

A better way is to push and pull , At present, the mainstream configuration center adopts this method . Push to ensure timeliness , Pull for the bottom of the bag , Ensure final configuration consistency , The push-pull mode can extend the pull time , Reduce server pressure .

Integrate Spring

Spring yes Java Language development essential good friend , Use Spring Can greatly improve our development efficiency , Various frameworks can be easily integrated .

stay Spring The two most common ways to get configuration values in are @Value and @ConfigurationProperties, If you want to use the above method to get the content in the configuration center , You need to load the corresponding configuration content from the configuration center when the project is started , Then integrate into Spring in .

Spring Provided in ConfigurableEnvironment,ConfigurableEnvironment There are many PropertySource.PropertySource Namely Key,Value Configuration of . So you need to start the app , Get configuration information and assemble it into PropertySource hand Spring management .

Authority audit

Whether all environments use a set of configuration centers or each environment has a separate deployment , Access control is still necessary , Because different groups are responsible for different businesses , You can't change the configuration of other groups .

Another scenario is who can change the configuration , This is usually modified by the person in charge , Team members can view configuration information , This is also a very common scenario that needs to be controlled .

 picture

Simply speaking from the function of configuration , A lot of people will say why I use configuration center , It's OK to make a table and store it , I think the storage of configuration is the most basic function , The reason why we use the configuration is that we can save the cost of doing it ourselves . At the same time, the configuration center has a full range of governance capabilities , For example, authority , Gray practical function and so on .

Indicator monitoring

As a middleware , And it's used by many systems , Some of its performance indicators also need to be monitored . There are several ways to do it .

One is that the configuration center itself exposes some index data , It can be pulled by external monitoring system ,pull The way . image Nacos It's revealed that metrics data , It can be used prometheus Pull and monitor , Very convenient .

 picture

One is to configure the center to bury itself , Docking some monitoring systems , use push The way . such as Apollo It integrates Cat Monitoring of , Relevant monitoring data can be delivered to Cat Show and alert .

Another way is to provide Tracer dependent SPI, Users can access different monitoring by themselves , More flexibility .

Either way , Our ultimate goal is the same , It's all about being able to make Bug It doesn't happen , Even if there's a problem, we can monitor , Otherwise, it will be miserable , ha-ha .

to open up API/ Multilingual SDK

At present, several active configuration centers are Java Developed , The corresponding Java SDK. If you develop your own configuration center in other languages , It's the same thing. You need a corresponding language SDK. If the company is a multilingual technology stack , So you can develop one for each language SDK Access .

If it's an open source project , You can't dictate what language people use , If you don't want to develop multilingualism SDK Words , Can offer a set of openness API Let users encapsulate themselves SDK Or access directly in the project .

About author : Yin Jihuan , Simple technology enthusiasts ,《Spring Cloud Microservices - Full stack technology and case analysis 》, 《Spring Cloud Microservices introduction Actual combat and advanced 》 author , official account Ape world Originator .

I have compiled a complete set of learning materials , Those who are interested can search through wechat 「 Ape world 」, Reply key 「 Learning materials 」 Get what I've sorted out Spring Cloud,Spring Cloud Alibaba,Sharding-JDBC Sub database and sub table , Task scheduling framework XXL-JOB,MongoDB, Reptiles and other related information .

版权声明
本文为[Yin Jihuan]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/202012071026255717.html