使用共享数据库的Spring Boot微服务架构

If you got here, you have probably been researching about microservices projects using shared databases. I suppose you found a lot of people talking about it, but you didn’t find any code showing it in practice.

如果到达这里,您可能已经在研究使用共享数据库的微服务项目。 我想您发现有很多人在谈论它,但实际上您没有找到任何显示它的代码。

Yes, I know. This is a controversial topic and one that brings a lot of discussions. According to Martin Flower, if you want to apply the concept of microservices to the letter, you probably need to use a decentralized database approach. It means that each microservice has its own database. This may sound confusing, especially if you coming from the monolithic world. Flower discussed this topic and pointed out some issues to be considered involving decentralized data management. In general, you should consider an additional effort for decentralized update management and integrity assurance to avoid inconsistencies.

是的我知道。 这是一个有争议的主题,并且引起了很多讨论。 根据Martin Flower的说法,如果要将微服务的概念应用到这封信中,则可能需要使用分散式数据库方法。 这意味着每个微服务都有自己的数据库。 这听起来可能令人困惑,尤其是如果您来自整体世界。 Flower讨论了这个主题,并指出了涉及分散数据管理的一些问题。 通常,您应该考虑在分散式更新管理和完整性保证方面做出额外的努力,以避免不一致。

This can make sense in small applications but it can get a little confusing for large ERP systems, or even for the migration of large monolithic systems highly dependent on centralized databases to a microservice architecture. As Flower said, error is acceptable as long as the cost for the correction is offset by the gain in speed to meet demand.

在小型应用程序中这可能很有意义,但是对于大型ERP系统,甚至对于高度依赖集中式数据库的大型整体式系统向微服务体系结构的迁移,都可能会造成一些混乱。 正如Flower所说,只要校正成本被满足需求的速度提高所抵消,误差就可以接受。

So, what to do? My answer is: it depends on your need.

那么该怎么办? 我的答案是:这取决于您的需要。

Let’s remember that there are several supposed benefits to using a microservice architecture. Decoupling parts of the system and transforming them into isolated and independent microservices can allow the allocation of more computational resources to the most requested components. Another interesting point is that each microservice can be written in a different programming language, since they communicate using standardized mechanisms, such as HTTP APIs.

让我们记住,使用微服务架构有几个假定的好处。 解耦系统的各个部分并将它们转换为独立的独立微服务,可以为更多要求的组件分配更多的计算资源。 另一个有趣的一点是,由于每个微服务都使用标准化机制(例如HTTP API)进行通信,因此可以用不同的编程语言编写。

There are several reasons for using a microservice architecture when starting a new project or even migrating existing systems. You are not required to be looking for all the benefits of microservices to decide this. You may want only a few advantages, and not necessarily about databases.

在启动新项目甚至迁移现有系统时,使用微服务架构的原因有很多。 您不需要寻找微服务的所有好处来决定这一点。 您可能只需要一些优点,而不一定需要数据库。

What I mean is that, in my opinion, you are not required to strictly follow all microservice architecture concepts. If your project needs? Great! If not, you can use a single database on different microservices without being crucified for it. After all, you may have issues at the backend layer and not at the database.

我的意思是,我认为您不需要严格遵循所有微服务架构概念。 如果您的项目需要? 大! 否则,您可以在不同的微服务上使用单个数据库,而不必为此而被钉死。 毕竟,您可能在后端层而不是数据库上遇到问题。

So, let’s see how to do this in practice, using Spring Cloud and Netflix libraries.

因此,让我们看看如何使用Spring Cloud和Netflix库在实践中做到这一点。

Since part of the libraries used to deal with microservices were developed by Netflix, let’s use as an example an application called netflix-simulator.

由于用于处理微服务的部分库是由Netflix开发的,因此以一个名为netflix-simulator的应用程序为例。

It is not the purpose of this article to detail the complete construction of a Spring Boot microservice. I suggest you download the source code from my github repository to follow the explanation.

本文的目的不是详细介绍Spring Boot微服务的完整构造。 我建议您从我的github存储库下载源代码以按照说明进行操作。

This article will focus on presenting the shared database approach. To understand the DevOps approach implemented to compile and run the entire microservice stack in an automated way using Maven, Docker and shell script, take a look at:

本文将重点介绍共享数据库方法。 要了解使用Maven,Docker和Shell脚本以自动化方式编译和运行整个微服务堆栈的DevOps方法,请看一下:

https://medium.com/@marceloh.web/deploy-spring-boot-microservices-monorepo-project-with-docker-compose-ae4abbe8d2b4

https://medium.com/@marceloh.web/deploy-spring-boot-microservices-monorepo-project-with-docker-compose-ae4abbe8d2b4

Netflix Simulator is a set of three microservices, where two of them use the same Mysql database and the other uses an instance of a MongoDB.

Netflix Simulator是一组三个微服务,其中两个使用相同的Mysql数据库,另一个使用MongoDB的实例。

Eureka Service Discovery library was used to register the microservices. Zull Gateway was used to receive requests and forward them to microservices. And finally, Spring Cloud Config library to serve as a centralized point for storing configuration data for microservices.

Eureka服务发现库用于注册微服务。 Zull网关用于接收请求并将其转发到微服务。 最后,Spring Cloud Config库用作存储微服务配置数据的集中点。

Therefore, the following figure shows an overview of the project architecture.

因此,下图显示了项目体系结构的概述。

Image for post

The proposed approach started with the creation of a project to group the entities common to the two microservices. In this case, Category and Movie entities. This project was called netflix-data. This is not a Spring Boot project, it is a simple java/maven project, with the default jar packaging.

提议的方法始于创建一个项目,以对两个微服务的公共实体进行分组。 在这种情况下,类别和电影实体。 该项目称为netflix-data 。 这不是一个Spring Boot项目,它是一个简单的java / maven项目,带有默认的jar包装。

In summary, this project contains classes with object-relational mapping and DTOs for the Category and Movie entities with their respective JPA annotations.

总而言之,该项目包含具有对象关系映射的类以及类别和电影实体的DTO,以及它们各自的JPA批注。

Therefore, you need to add the netflix-data dependency entry to the netflix-category-microservice and netflix-movie-microservice projects.

因此,您需要将netflix-data依赖项添加到netflix-category-microservicenetflix-movie-microservice项目中。

<dependency>
<groupId>com.soares.microservice</groupId>
<artifactId>netflix-data</artifactId>
<version>1.0.0</version>
</dependency>

Since entities are imported from another project with a different package path, you need to inform the base package in the main class application using the EntityScan annotation. The following code shows this on line 12 for netflix-category-microservice. The same was done for netflix-movie-microservice.

由于实体是从具有不同包路径的另一个项目中导入的,因此您需要使用EntityScan注释在主类应用程序中通知基本包。 以下代码在netflix-category-microservice的第12行显示了这一点。 netflix-movie-microservice的操作也是如此

package com.soares.microservice.category.api;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;


@SpringBootApplication
@EnableDiscoveryClient
@EntityScan(basePackages = "com.soares.microservice.commons.entity")
public class CategoryMicroserviceApplication {


	public static void main(String[] args) {
		SpringApplication.run(CategoryMicroserviceApplication.class, args);
	}
	
	@Bean
	public BCryptPasswordEncoder bCryptPasswordEncoder() {
		return new BCryptPasswordEncoder();
	}


}

The rest of the code architecture for each project follows well-established web development standards. I mean: controller / service / repository. You are probably already comfortable with that, if you don’t, feel free to check everything in the project.

每个项目的其余代码体系结构都遵循公认的Web开发标准。 我的意思是:控制器/服务/存储库。 您可能已经对此感到满意,如果不满意,请随时检查项目中的所有内容。

Once you have defined the base package, you can use repositories by passing these entities as type parameters.

一旦定义了基本包,就可以通过将这些实体作为类型参数传递来使用存储库。

package com.soares.microservice.category.api.repository;


import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;


import com.soares.microservice.commons.entity.CategoryEntity;


/**
 * Repository class for {@link CategoryEntity}
 * 
 * @author Marcelo Soares <marceloh.web@gmail.com>
 *
 */
@Repository("categoryJpaRepository")
public interface ICategoryJpaRepository extends CrudRepository<CategoryEntity, String> {


}

As previously mentioned, the database connection data of each microservice is centralized in netflix-config. This project contains the connection data for Mysql database as well as the connection data for Mongo needed for the netflix-user-microservice. Since this last microservice has its own exclusive database, the User entity is defined within the project itself, and not in a separate project as done with the Category and Movie entities.

如前所述,每个微服务的数据库连接数据都集中在netflix-config中 。 该项目包含Mysql数据库的连接数据以及netflix-user-microservice所需的Mongo连接数据。 由于最后一个微服务具有自己的专有数据库,因此User实体是在项目本身中定义的,而不是像Category和Movie实体那样在单独的项目中定义的。

跑步 (Running)

To run the application, you need the following dependencies:

要运行该应用程序,您需要以下依赖项:

All you need to do is run the following commands:

您所需要做的就是运行以下命令:

git clone https://github.com/marcelohweb/netflix-microservices
cd netflix-microservices
make build
make run

You don’t need java or maven in your machine because this project is compiled inside a docker container.

您无需在计算机中使用Java或Maven,因为此项目是在Docker容器内编译的。

Before you start using, check the container logs to see if all microservices are running. It takes some time.

在开始使用之前,请检查容器日志以查看是否所有微服务都在运行。 需要一些时间。

You can use Portainer to check.

您可以使用Portainer进行检查。

Image for post
Portainer interface showing all running microservices
Portainer界面显示所有正在运行的微服务

You can then access eureka service discovery here: http://localhost:8010/

然后,您可以在此处访问eureka服务发现: http:// localhost:8010 /

Username: userPassword: user

用户名:userPassword:用户

You will see all registered microservices as the following image:

您将看到所有已注册的微服务,如下图所示:

Image for post
Eureka Service Discovery interface showing all registered microservices
Eureka服务发现界面显示所有已注册的微服务

用法 (Usage)

You can use this API by importing the Postman collection located in the postman directory.

您可以通过导入位于postman目录中的Postman集合来使用此API。

First of all, you need to create a user using user-create request.

首先,您需要使用user-create request创建一个用户。

Image for post
netflix-user-microservice netflix-user-microservice上创建一个新用户

After that, use user-login request to authenticate and get the token. The token is in the header response.

之后,使用用户登录请求进行身份验证并获取令牌。 令牌在标头响应中。

Image for post
netflix-user-microservice authentication netflix用户微服务认证

Then, access http://localhost:8081/ to visualize the mongo database via Mongo Express.

然后,访问http:// localhost:8081 /以通过Mongo Express可视化mongo数据库。

Username: userPassword: password

用户名:userPassword:密码

Image for post
Mongo Express admin interface — databases
Mongo Express管理界面-数据库

As you can see, there is a database called db_netflix_app_user. Click on it to explore and then click on user collection to verify the user you created.

如您所见,有一个名为db_netflix_app_user的数据库。 单击它进行浏览,然后单击用户集合以验证您创建的用户。

Image for post
Mongo Express admin interface — user collection
Mongo Express管理界面-用户集合

So now, let’s create category and film. Return to Postman and open category-create request. Go to Authorization tab, select Bearer Token, then paste the token into the field.

现在,让我们创建类别和电影。 返回邮递员并打开类别创建请求。 转到“授权”选项卡,选择“承载令牌”,然后将令牌粘贴到字段中。

Image for post
Postman — authentication token
邮递员-身份验证令牌

Once the token is set, create a category.

设置令牌后,创建一个类别。

Image for post

Copy the generated id to the category and use it to create a movie using the movie-create request. Remember you also need to put the token to call movie microservice.

将生成的ID复制到类别,并使用它通过movie-create请求创建电影 。 请记住,您还需要放入令牌以调用电影微服务。

Image for post

After creating a movie, use category-get-all request to view all records.

创建电影后,使用category-get-all请求查看所有记录。

Image for post

You can also view mysql data via phpmyadmin running at http://localhost:8080/

您还可以通过运行在http:// localhost:8080 /的 phpmyadmin查看mysql数据。

Username: userPassword: password

用户名:userPassword:密码

结论 (Conclusion)

In this article, we discuss microservice architecture with shared databases. The proof of concept presented demonstrates an approach to the use of the same database by multiple microservices. Although this is a very controversial topic, my opinion is that you don’t need to migrate your entire existing stack to a microservice architecture just because there are a lot of people talking about it. Managing microservices is not an easy task. In some cases, a monolithic architecture can solve your problem. If you think that the benefits of this architecture outweigh the effort of a migration, you shouldn’t be hated if you need to use centralized databases. It all depends on need.

在本文中,我们讨论了具有共享数据库的微服务体系结构。 提出的概念证明证明了多种微服务使用同一数据库的方法。 尽管这是一个非常有争议的话题,但是我认为您不必将整个现有堆栈迁移到微服务架构,因为有很多人在谈论它。 管理微服务并非易事。 在某些情况下,单片架构可以解决您的问题。 如果您认为此体系结构的好处胜于迁移工作,那么在需要使用集中式数据库的情况下,您就不会讨厌。 这一切都取决于需要。

翻译自: https://medium.com/@marceloh.web/spring-boot-microservices-architecture-using-shared-database-69312a968530

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值