简体   繁体   English

我应该如何设计Spring Microservice?

[英]How should I design my Spring Microservice?

I am trying to create a Microservice architecture for a hobby project and I am confused about some decisions. 我正在尝试为一个业余项目创建微服务Microservice architecture ,但我对某些决定感到困惑。 Can you please help me as I never worked using Microservice before? 因为我以前从未使用过微服务,您能帮我吗?

  1. One of my requirements is that my AngularJS GUI will need to show some drop-down or List of values (example: a list of countries). 我的要求之一是我的AngularJS GUI需要显示一些下拉列表或值列表(例如:国家/地区列表)。 This can be fetched using a Microservice REST call, but where should the values come from? 这可以使用被获取Microservice REST调用,但应该在哪里值从何而来? Can I fetch these from my Config Server ? 我可以从Config Server获取它们吗? or should it come from Database ? 还是应该来自Database If the latter, then should each of the Microservice have their own Database for lookup value or can it be a common one? 如果是后者,那么每个微服务都应该具有自己的数据库以获取查询值,或者它可以是一个公用数据库吗?
  2. How would server-side validation work in this case? 在这种情况下,服务器端验证将如何工作? I mean, there will certainly be a Microservice call the GUI will make for validation but should the validation service be a common Microservice for all Use Cases/Screens or should it be one per GUI page or should the CRUD Microservice be reused for validation as well? 我的意思是,GUI肯定会调用一个微服务来进行验证,但是验证服务应该是所有用例/屏幕的通用微服务,还是每个GUI页面一个,或者是否应该将CRUD也重新用于验证?
  3. How do I deal with a use-case where the back-end is not a Database but a Web-service call? 我如何处理后端不是数据库而是Web服务调用的用例? Will I need some local DB still to maintain some state in between these calls (especially to take care of scenario where the Web-service call fails) and finally pass on the status to GUI? 我是否仍需要一些本地DB在这些调用之间保持某种状态(尤其是要注意Web服务调用失败的情况),然后将状态最终传递给GUI?

First of all, there is no single way design micro-service , one has to choose according to the use case and project requirement. 首先,没有单向设计微服务 ,必须根据用例和项目要求进行选择。

  1. Can I keep these in a Config Server? 我可以将它们保存在配置服务器中吗? or should it come from Database? 还是应该来自数据库?

Again, it depends upon the use case and requirement. 同样,这取决于用例和需求。 However, because every MS should have their own DB then you can use DB if the countries have only names. 但是,由于每个MS都应该有自己的数据库,所以如果国家/地区只有名称,则可以使用数据库。 But if they have some relationship with City/State then you should use DB only. 但是,如果他们与城市/州有某种关系,则应仅使用DB。

  1. If DB should each of the Microservice have their own DB for lookup value or can it be a common one? 如果数据库应该每个微服务都具有自己的数据库以获取查询值,或者可以是一个通用数据库?

No, IMO multiple MS should not depend on a single DB.Because if the DB fails then all the MS will fail, which should not be done. 否,IMO多个MS不应依赖于单个DB,因为如果DB发生故障,则所有MS都将失败,因此不应该这样做。 Each MS should work alone with depending on other DB or MS. 每个MS应该与其他DB或MS一起单独工作。

  1. should the validation service be a common microservice for all UseCases/Screens 验证服务应为所有UseCases / Screens的通用微服务

Same as point 2 与第2点相同

  1. How do I deal with a use-case where the backend is not a Database call but another Web-service call? 在后端不是数据库调用而是另一个Web服务调用的情况下,如何处理? Will I need some local DB still to maintain some state in between these calls and finally pass on the status to GUI? 我是否仍需要一些本地数据库在这些调用之间保持某种状态,然后将状态最终传递给GUI?

If you are using HTTP then you should not save the state of any request. 如果使用的是HTTP,则不应保存任何请求的状态。 If you want to redirect the request to another MS then you can use Feign client which provides a very good way to call rest-api and other important features like: Load balancing . 如果要将请求重定向到另一个MS,则可以使用Feign客户端 ,该客户端提供了一种很好的方式来调用rest-api和其他重要功能,例如: Load balancing

Microservice architecture is simple. 微服务架构很简单。 Here we divide each task into separate services(like Spring-boot application). 在这里,我们将每个任务划分为单独的服务(例如Spring-boot应用程序)。 Example in every application there will be login function,registration function so on..each of these will a separate services in micro-service architecture. 在每个应用程序示例中都会有登录功能,注册功能等等。每一个都会在微服务架构中提供独立的服务。

1.You can store that in database, since in feature if you want add more values it is easy to add. 1.您可以将其存储在数据库中,因为如果要添加更多值,可以轻松添加。 You can maintain separate or single db. 您可以维护单独或单个数据库。 Single db with separate collections or table for each microservices. 每个微服务具有单独集合或表的单个数据库。

  1. Validation means you are asking about who can use which microservice(Role based access)??? 验证意味着您正在询问谁可以使用哪种微服务(基于角色的访问)???

3.I think you have to use local db. 3.我认为你必须使用本地数据库。

Microservices is a collection loosely coupled services. 微服务是一组松散耦合的服务。 For example, if you are creating an ecommerce application, user management can be a service, order management can be a service and refund & chargeback management can be another service. 例如,如果您正在创建电子商务应用程序,则用户管理可以是一项服务, 订单管理可以是一项服务, 退款和退款管理可以是另一项服务。 Now each of these services can be further divided into smaller units, lets call them API Endpoints. 现在,这些服务中的每一个都可以进一步划分为较小的单元,可以将它们称为API端点。 For example - user management can have login as an endpoint and signup as another endpoint. 例如, 用户管理可以将登录名作为端点,将注册名作为另一个端点。

If you want to leverage the power of Microservice architecture in its true sense, here is what I would suggest. 如果您想真正利用微服务架构的功能,这就是我的建议。 For the above example, create 3 Springboot Applications for each service. 对于上面的示例,为每个服务创建3个Springboot应用程序。 First thing that you should do after this, is establish trust between those applications. 此后,您应该做的第一件事是在这些应用程序之间建立信任。 I would prefer JWTs for trust establishment. 我希望JWT建立信任关系。 After that everything is a piece of cake. 之后,一切都是小菜一碟。 Here are the answers you are looking for : 这是您要寻找的答案:

  1. You should ideally use a database, as opposed to keeping the values in config server, for fetching a list of countries so that you need not recompile your code every time a new country is added. 理想情况下,应该使用数据库而不是将值保存在配置服务器中来获取国家/地区列表,这样就不必在每次添加新国家/地区时都重新编译代码。

  2. You can easily restrict access using @PreAuthorize if Role based access is what you are referring to. 如果您要指的是基于角色的访问,则可以使用@PreAuthorize轻松限制访问。

  3. You can use OkHttp or any other HttpClient in this usecase. 您可以在此用例中使用OkHttp或任何其他HttpClient。 And you certainly need not maintain any local db. 而且您当然不需要维护任何本地数据库。 However, you can cache the output of the webservice call if that is a requirement. 但是,如果需要,您可以缓存webservice调用的输出。

PS: Establishing trust between microservices can be a complex task if you dont understand all the delicacies. PS:如果您不了解所有美味佳肴,那么在微服务之间建立信任可能是一项复杂的任务。 In which case, I would recommend going ahead with a single Springboot application; 在这种情况下,我建议继续使用单个Springboot应用程序。 which is a monolithic architecture. 这是一个整体架构。 I would still recommend JWTs though. 我仍然会推荐JWT。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM