{"id":318760,"date":"2021-02-28T15:00:20","date_gmt":"2021-02-28T15:00:20","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=318760"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=318760","title":{"rendered":"\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0443\u043b\u044c\u0442\u0438\u0430\u0440\u0435\u043d\u0434\u043d\u043e\u0441\u0442\u0438 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Spring Boot, MongoDB \u0438 Redis"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u0412 \u044d\u0442\u043e\u043c \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0443\u043b\u044c\u0442\u0438\u0430\u0440\u0435\u043d\u0434\u043d\u043e\u0441\u0442\u044c \u0432 Spring Boot \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c MongoDB \u0438 Redis.<\/p>\n<h3>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f:<\/h3>\n<ul>\n<li>\n<p>Spring Boot 2.4<\/p>\n<\/li>\n<li>\n<p>Maven 3.6. +<\/p>\n<\/li>\n<li>\n<p>JAVA 8+<\/p>\n<\/li>\n<li>\n<p>\u041c\u043e\u043d\u0433\u043e 4.4<\/p>\n<\/li>\n<li>\n<p>Redis 5<\/p>\n<\/li>\n<\/ul>\n<h3>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u043c\u0443\u043b\u044c\u0442\u0438\u0430\u0440\u0435\u043d\u0434\u043d\u043e\u0441\u0442\u044c?<\/h3>\n<p>\u041c\u0443\u043b\u044c\u0442\u0438\u0430\u0440\u0435\u043d\u0434\u043d\u043e\u0441\u0442\u044c (\u0430\u043d\u0433\u043b. multitenancy \u2014 \u00ab\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0430\u0440\u0435\u043d\u0434\u0430\u00bb) \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u0430\u044f \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043e\u0434\u0438\u043d \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043e\u0431\u0441\u043b\u0443\u0436\u0438\u0432\u0430\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432.&nbsp;\u0412\u0441\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u043e\u0431\u0449\u0438\u043c, \u0437\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u0440\u0430\u0437\u043d\u044b\u0445 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0434\u043e\u043b\u0436\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u044b.&nbsp;\u041d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u043e\u043d\u0438 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0440\u0435\u0441\u0443\u0440\u0441\u044b, \u0430\u0440\u0435\u043d\u0434\u0430\u0442\u043e\u0440\u044b \u043d\u0435 \u0437\u043d\u0430\u044e\u0442 \u0434\u0440\u0443\u0433 \u0434\u0440\u0443\u0433\u0430, \u0438 \u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0435 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e.&nbsp;\u041a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u043a\u0443\u043f\u0430\u0442\u0435\u043b\u044c \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0430\u0440\u0435\u043d\u0434\u0430\u0442\u043e\u0440\u043e\u043c.<\/p>\n<p>\u041f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u00ab\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u043e\u0435 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u0435 \u043a\u0430\u043a \u0443\u0441\u043b\u0443\u0433\u0430\u00bb (SaaS) \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u043c \u043c\u0443\u043b\u044c\u0442\u0438\u0430\u0440\u0435\u043d\u0434\u043d\u043e\u0439 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b.&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/azure-sql\/database\/saas-tenancy-app-design-patterns\" rel=\"noopener noreferrer nofollow\"><strong><u>\u0411\u043e\u043b\u0435\u0435 <\/u><\/strong><\/a><strong><u>\u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e<\/u><\/strong>.<\/p>\n<h3>\u041c\u043e\u0434\u0435\u043b\u0438 \u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u0430\u0440\u0435\u043d\u0434\u0430\u0442\u043e\u0440\u0430\u043c\u0438<\/h3>\n<p>\u041c\u043e\u0436\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u0442\u0440\u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043d\u044b\u0445 \u0448\u0430\u0431\u043b\u043e\u043d\u0430 \u0434\u043b\u044f \u043c\u0443\u043b\u044c\u0442\u0438\u0430\u0440\u0435\u043d\u0434\u043d\u043e\u0439 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b (Multitenancy), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u0441\u0442\u0435\u043f\u0435\u043d\u044c\u044e \u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u043a\u043b\u0438\u0435\u043d\u0442\u0430.<\/p>\n<ol>\n<li>\n<p><strong>\u0411\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0430\u0440\u0435\u043d\u0434\u0430\u0442\u043e\u0440\u0430<\/strong>&nbsp;: \u043a\u0430\u0436\u0434\u044b\u0439 \u0430\u0440\u0435\u043d\u0434\u0430\u0442\u043e\u0440 \u0438\u043c\u0435\u0435\u0442 \u0441\u0432\u043e\u044e \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0438\u0437\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u043d \u043e\u0442 \u0434\u0440\u0443\u0433\u0438\u0445 \u0430\u0440\u0435\u043d\u0434\u0430\u0442\u043e\u0440\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0431\u0449\u0430\u044f \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u043e\u0431\u0449\u0430\u044f \u0441\u0445\u0435\u043c\u0430:<\/strong>&nbsp;\u0432\u0441\u0435 \u0430\u0440\u0435\u043d\u0434\u0430\u0442\u043e\u0440\u044b \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u044b.&nbsp;\u0412 \u043a\u0430\u0436\u0434\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0435\u0441\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0441 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u043c \u043a\u043b\u0438\u0435\u043d\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0432\u043b\u0430\u0434\u0435\u043b\u044c\u0446\u0430 \u0441\u0442\u0440\u043e\u043a\u0438.<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0431\u0449\u0430\u044f \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0430\u044f \u0441\u0445\u0435\u043c\u0430<\/strong>&nbsp;: \u0432\u0441\u0435 \u0430\u0440\u0435\u043d\u0434\u0430\u0442\u043e\u0440\u044b \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445, \u043d\u043e \u0438\u043c\u0435\u044e\u0442 \u0441\u0432\u043e\u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u0441\u0445\u0435\u043c\u044b \u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<\/li>\n<\/ol>\n<h3>\u041d\u0430\u0447\u043d\u0435\u043c<\/h3>\n<p>\u0412 \u044d\u0442\u043e\u043c \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0435 \u043c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043c\u0443\u043b\u044c\u0442\u0438\u0430\u0440\u0435\u043d\u0434\u043d\u043e\u0441\u0442\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0430.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d54\/3dd\/04a\/d543dd04a1a4bfab98d5e99baf646a16.png\" width=\"631\" height=\"686\"><figcaption><\/figcaption><\/figure>\n<p>\u041c\u044b \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 Spring Boot \u043d\u0430&nbsp;<a href=\"http:\/\/start.spring.io\/\" rel=\"noopener noreferrer nofollow\"><strong><u>start.spring.io<\/u><\/strong><\/a>&nbsp;\u0441\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c\u0438 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044f\u043c\u0438:<\/p>\n<pre><code class=\"xml\">&lt;dependencies&gt;     &lt;dependency&gt;         &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;         &lt;artifactId&gt;spring-boot-starter-data-mongodb&lt;\/artifactId&gt;     &lt;\/dependency&gt;     &lt;dependency&gt;         &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;         &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;     &lt;\/dependency&gt;     &lt;dependency&gt;         &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;         &lt;artifactId&gt;spring-boot-starter-data-redis&lt;\/artifactId&gt;     &lt;\/dependency&gt;     &lt;dependency&gt;         &lt;groupId&gt;redis.clients&lt;\/groupId&gt;         &lt;artifactId&gt;jedis&lt;\/artifactId&gt;     &lt;\/dependency&gt;     &lt;dependency&gt;         &lt;groupId&gt;org.projectlombok&lt;\/groupId&gt;         &lt;artifactId&gt;lombok&lt;\/artifactId&gt;         &lt;optional&gt;true&lt;\/optional&gt;     &lt;\/dependency&gt; &lt;\/dependencies&gt;<\/code><\/pre>\n<h4>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0430<\/h4>\n<p>\u0418\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430.&nbsp;\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u044b \u0432\u043a\u043b\u044e\u0447\u0438\u043c \u043f\u043e\u043b\u0435 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0432 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a HTTP-\u0437\u0430\u043f\u0440\u043e\u0441\u0430.&nbsp;<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0438\u0437 http \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430&nbsp;<code>X-Tenant.<\/code><\/p>\n<pre><code class=\"java\">@Slf4j @Component public class TenantInterceptor implements WebRequestInterceptor {     private static final String TENANT_HEADER = \"X-Tenant\";     @Override     public void preHandle(WebRequest request) {         String tenantId = request.getHeader(TENANT_HEADER);         if (tenantId != null &amp;&amp; !tenantId.isEmpty()) {             TenantContext.setTenantId(tenantId);             log.info(\"Tenant header get: {}\", tenantId);         } else {             log.error(\"Tenant header not found.\");             throw new TenantAliasNotFoundException(\"Tenant header not found.\");         }     }     @Override     public void postHandle(WebRequest webRequest, ModelMap modelMap) {         TenantContext.clear();     }     @Override     public void afterCompletion(WebRequest webRequest, Exception e) {     } }<\/code><\/pre>\n<p><code>TenantContext&nbsp;<\/code>\u2014&nbsp;\u044d\u0442\u043e \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0435\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e ThreadLocal.&nbsp;ThreadLocal \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f\u0430 (scope of access), \u0442\u0430\u043a\u0443\u044e \u200b\u200b\u043a\u0430\u043a \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u0430 (request scope) \u0438\u043b\u0438 \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0441\u0435\u0430\u043d\u0441\u0430 (session scope).<\/p>\n<p>\u0421\u043e\u0445\u0440\u0430\u043d\u044f\u044f tenantId \u0432 ThreadLocal, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0431\u044b\u0442\u044c \u0443\u0432\u0435\u0440\u0435\u043d\u044b, \u0447\u0442\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0438\u043c\u0435\u0435\u0442 \u0441\u0432\u043e\u044e \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u043a\u043e\u043f\u0438\u044e \u044d\u0442\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0438 \u0447\u0442\u043e \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043f\u043e\u0442\u043e\u043a \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0434\u0440\u0443\u0433\u043e\u043c\u0443 tenantId:<\/p>\n<pre><code class=\"java\">@Slf4j public class TenantContext {     private static final ThreadLocal&lt;String&gt; CONTEXT = new ThreadLocal&lt;&gt;();     public static void setTenantId(String tenantId) {         log.debug(\"Setting tenantId to \" + tenantId);         CONTEXT.set(tenantId);     }     public static String getTenantId() {         return CONTEXT.get();     }     public static void clear() {         CONTEXT.remove();     } }<\/code><\/pre>\n<h4>\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 (Tenant Datasources)<\/h4>\n<p>\u0412 \u043d\u0430\u0448\u0435\u0439 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0435 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 Redis, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u0431\u0430\u0437\u0443 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445 (master database), \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0446\u0435\u043d\u0442\u0440\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u0432\u0441\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043a\u043b\u0438\u0435\u043d\u0442\u0430.&nbsp;\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0438\u0437 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0438 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u0442\u0441\u044f \u0438\u0437 \u0431\u0430\u0437\u044b \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445 .<\/p>\n<p><code>RedisDatasourceService.java<\/code>&nbsp;\u2014 \u044d\u0442\u043e \u043a\u043b\u0430\u0441\u0441, \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0449\u0438\u0439 \u0437\u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u043c\u0438 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f\u043c\u0438 \u0441 \u0431\u0430\u0437\u043e\u0439 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445 .<\/p>\n<pre><code class=\"java\">@Service public class RedisDatasourceService {      private final RedisTemplate redisTemplate;     private final ApplicationProperties applicationProperties;     private final DataSourceProperties dataSourceProperties;  \t\tpublic RedisDatasourceService(RedisTemplate redisTemplate, ApplicationProperties applicationProperties, DataSourceProperties dataSourceProperties) {         this.redisTemplate = redisTemplate;         this.applicationProperties = applicationProperties;         this.dataSourceProperties = dataSourceProperties;     }          \/**      * Save tenant datasource infos      *      * @param tenantDatasource data of datasource      * @return status if true save successfully , false error      *\/          public boolean save(TenantDatasource tenantDatasource) {         try {             Map ruleHash = new ObjectMapper().convertValue(tenantDatasource, Map.class);             redisTemplate.opsForHash().put(applicationProperties.getServiceKey(), String.format(\"%s_%s\", applicationProperties.getTenantKey(), tenantDatasource.getAlias()), ruleHash);             return true;         } catch (Exception e) {             return false;         }     }          \/**      * Get all of keys      *      * @return list of datasource      *\/           public List findAll() {         return redisTemplate.opsForHash().values(applicationProperties.getServiceKey());     }          \/**      * Get datasource      *      * @return map key and datasource infos      *\/           public Map&lt;String, TenantDatasource&gt; loadServiceDatasources() {         List&lt;Map&lt;String, Object&gt;&gt; datasourceConfigList = findAll();         \/\/ Save datasource credentials first time         \/\/ In production mode, this part can be skip         if (datasourceConfigList.isEmpty()) {             List&lt;DataSourceProperties.Tenant&gt; tenants = dataSourceProperties.getDatasources();             tenants.forEach(d -&gt; {                 TenantDatasource tenant = TenantDatasource.builder()                         .alias(d.getAlias())                         .database(d.getDatabase())                         .host(d.getHost())                         .port(d.getPort())                         .username(d.getUsername())                         .password(d.getPassword())                         .build();                 save(tenant);             });         }         return getDataSourceHashMap();     }          \/**      * Get all tenant alias      *      * @return list of alias      *\/           public List&lt;String&gt; getTenantsAlias() {         \/\/ get list all datasource for this microservice         List&lt;Map&lt;String, Object&gt;&gt; datasourceConfigList = findAll();         return datasourceConfigList.stream().map(data -&gt; (String) data.get(\"alias\")).collect(Collectors.toList());     }          \/**      * Fill the data sources list.      *      * @return Map&lt;String, TenantDatasource&gt;      *\/           private Map&lt;String, TenantDatasource&gt; getDataSourceHashMap() {         Map&lt;String, TenantDatasource&gt; datasourceMap = new HashMap&lt;&gt;();         \/\/ get list all datasource for this microservice         List&lt;Map&lt;String, Object&gt;&gt; datasourceConfigList = findAll();         datasourceConfigList.forEach(data -&gt; datasourceMap.put(String.format(\"%s_%s\", applicationProperties.getTenantKey(), (String) data.get(\"alias\")), new TenantDatasource((String) data.get(\"alias\"), (String) data.get(\"host\"), (int) data.get(\"port\"), (String) data.get(\"database\"), (String) data.get(\"username\"), (String) data.get(\"password\"))));         return datasourceMap;     } }<\/code><\/pre>\n<p>\u0412 \u044d\u0442\u043e\u043c \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0435 \u043c\u044b \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u043b\u0438 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0435 \u0438\u0437 yml-\u0444\u0430\u0439\u043b\u0430 <code>(tenants.yml).&nbsp;<\/code>\u0412 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u043c \u0440\u0435\u0436\u0438\u043c\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0435 \u0442\u043e\u0447\u043a\u0438 \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0435 \u0432 \u0431\u0430\u0437\u0435 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7f2\/bf0\/034\/7f2bf003414385f88d7f52bf3bff1c59.png\" width=\"640\" height=\"718\"><figcaption><\/figcaption><\/figure>\n<p>\u0427\u0442\u043e\u0431\u044b \u0438\u043c\u0435\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 mongo, \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c&nbsp;\u043a\u043b\u0430\u0441\u0441 <code>MultiTenantMongoDBFactory<\/code>, \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u044e\u0449\u0438\u0439  \u043a\u043b\u0430\u0441\u0441 <code>SimpleMongoClientDatabaseFactory \u0438\u0437 org.springframework.data.mongodb.core<\/code>.&nbsp;\u041e\u043d \u0432\u0435\u0440\u043d\u0435\u0442&nbsp;\u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440<code>MongoDatabase<\/code>, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0439 \u0441 \u0442\u0435\u043a\u0443\u0449\u0438\u043c \u0430\u0440\u0435\u043d\u0434\u0430\u0442\u043e\u0440\u043e\u043c.<\/p>\n<pre><code class=\"java\">@Configuration public class MultiTenantMongoDBFactory extends SimpleMongoClientDatabaseFactory {  \t\t@Autowired     MongoDataSources mongoDataSources;  \t\tpublic MultiTenantMongoDBFactory(@Qualifier(\"getMongoClient\") MongoClient mongoClient, String databaseName) {         super(mongoClient, databaseName);     }      @Override     protected MongoDatabase doGetMongoDatabase(String dbName) {         return mongoDataSources.mongoDatabaseCurrentTenantResolver();     } }<\/code><\/pre>\n<p>\u041d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c&nbsp;\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 <code>MongoDBFactoryMultiTenant&nbsp;<\/code>\u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e (&nbsp;<code>MongoClient&nbsp;<\/code>\u0438&nbsp;<code>databaseName<\/code>).<\/p>\n<p>\u042d\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u043f\u0440\u043e\u0437\u0440\u0430\u0447\u043d\u044b\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0430.&nbsp;<\/p>\n<pre><code class=\"java\">@Component @Slf4j public class MongoDataSources {      \/**      * Key: String tenant alias      * Value: TenantDatasource      *\/     private Map&lt;String, TenantDatasource&gt; tenantClients;      private final ApplicationProperties applicationProperties;     private final RedisDatasourceService redisDatasourceService;      public MongoDataSources(ApplicationProperties applicationProperties, RedisDatasourceService redisDatasourceService) {         this.applicationProperties = applicationProperties;         this.redisDatasourceService = redisDatasourceService;     }      \/**      * Initialize all mongo datasource      *\/     @PostConstruct     @Lazy     public void initTenant() {         tenantClients = new HashMap&lt;&gt;();         tenantClients = redisDatasourceService.loadServiceDatasources();     }      \/**      * Default Database name for spring initialization. It is used to be injected into the constructor of MultiTenantMongoDBFactory.      *      * @return String of default database.      *\/     @Bean     public String databaseName() {         return applicationProperties.getDatasourceDefault().getDatabase();     }      \/**      * Default Mongo Connection for spring initialization.      * It is used to be injected into the constructor of MultiTenantMongoDBFactory.      *\/     @Bean     public MongoClient getMongoClient() {         MongoCredential credential = MongoCredential.createCredential(applicationProperties.getDatasourceDefault().getUsername(), applicationProperties.getDatasourceDefault().getDatabase(), applicationProperties.getDatasourceDefault().getPassword().toCharArray());         return MongoClients.create(MongoClientSettings.builder()                 .applyToClusterSettings(builder -&gt;                         builder.hosts(Collections.singletonList(new ServerAddress(applicationProperties.getDatasourceDefault().getHost(), Integer.parseInt(applicationProperties.getDatasourceDefault().getPort())))))                 .credential(credential)                 .build());     }      \/**      * This will get called for each DB operations      *      * @return MongoDatabase      *\/     public MongoDatabase mongoDatabaseCurrentTenantResolver() {         try {             final String tenantId = TenantContext.getTenantId();              \/\/ Compose tenant alias. (tenantAlias = key + tenantId)             String tenantAlias = String.format(\"%s_%s\", applicationProperties.getTenantKey(), tenantId);              return tenantClients.get(tenantAlias).getClient().                     getDatabase(tenantClients.get(tenantAlias).getDatabase());         } catch (NullPointerException exception) {             throw new TenantAliasNotFoundException(\"Tenant Datasource alias not found.\");         }     } 73 }<\/code><\/pre>\n<h4>\u0422\u0435\u0441\u0442<\/h4>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c CRUD \u043f\u0440\u0438\u043c\u0435\u0440 \u0441 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u043c Employee.<\/p>\n<pre><code class=\"java\">@Builder @Data @AllArgsConstructor @NoArgsConstructor @Accessors(chain = true) @Document(collection = \"employee\") public class Employee  {      @Id     private String id;      private String firstName;      private String lastName;      private String email; }<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441\u044b <code>EmployeeRepository, EmployeeService&nbsp;&nbsp;<\/code>\u0438&nbsp;<code>EmployeeController<\/code>.&nbsp;\u0414\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043c\u044b \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0444\u0438\u043a\u0442\u0438\u0432\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043a\u0430\u0436\u0434\u0443\u044e \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u043a\u043b\u0438\u0435\u043d\u0442\u0430.<\/p>\n<pre><code class=\"java\">@Override public void run(String... args) throws Exception {     List&lt;String&gt; aliasList = redisDatasourceService.getTenantsAlias();     if (!aliasList.isEmpty()) {         \/\/perform actions for each tenant         aliasList.forEach(alias -&gt; {             TenantContext.setTenantId(alias);             employeeRepository.deleteAll();              Employee employee = Employee.builder()                     .firstName(alias)                     .lastName(alias)                     .email(String.format(\"%s%s\", alias, \"@localhost.com\" ))                     .build();             employeeRepository.save(employee);              TenantContext.clear();         });     } }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438 \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e.&nbsp;<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/cab\/8d9\/171\/cab8d9171a8d207319658839372c923b.png\" width=\"1395\" height=\"810\"><figcaption><\/figcaption><\/figure>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3ea\/947\/b7b\/3ea947b7bb5783d2fd9df0a18e44c41d.png\" width=\"1542\" height=\"822\"><figcaption><\/figcaption><\/figure>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/5bd\/10d\/2a6\/5bd10d2a6699d9acf9c4b130b72e35f6.png\" width=\"1541\" height=\"804\"><figcaption><\/figcaption><\/figure>\n<p>\u0418\u0442\u0430\u043a, \u043c\u044b \u0432\u0441\u0435 \u0441\u0434\u0435\u043b\u0430\u043b\u0438. \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u044d\u0442\u043e \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0432\u0430\u043c \u043f\u043e\u043d\u044f\u0442\u044c, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u043c\u0443\u043b\u044c\u0442\u0438\u0430\u0440\u0435\u043d\u0434\u043d\u043e\u0441\u0442\u044c \u0438 \u043a\u0430\u043a \u043e\u043d\u0430 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u0432 Spring Boot \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c MongoDB \u0438 Redis.<\/p>\n<p>\u041f\u043e\u043b\u043d\u044b\u0439 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043d\u0430&nbsp;<a href=\"https:\/\/github.com\/anicetkeric\/spring-boot-multi-tenancy-mongo.git\" rel=\"noopener noreferrer nofollow\"><strong><u>GitHub<\/u><\/strong><\/a>.<\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/544642\/\"> https:\/\/habr.com\/ru\/post\/544642\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u0412 \u044d\u0442\u043e\u043c \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0443\u043b\u044c\u0442\u0438\u0430\u0440\u0435\u043d\u0434\u043d\u043e\u0441\u0442\u044c \u0432 Spring Boot \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c MongoDB \u0438 Redis.<\/p>\n<h3>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f:<\/h3>\n<ul>\n<li>\n<p>Spring Boot 2.4<\/p>\n<\/li>\n<li>\n<p>Maven 3.6. +<\/p>\n<\/li>\n<li>\n<p>JAVA 8+<\/p>\n<\/li>\n<li>\n<p>\u041c\u043e\u043d\u0433\u043e 4.4<\/p>\n<\/li>\n<li>\n<p>Redis 5<\/p>\n<\/li>\n<\/ul>\n<h3>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u043c\u0443\u043b\u044c\u0442\u0438\u0430\u0440\u0435\u043d\u0434\u043d\u043e\u0441\u0442\u044c?<\/h3>\n<p>\u041c\u0443\u043b\u044c\u0442\u0438\u0430\u0440\u0435\u043d\u0434\u043d\u043e\u0441\u0442\u044c (\u0430\u043d\u0433\u043b. multitenancy \u2014 \u00ab\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0430\u0440\u0435\u043d\u0434\u0430\u00bb) \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u0430\u044f \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043e\u0434\u0438\u043d \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043e\u0431\u0441\u043b\u0443\u0436\u0438\u0432\u0430\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432.&nbsp;\u0412\u0441\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u043e\u0431\u0449\u0438\u043c, \u0437\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u0440\u0430\u0437\u043d\u044b\u0445 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0434\u043e\u043b\u0436\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u044b.&nbsp;\u041d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u043e\u043d\u0438 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0440\u0435\u0441\u0443\u0440\u0441\u044b, \u0430\u0440\u0435\u043d\u0434\u0430\u0442\u043e\u0440\u044b \u043d\u0435 \u0437\u043d\u0430\u044e\u0442 \u0434\u0440\u0443\u0433 \u0434\u0440\u0443\u0433\u0430, \u0438 \u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0435 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e.&nbsp;\u041a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u043a\u0443\u043f\u0430\u0442\u0435\u043b\u044c \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0430\u0440\u0435\u043d\u0434\u0430\u0442\u043e\u0440\u043e\u043c.<\/p>\n<p>\u041f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u00ab\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u043e\u0435 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u0435 \u043a\u0430\u043a \u0443\u0441\u043b\u0443\u0433\u0430\u00bb (SaaS) \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u043c \u043c\u0443\u043b\u044c\u0442\u0438\u0430\u0440\u0435\u043d\u0434\u043d\u043e\u0439 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b.&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/azure-sql\/database\/saas-tenancy-app-design-patterns\" rel=\"noopener noreferrer nofollow\"><strong><u>\u0411\u043e\u043b\u0435\u0435 <\/u><\/strong><\/a><strong><u>\u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e<\/u><\/strong>.<\/p>\n<h3>\u041c\u043e\u0434\u0435\u043b\u0438 \u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u0430\u0440\u0435\u043d\u0434\u0430\u0442\u043e\u0440\u0430\u043c\u0438<\/h3>\n<p>\u041c\u043e\u0436\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u0442\u0440\u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043d\u044b\u0445 \u0448\u0430\u0431\u043b\u043e\u043d\u0430 \u0434\u043b\u044f \u043c\u0443\u043b\u044c\u0442\u0438\u0430\u0440\u0435\u043d\u0434\u043d\u043e\u0439 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b (Multitenancy), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u0441\u0442\u0435\u043f\u0435\u043d\u044c\u044e \u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u043a\u043b\u0438\u0435\u043d\u0442\u0430.<\/p>\n<ol>\n<li>\n<p><strong>\u0411\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0430\u0440\u0435\u043d\u0434\u0430\u0442\u043e\u0440\u0430<\/strong>&nbsp;: \u043a\u0430\u0436\u0434\u044b\u0439 \u0430\u0440\u0435\u043d\u0434\u0430\u0442\u043e\u0440 \u0438\u043c\u0435\u0435\u0442 \u0441\u0432\u043e\u044e \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0438\u0437\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u043d \u043e\u0442 \u0434\u0440\u0443\u0433\u0438\u0445 \u0430\u0440\u0435\u043d\u0434\u0430\u0442\u043e\u0440\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0431\u0449\u0430\u044f \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u043e\u0431\u0449\u0430\u044f \u0441\u0445\u0435\u043c\u0430:<\/strong>&nbsp;\u0432\u0441\u0435 \u0430\u0440\u0435\u043d\u0434\u0430\u0442\u043e\u0440\u044b \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u044b.&nbsp;\u0412 \u043a\u0430\u0436\u0434\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0435\u0441\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0441 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u043c \u043a\u043b\u0438\u0435\u043d\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0432\u043b\u0430\u0434\u0435\u043b\u044c\u0446\u0430 \u0441\u0442\u0440\u043e\u043a\u0438.<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0431\u0449\u0430\u044f \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0430\u044f \u0441\u0445\u0435\u043c\u0430<\/strong>&nbsp;: \u0432\u0441\u0435 \u0430\u0440\u0435\u043d\u0434\u0430\u0442\u043e\u0440\u044b \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445, \u043d\u043e \u0438\u043c\u0435\u044e\u0442 \u0441\u0432\u043e\u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u0441\u0445\u0435\u043c\u044b \u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<\/li>\n<\/ol>\n<h3>\u041d\u0430\u0447\u043d\u0435\u043c<\/h3>\n<p>\u0412 \u044d\u0442\u043e\u043c \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0435 \u043c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043c\u0443\u043b\u044c\u0442\u0438\u0430\u0440\u0435\u043d\u0434\u043d\u043e\u0441\u0442\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0430.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041c\u044b \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 Spring Boot \u043d\u0430&nbsp;<a href=\"http:\/\/start.spring.io\/\" rel=\"noopener noreferrer nofollow\"><strong><u>start.spring.io<\/u><\/strong><\/a>&nbsp;\u0441\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c\u0438 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044f\u043c\u0438:<\/p>\n<pre><code class=\"xml\">&lt;dependencies&gt;     &lt;dependency&gt;         &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;         &lt;artifactId&gt;spring-boot-starter-data-mongodb&lt;\/artifactId&gt;     &lt;\/dependency&gt;     &lt;dependency&gt;         &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;         &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;     &lt;\/dependency&gt;     &lt;dependency&gt;         &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;         &lt;artifactId&gt;spring-boot-starter-data-redis&lt;\/artifactId&gt;     &lt;\/dependency&gt;     &lt;dependency&gt;         &lt;groupId&gt;redis.clients&lt;\/groupId&gt;         &lt;artifactId&gt;jedis&lt;\/artifactId&gt;     &lt;\/dependency&gt;     &lt;dependency&gt;         &lt;groupId&gt;org.projectlombok&lt;\/groupId&gt;         &lt;artifactId&gt;lombok&lt;\/artifactId&gt;         &lt;optional&gt;true&lt;\/optional&gt;     &lt;\/dependency&gt; &lt;\/dependencies&gt;<\/code><\/pre>\n<h4>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0430<\/h4>\n<p>\u0418\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430.&nbsp;\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u044b \u0432\u043a\u043b\u044e\u0447\u0438\u043c \u043f\u043e\u043b\u0435 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0432 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a HTTP-\u0437\u0430\u043f\u0440\u043e\u0441\u0430.&nbsp;<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0447\u0438\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0438\u0437 http \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430&nbsp;<code>X-Tenant.<\/code><\/p>\n<pre><code class=\"java\">@Slf4j @Component public class TenantInterceptor implements WebRequestInterceptor {     private static final String TENANT_HEADER = \"X-Tenant\";     @Override     public void preHandle(WebRequest request) {         String tenantId = request.getHeader(TENANT_HEADER);         if (tenantId != null &amp;&amp; !tenantId.isEmpty()) {             TenantContext.setTenantId(tenantId);             log.info(\"Tenant header get: {}\", tenantId);         } else {             log.error(\"Tenant header not found.\");             throw new TenantAliasNotFoundException(\"Tenant header not found.\");         }     }     @Override     public void postHandle(WebRequest webRequest, ModelMap modelMap) {         TenantContext.clear();     }     @Override     public void afterCompletion(WebRequest webRequest, Exception e) {     } }<\/code><\/pre>\n<p><code>TenantContext&nbsp;<\/code>\u2014&nbsp;\u044d\u0442\u043e \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0435\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e ThreadLocal.&nbsp;ThreadLocal \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f\u0430 (scope of access), \u0442\u0430\u043a\u0443\u044e \u200b\u200b\u043a\u0430\u043a \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u0430 (request scope) \u0438\u043b\u0438 \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0441\u0435\u0430\u043d\u0441\u0430 (session scope).<\/p>\n<p>\u0421\u043e\u0445\u0440\u0430\u043d\u044f\u044f tenantId \u0432 ThreadLocal, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0431\u044b\u0442\u044c \u0443\u0432\u0435\u0440\u0435\u043d\u044b, \u0447\u0442\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0438\u043c\u0435\u0435\u0442 \u0441\u0432\u043e\u044e \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u043a\u043e\u043f\u0438\u044e \u044d\u0442\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0438 \u0447\u0442\u043e \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043f\u043e\u0442\u043e\u043a \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0434\u0440\u0443\u0433\u043e\u043c\u0443 tenantId:<\/p>\n<pre><code class=\"java\">@Slf4j public class TenantContext {     private static final ThreadLocal&lt;String&gt; CONTEXT = new ThreadLocal&lt;&gt;();     public static void setTenantId(String tenantId) {         log.debug(\"Setting tenantId to \" + tenantId);         CONTEXT.set(tenantId);     }     public static String getTenantId() {         return CONTEXT.get();     }     public static void clear() {         CONTEXT.remove();     } }<\/code><\/pre>\n<h4>\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 (Tenant Datasources)<\/h4>\n<p>\u0412 \u043d\u0430\u0448\u0435\u0439 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0435 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 Redis, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u0431\u0430\u0437\u0443 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445 (master database), \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0446\u0435\u043d\u0442\u0440\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u0432\u0441\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043a\u043b\u0438\u0435\u043d\u0442\u0430.&nbsp;\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0438\u0437 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0438 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u0442\u0441\u044f \u0438\u0437 \u0431\u0430\u0437\u044b \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445 .<\/p>\n<p><code>RedisDatasourceService.java<\/code>&nbsp;\u2014 \u044d\u0442\u043e \u043a\u043b\u0430\u0441\u0441, \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0449\u0438\u0439 \u0437\u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u043c\u0438 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f\u043c\u0438 \u0441 \u0431\u0430\u0437\u043e\u0439 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445 .<\/p>\n<pre><code class=\"java\">@Service public class RedisDatasourceService {      private final RedisTemplate redisTemplate;     private final ApplicationProperties applicationProperties;     private final DataSourceProperties dataSourceProperties;  \t\tpublic RedisDatasourceService(RedisTemplate redisTemplate, ApplicationProperties applicationProperties, DataSourceProperties dataSourceProperties) {         this.redisTemplate = redisTemplate;         this.applicationProperties = applicationProperties;         this.dataSourceProperties = dataSourceProperties;     }          \/**      * Save tenant datasource infos      *      * @param tenantDatasource data of datasource      * @return status if true save successfully , false error      *\/          public boolean save(TenantDatasource tenantDatasource) {         try {             Map ruleHash = new ObjectMapper().convertValue(tenantDatasource, Map.class);             redisTemplate.opsForHash().put(applicationProperties.getServiceKey(), String.format(\"%s_%s\", applicationProperties.getTenantKey(), tenantDatasource.getAlias()), ruleHash);             return true;         } catch (Exception e) {             return false;         }     }          \/**      * Get all of keys      *      * @return list of datasource      *\/           public List findAll() {         return redisTemplate.opsForHash().values(applicationProperties.getServiceKey());     }          \/**      * Get datasource      *      * @return map key and datasource infos      *\/           public Map&lt;String, TenantDatasource&gt; loadServiceDatasources() {         List&lt;Map&lt;String, Object&gt;&gt; datasourceConfigList = findAll();         \/\/ Save datasource credentials first time         \/\/ In production mode, this part can be skip         if (datasourceConfigList.isEmpty()) {             List&lt;DataSourceProperties.Tenant&gt; tenants = dataSourceProperties.getDatasources();             tenants.forEach(d -&gt; {                 TenantDatasource tenant = TenantDatasource.builder()                         .alias(d.getAlias())                         .database(d.getDatabase())                         .host(d.getHost())                         .port(d.getPort())                         .username(d.getUsername())                         .password(d.getPassword())                         .build();                 save(tenant);             });         }         return getDataSourceHashMap();     }          \/**      * Get all tenant alias      *      * @return list of alias      *\/           public List&lt;String&gt; getTenantsAlias() {         \/\/ get list all datasource for this microservice         List&lt;Map&lt;String, Object&gt;&gt; datasourceConfigList = findAll();         return datasourceConfigList.stream().map(data -&gt; (String) data.get(\"alias\")).collect(Collectors.toList());     }          \/**      * Fill the data sources list.      *      * @return Map&lt;String, TenantDatasource&gt;      *\/           private Map&lt;String, TenantDatasource&gt; getDataSourceHashMap() {         Map&lt;String, TenantDatasource&gt; datasourceMap = new HashMap&lt;&gt;();         \/\/ get list all datasource for this microservice         List&lt;Map&lt;String, Object&gt;&gt; datasourceConfigList = findAll();         datasourceConfigList.forEach(data -&gt; datasourceMap.put(String.format(\"%s_%s\", applicationProperties.getTenantKey(), (String) data.get(\"alias\")), new TenantDatasource((String) data.get(\"alias\"), (String) data.get(\"host\"), (int) data.get(\"port\"), (String) data.get(\"database\"), (String) data.get(\"username\"), (String) data.get(\"password\"))));         return datasourceMap;     } }<\/code><\/pre>\n<p>\u0412 \u044d\u0442\u043e\u043c \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0435 \u043c\u044b \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u043b\u0438 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0435 \u0438\u0437 yml-\u0444\u0430\u0439\u043b\u0430 <code>(tenants.yml).&nbsp;<\/code>\u0412 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u043c \u0440\u0435\u0436\u0438\u043c\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0435 \u0442\u043e\u0447\u043a\u0438 \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0435 \u0432 \u0431\u0430\u0437\u0435 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0427\u0442\u043e\u0431\u044b \u0438\u043c\u0435\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 mongo, \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c&nbsp;\u043a\u043b\u0430\u0441\u0441 <code>MultiTenantMongoDBFactory<\/code>, \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u044e\u0449\u0438\u0439  \u043a\u043b\u0430\u0441\u0441 <code>SimpleMongoClientDatabaseFactory \u0438\u0437 org.springframework.data.mongodb.core<\/code>.&nbsp;\u041e\u043d \u0432\u0435\u0440\u043d\u0435\u0442&nbsp;\u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440<code>MongoDatabase<\/code>, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0439 \u0441 \u0442\u0435\u043a\u0443\u0449\u0438\u043c \u0430\u0440\u0435\u043d\u0434\u0430\u0442\u043e\u0440\u043e\u043c.<\/p>\n<pre><code class=\"java\">@Configuration public class MultiTenantMongoDBFactory extends SimpleMongoClientDatabaseFactory {  \t\t@Autowired     MongoDataSources mongoDataSources;  \t\tpublic MultiTenantMongoDBFactory(@Qualifier(\"getMongoClient\") MongoClient mongoClient, String databaseName) {         super(mongoClient, databaseName);     }      @Override     protected MongoDatabase doGetMongoDatabase(String dbName) {         return mongoDataSources.mongoDatabaseCurrentTenantResolver();     } }<\/code><\/pre>\n<p>\u041d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c&nbsp;\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 <code>MongoDBFactoryMultiTenant&nbsp;<\/code>\u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e (&nbsp;<code>MongoClient&nbsp;<\/code>\u0438&nbsp;<code>databaseName<\/code>).<\/p>\n<p>\u042d\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u043f\u0440\u043e\u0437\u0440\u0430\u0447\u043d\u044b\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0430.&nbsp;<\/p>\n<pre><code class=\"java\">@Component @Slf4j public class MongoDataSources {      \/**      * Key: String tenant alias      * Value: TenantDatasource      *\/     private Map&lt;String, TenantDatasource&gt; tenantClients;      private final ApplicationProperties applicationProperties;     private final RedisDatasourceService redisDatasourceService;      public MongoDataSources(ApplicationProperties applicationProperties, RedisDatasourceService redisDatasourceService) {         this.applicationProperties = applicationProperties;         this.redisDatasourceService = redisDatasourceService;     }      \/**      * Initialize all mongo<\/code><\/pre>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-318760","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/318760","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=318760"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/318760\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=318760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=318760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=318760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}