CN117573111A - A microservice deployment method, device, equipment and storage medium - Google Patents
A microservice deployment method, device, equipment and storage medium Download PDFInfo
- Publication number
- CN117573111A CN117573111A CN202311520036.8A CN202311520036A CN117573111A CN 117573111 A CN117573111 A CN 117573111A CN 202311520036 A CN202311520036 A CN 202311520036A CN 117573111 A CN117573111 A CN 117573111A
- Authority
- CN
- China
- Prior art keywords
- micro
- service
- microservice
- deployment
- module
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
Classifications
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/30—Creation or generation of source code
- G06F8/36—Software reuse
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/70—Software maintenance or management
- G06F8/71—Version control; Configuration management
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/44—Arrangements for executing specific programs
- G06F9/448—Execution paradigms, e.g. implementations of programming paradigms
- G06F9/4488—Object-oriented
- G06F9/449—Object-oriented method invocation or resolution
-
- Y—GENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
- Y02—TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
- Y02D—CLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
- Y02D10/00—Energy efficient computing, e.g. low power processors, power management or thermal management
Landscapes
- Engineering & Computer Science (AREA)
- Software Systems (AREA)
- Theoretical Computer Science (AREA)
- General Engineering & Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Physics & Mathematics (AREA)
- Computer Security & Cryptography (AREA)
- Stored Programmes (AREA)
Abstract
Description
技术领域Technical field
本申请实施例涉及微服务领域,尤其涉及一种微服务部署方法、装置、设备及存储介质。Embodiments of the present application relate to the field of microservices, and in particular, to a microservice deployment method, device, equipment and storage medium.
背景技术Background technique
目前的技术开发框架选型,只能在系统设计阶段选定单体应用架构或者微服务架构其中一种,再选择对应的技术开发框架进行开发和部署,无法实现只开发一套系统,就能根据不同目标客户的情况进行单体应用部署或微服务方式部署。在同一套业务系统提供给两种不同部署需求的客户情况下,需要在两种架构上分别进行开发,系统无法复用,在降低了开发效率的同时,资源也会重复浪费。另外在产品升级时,两种架构也要同步升级,维护工作量翻倍。The current technology development framework selection can only select a single application architecture or a microservice architecture during the system design stage, and then select the corresponding technology development framework for development and deployment. It is impossible to develop only one system. Deploy single applications or microservices based on the needs of different target customers. When the same business system is provided to two customers with different deployment requirements, they need to be developed separately on the two architectures. The system cannot be reused, which reduces development efficiency and wastes resources repeatedly. In addition, when the product is upgraded, the two architectures must be upgraded simultaneously, doubling the maintenance workload.
综上所述,现有技术中的业务系统存在着开发效率低下以及维护工作量大的技术问题。To sum up, business systems in the existing technology have technical problems such as low development efficiency and heavy maintenance workload.
发明内容Contents of the invention
本发明实施例提供了一种微服务部署方法、装置、设备以及存储介质,能够在一个开发框架内实现单体应用部署或微服务方式部署,提高了业务系统的开发效率以及降低了业务系统的维护工作量,解决了现有技术中的业务系统存在着开发效率低下以及维护工作量大的技术问题。Embodiments of the present invention provide a microservice deployment method, device, equipment and storage medium, which can realize single application deployment or microservice deployment within a development framework, improve the development efficiency of the business system and reduce the cost of the business system. Maintenance workload solves the technical problems of low development efficiency and heavy maintenance workload in business systems in the existing technology.
第一方面,本发明实施例提供了一种微服务部署方法,包括:In a first aspect, embodiments of the present invention provide a microservice deployment method, including:
在所有微服务中确定出主微服务,将其他微服务确定为副微服务,并将所述副微服务作为所述主微服务的依赖项;Determine the main microservice among all microservices, determine other microservices as secondary microservices, and use the secondary microservice as a dependency of the main microservice;
确定所述微服务的部署方式;Determine how the microservices will be deployed;
在启动所述主微服务时,通过Spring模块扫描所述主微服务的代码以及所述所有微服务的配置文件,所述配置文件中设置有每个所述微服务的自动配置类;When starting the main microservice, scan the code of the main microservice and the configuration files of all microservices through the Spring module, and the automatic configuration class of each microservice is set in the configuration file;
当所述部署方式为单体应用部署时,通过Spring模块扫描每个所述副微服务的自动配置类所在路径的所有代码以获取每个所述副微服务的代码,根据每个所述副微服务的代码由Spring模块加载与每个所述副微服务相对应的功能组件,以及根据所述主微服务的代码由Spring模块加载与所述主微服务相对应的功能组件;When the deployment method is single application deployment, all codes in the path where the automatic configuration class of each secondary microservice is located are scanned through the Spring module to obtain the code of each secondary microservice. The code of the microservice is loaded by the Spring module with the functional components corresponding to each of the secondary microservices, and the functional components corresponding to the main microservice are loaded by the Spring module according to the code of the main microservice;
当所述部署方式为微服务部署时,根据所述主微服务的代码由Spring模块加载与所述主微服务相对应的功能组件;When the deployment method is microservice deployment, the Spring module loads the functional components corresponding to the main microservice according to the code of the main microservice;
调用MyBatis模块从已加载的功能组件中获取预设的调用接口实现类,由所述MyBatis模块通过所述调用接口实现类获取与功能组件已加载的目标微服务相对应的初始化脚本并缓存,以当所述目标微服务被外部调用时,由所述Spring模块执行相对应的功能组件,并由所述MyBatis模块通过执行所述初始化脚本从数据库中获取或更新与所述目标微服务相对应的业务数据,所述数据库用于储存所述微服务的业务数据。Call the MyBatis module to obtain the preset calling interface implementation class from the loaded functional component. The MyBatis module obtains and caches the initialization script corresponding to the target microservice loaded by the functional component through the calling interface implementation class, so as to When the target microservice is called externally, the Spring module executes the corresponding functional component, and the MyBatis module obtains or updates the data corresponding to the target microservice from the database by executing the initialization script. Business data, the database is used to store the business data of the microservice.
第二方面,本发明实施例提供了一种微服务部署装置,包括:In a second aspect, embodiments of the present invention provide a microservice deployment device, including:
主微服务确定模块,用于在所有微服务中确定出主微服务,将其他微服务确定为副微服务,并将所述副微服务作为所述主微服务的依赖项;The main microservice determination module is used to determine the main microservice among all microservices, determine other microservices as secondary microservices, and use the secondary microservice as a dependency of the main microservice;
部署方式设置模块,用于确定所述微服务的部署方式;A deployment mode setting module is used to determine the deployment mode of the microservice;
服务扫描模块,用于在启动所述主微服务时,通过Spring模块扫描所述主微服务的代码以及所述所有微服务的配置文件,所述配置文件中设置有每个所述微服务的自动配置类;The service scanning module is used to scan the code of the main microservice and the configuration files of all microservices through the Spring module when starting the main microservice. The configuration file is set with the configuration files of each microservice. Automatic configuration class;
应用部署模块,用于当所述部署方式为单体应用部署时,通过Spring模块扫描每个所述副微服务的自动配置类所在路径的所有代码以获取每个所述副微服务的代码,根据每个所述副微服务的代码由Spring模块加载与每个所述副微服务相对应的功能组件,以及根据所述主微服务的代码由Spring模块加载与所述主微服务相对应的功能组件;An application deployment module, configured to use the Spring module to scan all codes in the path where the automatic configuration class of each secondary microservice is located to obtain the code of each secondary microservice when the deployment method is single application deployment, The functional components corresponding to each secondary microservice are loaded by the Spring module according to the code of each secondary microservice, and the functional components corresponding to the primary microservice are loaded by the Spring module according to the code of the primary microservice. functional components;
微服务部署模块,用于当所述部署方式为微服务部署时,根据所述主微服务的代码由Spring模块加载与所述主微服务相对应的功能组件;A microservice deployment module, used to load the functional components corresponding to the main microservice by the Spring module according to the code of the main microservice when the deployment mode is microservice deployment;
微服务执行模块,用于调用MyBatis模块从已加载的功能组件中获取预设的调用接口实现类,由所述MyBatis模块通过所述调用接口实现类获取与功能组件已加载的目标微服务相对应的初始化脚本并缓存,以当所述目标微服务被外部调用时,由所述Spring模块执行相对应的功能组件,并由所述MyBatis模块通过执行所述初始化脚本从数据库中获取或更新与所述目标微服务相对应的业务数据,所述数据库用于储存所述微服务的业务数据。The microservice execution module is used to call the MyBatis module to obtain the preset calling interface implementation class from the loaded functional component. The MyBatis module obtains the target microservice corresponding to the loaded functional component through the calling interface implementation class. The initialization script is cached, so that when the target microservice is called externally, the Spring module executes the corresponding functional component, and the MyBatis module obtains or updates the relevant information from the database by executing the initialization script. business data corresponding to the target microservice, and the database is used to store the business data of the microservice.
第三方面,本发明实施例提供了一种微服务部署设备,所述微服部署设备包括处理器以及存储器;In a third aspect, embodiments of the present invention provide a microservice deployment device, which includes a processor and a memory;
所述存储器用于存储计算机程序,并将所述计算机程序传输给所述处理器;The memory is used to store a computer program and transmit the computer program to the processor;
所述处理器用于根据所述计算机程序中的指令执行如第一方面所述的一种微服务部署方法。The processor is configured to execute a microservice deployment method as described in the first aspect according to instructions in the computer program.
第四方面,本发明实施例提供了一种存储计算机可执行指令的存储介质,所述计算机可执行指令在由计算机处理器执行时用于执行如第一方面所述的一种微服务部署方法。In a fourth aspect, embodiments of the present invention provide a storage medium that stores computer-executable instructions, which when executed by a computer processor are used to execute a microservice deployment method as described in the first aspect. .
上述,本发明实施例提供了一种微服务部署方法、装置、设备以及存储介质,本发明实施例在微服务的部署过程中,用户可以在单体应用部署或者微服务部署中根据需要选择部署方式,后续本发明实施例即可根据用户所选择的部署方式部署相对应的微服务。本发明实施例能够根据不同目标客户的资金或运维资源投入情况,对微服务进行单体应用或微服务方式部署,在开发业务系统的过程中无需在不同的架构上进行开发,提高了业务系统的开发效率以及降低了维护工作量,解决了现有技术中的业务系统存在着开发效率低下以及维护工作量大的技术问题。As mentioned above, embodiments of the present invention provide a microservice deployment method, device, equipment and storage medium. During the deployment process of microservices in the embodiment of the present invention, users can choose to deploy as needed in single application deployment or microservice deployment. method, subsequent embodiments of the present invention can deploy corresponding microservices according to the deployment method selected by the user. The embodiments of the present invention can deploy microservices in a single application or in a microservice manner according to the investment of funds or operation and maintenance resources of different target customers. In the process of developing the business system, there is no need to develop on different architectures, which improves business efficiency. The development efficiency of the system and the reduction of maintenance workload solve the technical problems of low development efficiency and heavy maintenance workload of business systems in the existing technology.
附图说明Description of the drawings
图1为本发明实施例提供的单体应用部署的框架示意图。Figure 1 is a schematic framework diagram of a single application deployment provided by an embodiment of the present invention.
图2为本发明实施例提供的微服务部署的框架示意图。Figure 2 is a schematic framework diagram of microservice deployment provided by an embodiment of the present invention.
图3为本发明实施例提供的一种微服务部署方法的流程示意图。Figure 3 is a schematic flowchart of a microservice deployment method provided by an embodiment of the present invention.
图4为本发明实施例提供的一种包名规划的示意图。Figure 4 is a schematic diagram of a package name planning provided by an embodiment of the present invention.
图5为本发明实施例提供的一种微服务部署方法的方法原理图。Figure 5 is a schematic diagram of a microservice deployment method provided by an embodiment of the present invention.
图6为本发明实施例提供的一种微服务部署装置的结构示意图。Figure 6 is a schematic structural diagram of a microservice deployment device provided by an embodiment of the present invention.
图7为本发明实施例提供的一种微服务部署设备的结构示意图。Figure 7 is a schematic structural diagram of a microservice deployment device provided by an embodiment of the present invention.
具体实施方式Detailed ways
以下描述和附图充分地示出本申请的具体实施方案,以使本领域的技术人员能够实践它们。实施例仅代表可能的变化。除非明确要求,否则单独的部件和功能是可选的,并且操作的顺序可以变化。一些实施方案的部分和特征可以被包括在或替换其他实施方案的部分和特征。本申请的实施方案的范围包括权利要求书的整个范围,以及权利要求书的所有可获得的等同物。在本文中,各实施方案可以被单独地或总地用术语“发明”来表示,这仅仅是为了方便,并且如果事实上公开了超过一个的发明,不是要自动地限制该应用的范围为任何单个发明或发明构思。本文中,诸如第一和第二等之类的关系术语仅仅用于将一个实体或者操作与另一个实体或操作区分开来,而不要求或者暗示这些实体或操作之间存在任何实际的关系或者顺序。而且,术语“包括”、“包含”或者其任何其他变体意在涵盖非排他性的包含,从而使得包括一系列要素的过程、方法或者设备不仅包括那些要素,而且还包括没有明确列出的其他要素。本文中各个实施例采用递进的方式描述,每个实施例重点说明的都是与其他实施例的不同之处,各个实施例之间相同相似部分互相参见即可。对于实施例公开的结构、产品等而言,由于其与实施例公开的部分相对应,所以描述的比较简单,相关之处参见方法部分说明即可。The following description and drawings illustrate specific embodiments of the application sufficiently to enable those skilled in the art to practice them. The examples represent only possible variations. Unless explicitly required, individual components and features are optional and the order of operations may vary. Portions and features of some embodiments may be included in or substituted for those of other embodiments. The scope of embodiments of the present application includes the entire scope of the claims, and all available equivalents of the claims. Each embodiment may be referred to herein, individually or collectively, by the term "invention" for convenience only and is not intended to automatically limit the scope of the application to any one if the fact that more than one invention is disclosed is disclosed. A single invention or inventive concept. Herein, relational terms such as first, second, etc. are used only to distinguish one entity or operation from another entity or operation without requiring or implying that any actual relationship exists between these entities or operations or order. Furthermore, the terms "comprises," "comprises," or any other variation thereof are intended to cover a non-exclusive inclusion, such that a process, method, or apparatus that includes a list of elements includes not only those elements, but also others not expressly listed. elements. Each embodiment in this article is described in a progressive manner. Each embodiment focuses on its differences from other embodiments. The same and similar parts between the various embodiments can be referred to each other. For the structures, products, etc. disclosed in the embodiments, since they correspond to the parts disclosed in the embodiments, the descriptions are relatively simple. For relevant details, please refer to the description of the method section.
目前市面上的微服务开发框架,只要是在开发时,使用该框架开发了多个微服务,则每个微服务在部署时只能采用微服务部署,即每个微服务独立部署,所有微服务无法打包成一个独立的单体应用进行部署。The current microservice development framework on the market, as long as multiple microservices are developed using this framework during development, each microservice can only be deployed using microservice deployment, that is, each microservice is deployed independently, and all microservices are deployed independently. Services cannot be packaged into an independent single application for deployment.
其中,单体应用部署的架构如图1所示,单体应用部署是一种把系统中所有的功能、模块耦合在一个应用中的架构方式,单体应用是作为一个不可分割的单元构建的,单体应用最终会打包成一个部署包。单体应用架构的优势为:(1)上手容易,学习成本低,技术栈简单。(2)易于部署,单体应用部署的复杂度要比微服务部署低得多。(3)适合小型团队开发对业务复杂度不高,对应用扩展能力要求不高的应用程序架构。而单体应用架构的缺点为:(1)随着应用需求的增加以及规模的扩大,由于其代码耦合度高,单体应用程序自身的代码复杂度会很高变得难以维护,一个典型场景为:修改任何一处细节的代码,都会影响整个应用,因此必须全面协调开发、测试、部署。然而这种开发方式会使得软件迭代变得很慢,交付效率低下。(2)扩展性差:很难去针对某个模块进行扩展,只能针对整个应用程序扩展,例如当IO密集型和计算密集型模块混合在一起,则无法独立升级及扩容。(3)技术壁垒高,在单体应用程序中应用新技术极其困难,因为必须重写整个应用程序。Among them, the architecture of single application deployment is shown in Figure 1. Single application deployment is an architectural method that couples all functions and modules in the system into one application. The single application is built as an indivisible unit. , the single application will eventually be packaged into a deployment package. The advantages of the monolithic application architecture are: (1) Easy to get started, low learning cost, and simple technology stack. (2) Easy to deploy, the complexity of single application deployment is much lower than that of microservice deployment. (3) It is suitable for small teams to develop application architectures that do not have high business complexity and do not require high application expansion capabilities. The disadvantages of the monolithic application architecture are: (1) As application requirements increase and the scale expands, due to its high degree of code coupling, the code complexity of the monolithic application itself will be very high and become difficult to maintain. A typical scenario Because: Modifying any detail of the code will affect the entire application, so development, testing, and deployment must be fully coordinated. However, this development method will make software iteration very slow and delivery efficiency low. (2) Poor scalability: It is difficult to expand a certain module and can only expand the entire application. For example, when IO-intensive and computing-intensive modules are mixed together, they cannot be upgraded and expanded independently. (3) Technical barriers are high and it is extremely difficult to apply new technologies in a single application because the entire application must be rewritten.
微服务部署的架构如图2所示,微服务架构提倡将单一应用程序划分成多个小的服务,每个服务能够独立地部署,服务之间互相协调和互相配合,为用户提供最终价值。在微服务架构中,服务被分解为可独立部署的模块,这些模块通过远程调用方法(例如HTTP)相互通信,每个服务都可以独立更新、部署和扩展。微服务架构的优势为:(1)独立组件:所有服务都可以独立部署和更新,从而提供了更大的灵活性。比如:针对IO密集型服务增加内存,针对计算密集型服务增加服务器的CPU等等,达到资源的合理分配。(2)业务关注更加集中:将应用程序分解为更小和更简单的服务后,将更易于业务关注理解和管理,只需专注于与业务目标相关的特定服务即可。(3)更好的可扩展性:微服务另一个优点是每个服务都可以独立缩放。具体的,结合容器化编排管理(k8s、docker等),可以快速的实现服务的扩容与缩放。比如:在网购高峰期,电商可以快速地增加微服务部署的数量以应对更多的并发需求。待高峰过后,可以将微服务容器释放,回收资源。(4)选择技术的灵活性:工程团队不受技术的限制,可以自由地为每个微服务应用各种技术和框架,只要保证接口通信协议一致即可。微服务架构的缺点为:(1)编码复杂度增加:由于微服务架构是分布式系统,因此会产生分布式的数据库操作及分布式事务等复杂度更高的编码需求,需要进行合理的微服务拆分,确定服务边界,保证服务内部高内聚,降低分布式事务等操作出现的概率。(2)额外关注:微服务部署由于其多主机、分布式部署,因此需要更多的额外关注:如统一配置、统一日志记录、统一的指标监控以及统一的运行状况检查等。(3)测试困难:虽然单个服务的测试复杂度降低,但是众多可独立部署的服务使集成测试变的更加困难。The architecture of microservice deployment is shown in Figure 2. The microservice architecture advocates dividing a single application into multiple small services. Each service can be deployed independently, and the services coordinate and cooperate with each other to provide users with the ultimate value. In a microservices architecture, services are decomposed into independently deployable modules that communicate with each other through remote invocation methods (such as HTTP). Each service can be independently updated, deployed, and expanded. The advantages of microservice architecture are: (1) Independent components: All services can be deployed and updated independently, thus providing greater flexibility. For example: increase memory for IO-intensive services, increase server CPU for computing-intensive services, etc., to achieve reasonable allocation of resources. (2) Business focus is more focused: After the application is decomposed into smaller and simpler services, the business focus will be easier to understand and manage, and only the specific services related to the business goals can be focused. (3) Better scalability: Another advantage of microservices is that each service can be scaled independently. Specifically, combined with containerized orchestration management (k8s, docker, etc.), service expansion and scaling can be quickly achieved. For example: During the peak period of online shopping, e-commerce companies can quickly increase the number of microservice deployments to cope with more concurrent demands. After the peak is over, the microservice container can be released and resources can be recycled. (4) Flexibility in selecting technology: The engineering team is not restricted by technology and can freely apply various technologies and frameworks to each microservice, as long as the interface communication protocol is consistent. The disadvantages of the microservice architecture are: (1) Increased coding complexity: Since the microservice architecture is a distributed system, it will generate more complex coding requirements such as distributed database operations and distributed transactions, and reasonable microservices are required. Service splitting determines service boundaries, ensures high cohesion within services, and reduces the probability of operations such as distributed transactions. (2) Additional attention: Due to its multi-host and distributed deployment, microservice deployment requires more additional attention: such as unified configuration, unified logging, unified indicator monitoring, and unified health check, etc. (3) Difficulty in testing: Although the complexity of testing a single service is reduced, many independently deployable services make integration testing more difficult.
由于不同企业的经济实力不同,对于资金雄厚的企业,可以提供大量服务器以支撑微服务的运行,并建立专业运维团队来保障整套微服务体系正常运行,因此比较适合采用微服务部署。而对于资金紧张的企业,若只能提供1,2台服务器部署微服务,且运维人员很少甚至没有,此时则不适合采用微服务部署。Since different companies have different economic strengths, companies with strong financial resources can provide a large number of servers to support the operation of microservices and establish a professional operation and maintenance team to ensure the normal operation of the entire microservice system. Therefore, microservice deployment is more suitable. For enterprises with tight funds, if they can only provide 1 or 2 servers to deploy microservices and have few or even no operation and maintenance personnel, microservice deployment is not suitable at this time.
目前的技术开发框架选型,只能在系统设计阶段选定单体应用架构或者微服务架构其中一种,再选择对应的技术开发框架进行开发和部署,无法实现只开发一套系统,就能根据不同目标客户的情况进行单体应用部署或微服务部署。在同一套业务系统提供给两种不同部署需求的客户情况下,需要在两种架构上分别进行开发,系统无法复用,在降低了开发效率的同时,资源也会重复浪费。另外在产品升级时,两种架构也要同步升级,维护工作量翻倍。The current technology development framework selection can only select a single application architecture or a microservice architecture during the system design stage, and then select the corresponding technology development framework for development and deployment. It is impossible to develop only one system. Deploy single applications or microservices according to the needs of different target customers. When the same business system is provided to two customers with different deployment requirements, they need to be developed separately on the two architectures. The system cannot be reused, which reduces development efficiency and wastes resources repeatedly. In addition, when the product is upgraded, the two architectures must be upgraded simultaneously, doubling the maintenance workload.
综上,为了解决现有技术中的业务系统存在着开发效率低下以及维护工作量大的技术问题,本发明实施例提供了一种微服务部署方法,如图3所示,图3为本发明实施例提供的一种微服务部署方法的流程图。本发明实施例提供的微服务部署方法可以由微服务部署设备执行,该微服务部署设备可以通过软件和/或硬件的方式实现,该微服务部署设备可以是两个或多个物理实体构成,也可以由一个物理实体构成。例如微服务部署设备可以是电脑、上位机、平板等设备。另外本实施例以采用的框架为Isass为例进行说明,Isass全称为Intelligent System Architecture Service Solution中文名为“智能系统架构服务解决方案”,是一个专注于技术研发团队的全生命周期整合解决方案,包括基础设施与中间件、开发框架与流程、运维部署与实施、团队管理与提升等,具体内容可参考现有技术,本实施例中不再进行赘述。本发明实施例提供的一种微服务部署方法包括以下步骤:In summary, in order to solve the technical problems of low development efficiency and heavy maintenance workload in business systems in the prior art, embodiments of the present invention provide a microservice deployment method, as shown in Figure 3. Figure 3 is a diagram of the present invention The embodiment provides a flow chart of a microservice deployment method. The microservice deployment method provided by the embodiment of the present invention can be executed by a microservice deployment device. The microservice deployment device can be implemented in the form of software and/or hardware. The microservice deployment device can be composed of two or more physical entities. It can also be composed of a physical entity. For example, microservice deployment equipment can be computers, host computers, tablets and other devices. In addition, this embodiment uses Isass as an example for illustration. The full name of Isass is Intelligent System Architecture Service Solution. Its Chinese name is "Intelligent System Architecture Service Solution". It is a full life cycle integration solution focusing on technology research and development teams. Including infrastructure and middleware, development framework and process, operation and maintenance deployment and implementation, team management and improvement, etc. The specific content can be referred to the existing technology, and will not be described in detail in this embodiment. A microservice deployment method provided by an embodiment of the present invention includes the following steps:
步骤101、在所有微服务中确定出主微服务,将其他微服务确定为副微服务,并将副微服务作为主微服务的依赖项。Step 101: Determine the main microservice among all microservices, determine other microservices as secondary microservices, and use the secondary microservices as dependencies of the main microservice.
本发明实施例中,首先需要在所有的微服务中确定出主微服务,并将其他微服务确定为副微服务。例如,可以将订单微服务order确定为主微服务,将其他微服务例如用户微服务user和设备微服务device等确定为副微服务。在确定出主微服务后,需要进一步将副微服务作为主微服务的依赖项,即主微服务拥有所有副微服务的所有代码。示例性的,在订单微服务中添加用户微服务的依赖过程为:(1)确定用户微服务的Maven坐标,其中Maven是一款基于Java平台的项目管理和整合工具,它将项目的开发和管理过程抽象成一个项目对象模型(POM)。开发人员只需要做一些简单的配置,Maven就可以自动完成项目的编译、测试、打包、发布以及部署等工作。确定用户微服务的Maven坐标具体代码为:In the embodiment of the present invention, it is first necessary to determine the main microservice among all microservices, and determine other microservices as secondary microservices. For example, the order microservice order can be determined as the main microservice, and other microservices such as user microservice user and device microservice device can be determined as secondary microservices. After the main microservice is determined, the secondary microservice needs to be further used as a dependency of the main microservice, that is, the main microservice owns all the codes of all secondary microservices. For example, the dependency process of adding user microservices to the order microservice is: (1) Determine the Maven coordinates of the user microservice, where Maven is a project management and integration tool based on the Java platform, which combines project development and The management process is abstracted into a project object model (POM). Developers only need to make some simple configurations, and Maven can automatically complete the compilation, testing, packaging, publishing, and deployment of the project. The specific code to determine the Maven coordinates of the user's microservice is:
<groupId>vip.isass</groupId><groupId>vip.isass</groupId>
<artifactId>user-service</artifactId><artifactId>user-service</artifactId>
<version>1.0.0</version><version>1.0.0</version>
(2)在订单微服务添加用户微服务的依赖,具体为在订单微服务pom文件添加:(2) Add the dependencies of the user microservice to the order microservice. Specifically, add the following to the order microservice pom file:
在订单微服务中添加其他副微服务的依赖的原理与上述类似,本实施例中不再进行赘述。The principle of adding dependencies of other secondary microservices to the order microservice is similar to the above, and will not be described again in this embodiment.
步骤102、确定微服务的部署方式。Step 102: Determine the deployment method of the microservice.
在确定出主微服务和副微服务后,需要进一步确定微服务的部署方式,本实施例中微服务的部署方式为单体应用部署或微服务部署。After determining the main microservice and the secondary microservice, it is necessary to further determine the deployment method of the microservice. In this embodiment, the deployment method of the microservice is single application deployment or microservice deployment.
步骤103、在启动主微服务时,通过Spring模块扫描主微服务的代码以及所有微服务的配置文件,配置文件中设置有每个微服务的自动配置类。Step 103. When starting the main microservice, scan the code of the main microservice and the configuration files of all microservices through the Spring module. The automatic configuration class of each microservice is set in the configuration file.
后续在启动主微服务后,可通过Spring模块扫描主微服务的代码以及所有微服务的配置文件,配置文件中设置有每个微服务的自动配置类,其中自动配置类为AutoConfiguration类,自动配置类可预先在每个微服务的配置文件上进行写入。其中Spring模块是一个java轻量级的容器框架,该框架最初是为了解决EJB(sun的JavaEE服务器端组件模型,是一种规范,设计目标与核心应用是部署分布式应用程序)开发笨重臃肿的问题,Spring模块使用的是基本的JavaBean来完成由EJB完成的事情,为J2EE(Java2Platform Enterprise Edition,Java 2平台企业版)提供了另一种简单又实用的解决方案,Spring模块被认为是一个低成本、安全、灵活的框架。After starting the main microservice, you can scan the code of the main microservice and the configuration files of all microservices through the Spring module. The automatic configuration class of each microservice is set in the configuration file. The automatic configuration class is the AutoConfiguration class. Automatic configuration Classes can be pre-written on each microservice's configuration file. The Spring module is a Java lightweight container framework. This framework was originally designed to solve the cumbersome and bloated development of EJB (Sun's JavaEE server-side component model, which is a specification. The design goal and core application is to deploy distributed applications). Problem, the Spring module uses basic JavaBeans to complete things done by EJB, providing another simple and practical solution for J2EE (Java2Platform Enterprise Edition, Java 2 Platform Enterprise Edition). The Spring module is considered a low-level Cost-effective, secure, and flexible framework.
一个实施例中,每个微服务包括部署包,部署包包括扫描包以及业务包,方法还包括:In one embodiment, each microservice includes a deployment package, and the deployment package includes a scanning package and a business package. The method also includes:
预先编写微服务,在微服务的扫描包中标注自动配置类,并添加扫描注解;在微服务的扫描包中标注ServiceScanner类,并添加条件注解;扫描注解用于配置Spring模块扫描非主微服务的自动配置类,条件注解用于配置Spring模块在指定条件下执行与微服务相对应的功能组件的创建与加载。Pre-write the microservice, mark the automatic configuration class in the scanning package of the microservice, and add scanning annotations; mark the ServiceScanner class in the scanning package of the microservice, and add conditional annotations; scanning annotations are used to configure the Spring module to scan non-main microservices The automatic configuration class, conditional annotations are used to configure the Spring module to execute the creation and loading of functional components corresponding to the microservice under specified conditions.
一个实施例中,每个微服务包括部署包,部署包包括有扫描包(scanner包)以及业务包,示例性的,微服务的包名规划如图4所示,图4中vip.isass.auth为用户微服务的包名前缀,而vip.isass.order则为订单微服务的包名前缀。其中扫描包下只存放AutoConfiguration类和Scanner类。本实施例在预先编写微服务的代码时,需要在微服务的扫描包中标注自动配置类,并添加扫描注解,其中扫描注解用于配置Spring模块扫描非主微服务的自动配置类。示例性的,在编写微服务的代码时,可以在用户微服务的scanner包添加AuthAutoConfiguration类,并添加@ComponentScan注解,其中@ComponentScan注解即为扫描注解,如果该注解的类被Spring模块扫描到,则该注解配置的路径也会被Spring模块扫描,用于配置Spring模块额外扫描非main程序目录的类,具体代码为:In one embodiment, each microservice includes a deployment package. The deployment package includes a scanning package (scanner package) and a business package. For example, the package name planning of the microservice is as shown in Figure 4. In Figure 4, vip.isass. auth is the package name prefix of the user microservice, and vip.isass.order is the package name prefix of the order microservice. Only the AutoConfiguration class and Scanner class are stored under the scanning package. In this embodiment, when pre-writing the code of the microservice, it is necessary to mark the automatic configuration class in the scanning package of the microservice and add scanning annotations, where the scanning annotation is used to configure the Spring module to scan the automatic configuration classes of non-main microservices. For example, when writing microservice code, you can add the AuthAutoConfiguration class to the scanner package of the user microservice and add the @ComponentScan annotation, where the @ComponentScan annotation is the scanning annotation. If the annotated class is scanned by the Spring module, Then the path configured by this annotation will also be scanned by the Spring module, which is used to configure the Spring module to additionally scan classes in non-main program directories. The specific code is:
另外,用户还需要在微服务的scanner包中标注AuthServiceScanner类,并添加条件注解,其中条件注解用于配置Spring模块在指定条件下执行与微服务相对应的功能组件的创建与加载,例如条件注解可以为@Configuration注解和@ConditionalOnProperty注解,用于在特定条件下进行功能组件(即bean)的装载和创建,它的作用是根据指定的属性值来判断是否满足条件,如果满足条件,则创建与对应的Bean。具体代码如下:In addition, users also need to mark the AuthServiceScanner class in the scanner package of the microservice and add conditional annotations. The conditional annotations are used to configure the Spring module to create and load functional components corresponding to the microservice under specified conditions, such as conditional annotations. @Configuration annotations and @ConditionalOnProperty annotations can be used to load and create functional components (i.e. beans) under specific conditions. Its function is to determine whether the conditions are met based on the specified property value. If the conditions are met, create the same The corresponding Bean. The specific code is as follows:
其中代码的第9行使用了Springboot的ConditionalOnProperty注解功能,实现了如果配置项start.microservice.enabled=false,则让Spring模块加载此类,否则不加载。如果加载了此类,又因此类有第10行@ComponentScan(value={"vip.isass.auth"}),则Spring模块会扫描vip.isass.auth的部署包。Line 9 of the code uses Springboot's ConditionalOnProperty annotation function to realize that if the configuration item start.microservice.enabled=false, the Spring module will load this class, otherwise it will not load. If this class is loaded, and because this class has line 10 @ComponentScan(value="{"vip.isass.auth"}), the Spring module will scan the deployment package of vip.isass.auth.
在上述实施例的基础上,在预先编码微服务时,还包括:Based on the above embodiments, when pre-coding microservices, it also includes:
在微服务的部署包中添加配置文件,并在配置文件标注自动配置类。Add a configuration file to the microservice deployment package and mark the automatic configuration class in the configuration file.
一个实施例中,在预先编写微服务的代码时,还需要在微服务的部署包中添加配置文件,并在配置文件标注自动配置类。示例性的,本实施例中配置文件为META-INF/Spring.factories文件,用户可以预先在用户微服务添加META-INF/Spring.factories文件并写上AuthAutoConfiguration类,具体代码如下:In one embodiment, when pre-writing the code of the microservice, it is also necessary to add a configuration file to the deployment package of the microservice and mark the automatic configuration class in the configuration file. For example, in this embodiment, the configuration file is the META-INF/Spring.factories file. The user can add the META-INF/Spring.factories file to the user microservice in advance and write the AuthAutoConfiguration class. The specific code is as follows:
#Auto Configure#AutoConfigure
org.Springframework.boot.autoconfigure.EnableAutoConfiguration=\org.Springframework.boot.autoconfigure.EnableAutoConfiguration=\
vip.isass.auth.scanner.AuthAutoConfiguration。vip.isass.auth.scanner.AuthAutoConfiguration.
之后,用户可正常编写用户微服务的main入口类和业务代码,示例性的,具体代码如下所示:After that, the user can normally write the main entry class and business code of the user microservice. For example, the specific code is as follows:
在上述实施例的基础上,步骤103中通过Spring模块扫描主微服务的代码以及主微服务的配置文件,包括:Based on the above embodiment, in step 103, the code of the main microservice and the configuration file of the main microservice are scanned through the Spring module, including:
步骤1031、通过Spring模块扫描主微服务入口类所在的包路径及子路径,得到主微服务的部署包;Step 1031: Scan the package path and sub-path where the main microservice entry class is located through the Spring module to obtain the deployment package of the main microservice;
步骤1032、读取部署包内所有的配置文件。Step 1032: Read all configuration files in the deployment package.
本实施例中,在启动主微服务后,Spring模块扫描主微服务入口类所在的包路径及子路径,得到主微服务的部署包,并读取部署包内所有的配置文件。其中需要进一步说明的是,本实施例中需要预先对Spring模块进行配置,具体配置过程如下:In this embodiment, after starting the main microservice, the Spring module scans the package path and subpath where the main microservice entry class is located, obtains the deployment package of the main microservice, and reads all configuration files in the deployment package. What needs further explanation is that in this embodiment, the Spring module needs to be configured in advance. The specific configuration process is as follows:
(1)配置Springboot会在主微服务启动时,默认扫描当前主微服务目录下部署包的所有类,扫描到的符合规定的类会被Spring模块实例化成bean对象并管理。可理解,不同微服务,其部署包的包名前缀的命名肯定不同,例如用户微服务的包名前缀为vip.isass.user,订单服务的包名前缀为vip.isass.order。用户微服务的main方法一般放在用户微服务包下的一级目录,例如类名为vip.isass.user.UserApp,而订单微服务的主程序一般放在订单微服务包下的一级目录例如类名为vip.isass.user.OrderApp。若只根据本环节的设置,将用户微服务作为依赖包,被订单微服务依赖,则当订单微服务作为单体应用启动时,Springboot只会扫描vip.isass.user下的所有类,由于vip.isass.user不在Springboot的扫描范围,因此订单服务单体应用便没有用户微服务的功能,因此本实施例中还需要对Spring模块进行后续(2)、(3)、(4)环节的设置。(1) Configure Springboot to scan all classes of the deployment package in the current main microservice directory by default when the main microservice starts. The scanned classes that meet the requirements will be instantiated into bean objects and managed by the Spring module. It is understandable that the package name prefix of the deployment package of different microservices must be named differently. For example, the package name prefix of the user microservice is vip.isass.user, and the package name prefix of the order service is vip.isass.order. The main method of the user microservice is generally placed in the first-level directory under the user microservice package, for example, the class name is vip.isass.user.UserApp, while the main program of the order microservice is generally placed in the first-level directory under the order microservice package. For example, the class name is vip.isass.user.OrderApp. If only based on the settings of this link, the user microservice is used as a dependency package and is relied on by the order microservice, then when the order microservice is started as a single application, Springboot will only scan all classes under vip.isass.user. Since vip .isass.user is not in the scanning scope of Springboot, so the order service monolithic application does not have the function of user microservice. Therefore, in this embodiment, the Spring module also needs to be set in the subsequent steps (2), (3), and (4). .
(2)Spring模块提供@ComponentScan注解,如果该注解的类被扫描到,则该注解配置的路径也会被Spring模块扫描。(2) The Spring module provides the @ComponentScan annotation. If the annotated class is scanned, the path configured by the annotation will also be scanned by the Spring module.
(3)Spring模块提供@Configuration和@ConditionalOnProperty注解,用于在特定条件下进行Bean的装载和创建。(3) The Spring module provides @Configuration and @ConditionalOnProperty annotations for loading and creating beans under specific conditions.
(4)Spring模块提供Spring Factories,其中Spring Factories是一种类似于Java SPI的机制,用于在META-INF/Spring.factories文件中配置接口的实现类名称,之后在程序中读取相对应的配置文件并实例化。Spring Factories机制提供了一种解耦容器注入的方式,帮助外部包(不在主微服务的类的目录及所有子目录下的包)注册Bean到Springboot项目容器中。(4) The Spring module provides Spring Factories, where Spring Factories is a mechanism similar to Java SPI, used to configure the implementation class name of the interface in the META-INF/Spring.factories file, and then read the corresponding in the program Configuration file and instantiation. The Spring Factories mechanism provides a way to decouple container injection and help external packages (packages not in the main microservice's class directory and all subdirectories) register beans into the Springboot project container.
在预先对Spring模块进行配置后,后续主微服务启动后,Spring模块即可在主微服务启动后,自动扫描主微服务入口类所在的包路径以及子路径,之后读取部署包内所有META-INF/Spring.factories文件(包括主微服务和副微服务),之后读取META-INF/Spring.factories文件内所有配置的AutoConfiguration类,并扫描AutoConfiguration类所在的包路径以及子路径(xxx.scanner),以获取该路径下的xxxServicescanner类。After configuring the Spring module in advance, and subsequently starting the main microservice, the Spring module can automatically scan the package path and subpath where the main microservice entry class is located, and then read all META in the deployment package. -INF/Spring.factories file (including main microservice and secondary microservice), then read all configured AutoConfiguration classes in the META-INF/Spring.factories file, and scan the package path and subpath where the AutoConfiguration class is located (xxx. scanner) to obtain the xxxServicescanner class under this path.
步骤104、当部署方式为单体应用部署时,通过Spring模块扫描每个副微服务的自动配置类所在路径的所有代码以获取每个副微服务的代码,根据每个副微服务的代码由Spring模块加载与每个副微服务相对应的功能组件,以及根据主微服务的代码由Spring模块加载与主微服务相对应的功能组件。Step 104. When the deployment mode is single application deployment, use the Spring module to scan all codes in the path where the automatic configuration class of each secondary microservice is located to obtain the code of each secondary microservice. According to the code of each secondary microservice, The Spring module loads the functional components corresponding to each secondary microservice, and the Spring module loads the functional components corresponding to the main microservice according to the code of the main microservice.
在获取到自动配置类后,需要进一步判断主微服务的部署方式。如果主微服务的部署方式为单体应用部署时,则Spring模块扫描每个副微服务的自动配置类所在路径,以获取每个副微服务的代码。之后,根据每个副微服务的代码由Spring模块加载与每个副微服务相对应的功能组件。例如,Spring模块可将代码中所有的AutoConfiguration类加载为bean。同理,Spring模块也需要根据主微服务的代码加载与主微服务相对应的bean。After obtaining the automatic configuration class, you need to further determine the deployment method of the main microservice. If the main microservice is deployed as a single application, the Spring module scans the path of the automatic configuration class of each secondary microservice to obtain the code of each secondary microservice. Afterwards, the functional components corresponding to each secondary microservice are loaded by the Spring module according to the code of each secondary microservice. For example, the Spring module can load all AutoConfiguration classes in the code as beans. In the same way, the Spring module also needs to load the beans corresponding to the main microservice according to the code of the main microservice.
另外,需要进一步说明的是,Spring.factories记录的是AutoConfiguration类,因为AutoConfiguration类里面有@ComponentScan注解,因此在扫描过程中扫描的是AutoConfiguration类的目录。而该目录下还有一个Scanner类,Scanner类的功能是判断微服务部署方式,例如可以判断条件注解中start.microservice.enabled=false是否成立,来确定部署方式。副微服务的业务功能代码储存在AutoConfiguration类目录的平级目录,因此AutoConfiguration类里面有@ComponentScan注解,Spring也只能扫描AutoConfiguration目录下的代码,而副微服务的代码不会被Spring直接扫到。Scanner类也有@ComponentScan注解,但是该注解指定了Spring的扫描范围扩大到了上级目录,因此Scanner类判断到是主微服务为单体部署时,通过@ComponentScan注解可以扫描到上一级目录,即可以扫描到整个副微服务的代码。而AutoConfiguration类里的ComponentScan注解没有指定扫描范围,所以默认扫描同级及下级的目录。In addition, it needs further explanation that Spring.factories records the AutoConfiguration class. Because the AutoConfiguration class has the @ComponentScan annotation, the directory of the AutoConfiguration class is scanned during the scanning process. There is also a Scanner class in this directory. The function of the Scanner class is to determine the microservice deployment method. For example, it can determine whether start.microservice.enabled=false in the conditional annotation is true to determine the deployment method. The business function code of the secondary microservice is stored in the directory at the same level as the AutoConfiguration class directory. Therefore, there is an @ComponentScan annotation in the AutoConfiguration class. Spring can only scan the code in the AutoConfiguration directory, and the code of the secondary microservice will not be directly scanned by Spring. . The Scanner class also has the @ComponentScan annotation, but this annotation specifies that Spring's scanning range is expanded to the upper-level directory. Therefore, when the Scanner class determines that the main microservice is deployed as a single entity, it can scan to the upper-level directory through the @ComponentScan annotation, that is, it can Scan the code of the entire secondary microservice. The ComponentScan annotation in the AutoConfiguration class does not specify a scanning range, so it scans the same level and lower-level directories by default.
步骤105、当部署方式为微服务部署时,根据主微服务的代码由Spring模块加载与主微服务相对应的功能组件。Step 105: When the deployment mode is microservice deployment, the Spring module loads the functional components corresponding to the main microservice according to the code of the main microservice.
而当部署方式为微服务部署时,则不需要获取副微服务的代码,根据主微服务的代码由Spring模块加载与主微服务相对应的功能组件,即根据主微服务的扫描包由Spring模块加载与自动配置类相对应的bean。When the deployment mode is microservice deployment, there is no need to obtain the code of the secondary microservice. The Spring module loads the functional components corresponding to the main microservice according to the code of the main microservice, that is, based on the scanning package of the main microservice, Spring The module loads the beans corresponding to the autoconfiguration classes.
步骤106、调用MyBatis模块从已加载的功能组件中获取预设的调用接口实现类,由MyBatis模块通过调用接口实现类获取与功能组件已加载的目标微服务相对应的初始化脚本并缓存,以当目标微服务被外部调用时,由Spring模块执行相对应的功能组件,并由MyBatis模块通过执行初始化脚本从数据库中获取或更新与目标微服务相对应的业务数据,数据库用于储存微服务的业务数据。Step 106: Call the MyBatis module to obtain the preset calling interface implementation class from the loaded functional component. The MyBatis module obtains the initialization script corresponding to the target microservice that the functional component has loaded by calling the interface implementation class and caches it for current use. When the target microservice is called externally, the Spring module executes the corresponding functional component, and the MyBatis module obtains or updates the business data corresponding to the target microservice from the database by executing the initialization script. The database is used to store the business of the microservice. data.
在加载了微服务相对应的bean后,需要进一步调用MyBatis模块,其中MyBatis模块是一款持久层框架,支持定制化Sql、存储过程以及高级映射,避免了几乎所有的JDBC(Java DataBase Connectivity,Java语言连接数据库)代码和手动设置参数以及获取结果集。本实施例在编写微服务的代码时,同样需要对MyBatis模块进行适配性设置,在使用MyBatis模块时,需要在java类中需要定义Mapper接口和一系列执行Sql语句的方法,并且Mapper接口可以绑定xml文件,xml文件用于编写具体的Sql语句,达到java代码与Sql语句分离,提高系统可维护性和可读性。MapperLocation是MyBatis模块的一个配置,用于告知MyBatis模块需要从哪个路径读取xml文件,路径配置支持通配符,例如“MyBatis.Mapper-locations=classpath:Mapper/*.xml”,代码Mapper目录下所有以xml结尾的文件。该配置项一般写在Springboot的配置文件中,当多个微服务打包成单体程序进行部署时,该配置项会被覆盖,从而无法让MyBatis模块正确加载所有微服务的xml文件。因此本实施例中需要在MyBatis模块中新增调用接口以获取各个微服务相对应的xml文件。具体的,本实施例可以在MyBatis模块中新增IMapperLocationProvider接口,以获取各个微服务的xml文件。示例性的,具体代码如下:After loading the beans corresponding to the microservice, you need to further call the MyBatis module. The MyBatis module is a persistence layer framework that supports customized Sql, stored procedures and advanced mapping, avoiding almost all JDBC (Java DataBase Connectivity, Java Language connection database) code and manually set parameters and obtain result sets. In this embodiment, when writing the code of the microservice, it is also necessary to set the adaptability of the MyBatis module. When using the MyBatis module, it is necessary to define the Mapper interface and a series of methods for executing Sql statements in the java class, and the Mapper interface can Bind xml files, which are used to write specific Sql statements to separate Java code from Sql statements and improve system maintainability and readability. MapperLocation is a configuration of the MyBatis module. It is used to tell the MyBatis module which path it needs to read the xml file from. The path configuration supports wildcards, such as "MyBatis.Mapper-locations=classpath:Mapper/*.xml". All code in the Mapper directory begin with File ending with xml. This configuration item is generally written in the Springboot configuration file. When multiple microservices are packaged into a single program for deployment, this configuration item will be overwritten, making it impossible for the MyBatis module to correctly load the xml files of all microservices. Therefore, in this embodiment, it is necessary to add a new calling interface in the MyBatis module to obtain the xml file corresponding to each microservice. Specifically, in this embodiment, the IMapperLocationProvider interface can be added to the MyBatis module to obtain the xml file of each microservice. For example, the specific code is as follows:
public interface IMapperLocationProvider{public interface IMapperLocationProvider{
List<String>getMapperLocations();List<String>getMapperLocations();
}}
其中,getMapperLocations()方法是返回微服务的xml文件的路径。Among them, the getMapperLocations() method returns the path of the xml file of the microservice.
相对应的,需要在编写微服务的过程中,使得所有微服务都能各自实现IMapperLocationProvider。示例性的,对于用户微服务,所需要添加的代码为:Correspondingly, it is necessary to ensure that all microservices can implement IMapperLocationProvider in the process of writing microservices. For example, for user microservices, the code that needs to be added is:
该代码指定了用户微服务的MyBatis模块的xml文件位于java类路径下/vip/isass/auth/db下,任意子目录下的Mapper目录下的以Mapper.xml结尾的文件。而Classpath代表当前java执行程序的目录。This code specifies the xml file of the MyBatis module of the user microservice located under /vip/isass/auth/db under the java classpath, and the file ending with Mapper.xml in the Mapper directory in any subdirectory. Classpath represents the directory of the current java execution program.
另外,本实施例中还需要在MyBatis模块中创建SqlSessionConfig类,其中SqlSessionFactory是MyBatis框架中十分重要的对象,其主要作用是创建SqlSession。而SqlSession是MyBatis模块中的关键对象,用于执行持久化操作。SqlSession底层封装了JDBC连接,可以用SqlSession实例来直接执行被映射的Sql语句,具体代码如下:In addition, in this embodiment, the SqlSessionConfig class needs to be created in the MyBatis module. SqlSessionFactory is a very important object in the MyBatis framework, and its main function is to create SqlSession. SqlSession is the key object in the MyBatis module and is used to perform persistence operations. The bottom layer of SqlSession encapsulates the JDBC connection. You can use the SqlSession instance to directly execute the mapped Sql statement. The specific code is as follows:
其中,代码的第6行定义了IMapperLocationProvider接口的成员变量,该对象会被Spring模块自动赋值,值为所有实现了IMapperLocationProvider接口的对象。Among them, line 6 of the code defines the member variables of the IMapperLocationProvider interface. This object will be automatically assigned by the Spring module, and the value is all objects that implement the IMapperLocationProvider interface.
第9行定义的方法用于将把xml路径设置给SqlSessionFactory对象。The method defined in line 9 is used to set the xml path to the SqlSessionFactory object.
第16行定义的方法用于把IMapperLocationProvider的对象提供的xml路径,转换成SqlSessionFactory需要的Resource对象,以使得MyBatis模块获取相对应的xml文件中的Sql语句。之后MyBatis模块会缓存Sql语句,以在后续微服务被调用时,当微服务的业务逻辑需要查询数据库时,则MyBatis模块可以使用缓存的Sql语句查询数据库。The method defined in line 16 is used to convert the xml path provided by the IMapperLocationProvider object into the Resource object required by SqlSessionFactory, so that the MyBatis module can obtain the Sql statement in the corresponding xml file. The MyBatis module will then cache the Sql statement so that when the microservice is called subsequently, when the business logic of the microservice needs to query the database, the MyBatis module can use the cached Sql statement to query the database.
在上述实施例的基础上,还包括:On the basis of the above embodiments, it also includes:
预先对数据库进行配置,在数据库用于储存每个微服务的初始化脚本的储存路径中添加相对应的微服务的名称,以及在数据库的历史记录表所记录的数据中添加相对应的微服务的名称,历史记录表用于记录微服务相对应的初始化脚本的执行记录。Configure the database in advance, add the name of the corresponding microservice to the storage path used by the database to store the initialization script of each microservice, and add the name of the corresponding microservice to the data recorded in the history table of the database. Name, the history table is used to record the execution records of the initialization script corresponding to the microservice.
本实施例中的数据库用于储存与每个微服务的业务数据。本实施例中,由于不同微服务有不同的数据表,再将所有的微服务打包到一个单体应用后,所有微服务的初始化脚本都储存在一起了。其中需要说明的是,初始化脚本即为初始化Sql脚本,是数据库的初始化脚本,用于在数据库中执行例如建表,增删改字段等操作,与MyBatis模块的Sql语句(业务逻辑所用到的Sql语句,别人调用服务接口时执行的Sql语句)不一样,微服务通过执行初始化脚本即可对数据库中存储的业务数据进行更新和修改。因此需要对微服务的初始化Sql脚本进行区分,因此本实施例中需要在数据库用于储存每个微服务的初始化脚本的储存路径中添加相对应的微服务的名称,以及在数据库的历史记录表所记录的数据中添加相对应的微服务的名称,历史记录表用于记录微服务相对应的初始化脚本的执行记录,以得到达到各个微服务可以独立配置初始化Sql脚本,避免互相覆盖的目的。The database in this embodiment is used to store business data related to each microservice. In this embodiment, since different microservices have different data tables, after all microservices are packaged into a single application, the initialization scripts of all microservices are stored together. It should be noted that the initialization script is the initialization Sql script, which is the initialization script of the database. It is used to perform operations such as creating tables, adding, deleting and modifying fields in the database, and is consistent with the Sql statements of the MyBatis module (Sql statements used in business logic). , unlike the Sql statements executed when others call the service interface), microservices can update and modify the business data stored in the database by executing the initialization script. Therefore, it is necessary to distinguish the initialization Sql scripts of microservices. Therefore, in this embodiment, it is necessary to add the name of the corresponding microservice in the storage path used by the database to store the initialization script of each microservice, and in the history table of the database. The name of the corresponding microservice is added to the recorded data, and the history table is used to record the execution record of the initialization script corresponding to the microservice, so that each microservice can independently configure the initialization Sql script to avoid overwriting each other.
另外本实施例中,数据库可以采用Flyway进行管理,其中Flyway是一款开源的数据库版本管理工具,它更倾向于规约优于配置的方式。Flyway可以独立于应用实现管理并跟踪数据库变更,支持数据库版本自动升级,并且有一套默认的规约,不需要复杂的配置。本实施例中,Flyway的工作流程为:In addition, in this embodiment, the database can be managed using Flyway, which is an open source database version management tool that prefers specifications over configuration. Flyway can manage and track database changes independently of the application, supports automatic database version upgrades, and has a set of default protocols without complex configuration. In this embodiment, Flyway’s workflow is:
(1)初次使用时,flyway会创建一个flyway_schema_history历史记录表,用于记录初始化Sql脚本Sql脚本的执行记录。(1) When used for the first time, flyway will create a flyway_schema_history history table to record the execution records of the initialization Sql script Sql script.
(2)启动flyway后,flyway会自动扫描项目指定路径下的所有初始化Sql脚本Sql脚本,并与flyway_schema_history历史记录表进行比对。如果数据库记录执行过的初始化Sql脚本Sql脚本,与所执行的项目中的初始化Sql脚本Sql脚本不一致,flyway会报错并停止项目执行。(2) After starting flyway, flyway will automatically scan all initialization Sql scripts in the path specified by the project and compare them with the flyway_schema_history history table. If the initialization Sql script Sql script executed in the database record is inconsistent with the initialization Sql script Sql script in the executed project, flyway will report an error and stop the project execution.
(3)如果校验通过,则根据表中的初始化Sql脚本记录最大版本号,忽略所有版本号小于该版本号的初始化Sql脚本。再按照版本号从小到大,逐个执行其余初始化Sql脚本。(3) If the verification passes, record the maximum version number according to the initialization Sql script in the table, and ignore all initialization Sql scripts with version numbers smaller than this version number. Then execute the remaining initialization Sql scripts one by one according to the version number from small to large.
本实施例中还需要在flywawy中重写FlywayConfiguration类,其中FlywayConfiguration类是flyway官方的配置类,包括“初始化Sql脚本编码”、“初始化Sql脚本路径”、“flyway历史记录表名称”等,重写代码如下:In this embodiment, it is also necessary to rewrite the FlywayConfiguration class in flywawy, where the FlywayConfiguration class is the official configuration class of flyway, including "initializing Sql script encoding", "initializing Sql script path", "flyway history table name", etc., rewriting code show as below:
其中新增第3行代码,添加一个getServiceName(),作用是获取当前微服务的名称。Add the third line of code and add a getServiceName(), which is used to obtain the name of the current microservice.
新增第7至12行代码,用于修改初始化Sql脚本的路径,通过调用了第3行代码新增的getServiceName()方法,使初始化Sql脚本的路径拼接上微服务名称,达到各个微服务可以独立配置初始化Sql脚本目录,避免互相覆盖的目的。Added lines 7 to 12 of code to modify the path of the initialization Sql script. By calling the getServiceName() method added in the third line of code, the path of the initialization Sql script is spliced with the name of the microservice, so that each microservice can Configure the initialization Sql script directory independently to avoid overwriting each other.
新增第14行代码,用于修改默认的flyway历史记录表的名称,使其拼接上微服务名称,达到各个微服务可以独立配置历史记录表的目的。Add the 14th line of code to modify the name of the default flyway history table so that it is spliced with the name of the microservice, so that each microservice can independently configure the history table.
另外,由于在重写flywawy的代码是新增了getServiceName(),因此本实施例中也需要使得所有微服务都各自实现FlywayConfiguration类。例如在编写用户微服务的过程中,新增的代码如下:In addition, since getServiceName() is added when rewriting flywawy's code, in this embodiment it is also necessary for all microservices to implement the FlywayConfiguration class. For example, in the process of writing user microservices, the new code is as follows:
其中,从第4行代码开始,复写getServiceName()方法,返回用户微服务的名称为user。Among them, starting from the 4th line of code, the getServiceName() method is overridden and the name of the user microservice returned is user.
从第9行代码开始,提供一个名为userFlyway的方法,该方法使用@Bean注解标记,即该方法会被Spring管理调用,生成一个bean,该bean最终会被flyway本身的启动流程功能中获取到,并初始化用户微服务的初始化Sql脚本。Starting from the 9th line of code, a method named userFlyway is provided. This method is marked with @Bean annotation, that is, this method will be called by Spring management to generate a bean, which will eventually be obtained by flyway's own startup process function. , and initialize the initialization Sql script of the user microservice.
另外还需要在资源目录db/migration/user下添加用户微服务的初始化Sql脚本,该初始化Sql脚本会被flyway自动执行。In addition, you need to add the initialization Sql script of the user microservice under the resource directory db/migration/user. The initialization Sql script will be automatically executed by flyway.
具体的,本实施例中对于加载了bean的目标微服务,需要进一步调用MyBatis模块从已加载的bean中获取预设的调用接口实现类,即获取IMapperLocationProvider实现类,之后调用IMapperLocationProvider实现类获取Mapper路径的方法,以获取与bean已加载的目标微服务相对应的初始化脚本并进行缓存。当目标微服务被外部调用时,由Spring模块执行相对应的bean,同时由MyBatis模块通过执行初始化脚本从数据库中获取或更新与目标微服务相对应的业务数据。Specifically, in this embodiment, for the target microservice with loaded beans, it is necessary to further call the MyBatis module to obtain the preset calling interface implementation class from the loaded bean, that is, obtain the IMapperLocationProvider implementation class, and then call the IMapperLocationProvider implementation class to obtain the Mapper path. Method to obtain and cache the initialization script corresponding to the target microservice where the bean has been loaded. When the target microservice is called externally, the Spring module executes the corresponding bean, and the MyBatis module obtains or updates the business data corresponding to the target microservice from the database by executing the initialization script.
在上述实施例的基础上,步骤106中调用MyBatis模块从已加载的功能组件中获取预设的调用接口实现类,由所述MyBatis模块通过所述调用接口实现类获取与功能组件已加载的目标微服务相对应的初始化脚本并缓存,包括:On the basis of the above embodiment, in step 106, the MyBatis module is called to obtain the preset calling interface implementation class from the loaded functional component, and the MyBatis module implements the class acquisition and loaded functional component through the calling interface. The initialization script corresponding to the microservice is cached, including:
步骤1061、初始化MyBatis模块中的SqlSessionFactory。Step 1061. Initialize the SqlSessionFactory in the MyBatis module.
本实施例中,首先需要初始化MyBatis模块中的SqlSessionFactory。In this embodiment, you first need to initialize the SqlSessionFactory in the MyBatis module.
步骤1062、调用MyBatis模块从已加载的功能组件中获取预设的调用接口实现类,由所述MyBatis模块通过所述调用接口实现类获取与功能组件已加载的目标微服务相对应的xml文件路径,并将所述xml文件路径转化为所述SqlSessionFactory所需的目标对象,以使所述SqlSessionFactory根据所述目标对象获取与所述目标微服务相对应的初始化脚本并缓存。Step 1062: Call the MyBatis module to obtain the preset calling interface implementation class from the loaded functional component. The MyBatis module obtains the xml file path corresponding to the target microservice loaded by the functional component through the calling interface implementation class. and convert the xml file path into the target object required by the SqlSessionFactory, so that the SqlSessionFactory obtains and caches the initialization script corresponding to the target microservice according to the target object.
在初始化SqlSessionFactory后,进一步调用MyBatis模块从已加载的bean中获取IMapperLocationProvider实现类,之后调用IMapperLocationProvider实现类获取Mapper路径的方法,获取与目标微服务相对应的xml文件路径,并转化为SqlSessionFactory需要的Resource对象,使得SqlSessionFactory根据Resource对象获取与目标微服务相对应的初始化脚本并缓存。一个实施例中,本发明实施例的具体过程如图5所示。After initializing SqlSessionFactory, further call the MyBatis module to obtain the IMapperLocationProvider implementation class from the loaded bean, and then call the IMapperLocationProvider implementation class to obtain the Mapper path method, obtain the xml file path corresponding to the target microservice, and convert it into the Resource required by SqlSessionFactory Object, so that SqlSessionFactory obtains the initialization script corresponding to the target microservice based on the Resource object and caches it. In one embodiment, the specific process of the embodiment of the present invention is shown in Figure 5.
上述,本发明实施例提供了一种微服务部署方法,本发明实施例在微服务的部署过程中,用户可以在单体应用部署或者微服务部署中根据需要选择部署方式,后续本发明实施例即可根据用户所选择的部署方式部署相对应的微服务。本发明实施例能够根据不同目标客户的资金或运维资源投入情况,对微服务进行单体应用或微服务方式部署,在开发业务系统的过程中无需在不同的架构上进行开发,提高了业务系统的开发效率以及降低了维护工作量,解决了现有技术中的业务系统存在着开发效率低下以及维护工作量大的技术问题。As mentioned above, embodiments of the present invention provide a microservice deployment method. During the deployment process of microservices, users can select a deployment method as needed in single application deployment or microservice deployment. Subsequent embodiments of the present invention The corresponding microservices can be deployed according to the deployment method selected by the user. The embodiments of the present invention can deploy microservices in a single application or in a microservice manner according to the investment of funds or operation and maintenance resources of different target customers. In the process of developing the business system, there is no need to develop on different architectures, which improves business efficiency. The development efficiency of the system and the reduction of maintenance workload solve the technical problems of low development efficiency and heavy maintenance workload of business systems in the existing technology.
如图6所示,图6为本发明实施例提供的一种微服务部署装置的结构示意图,本发明实施例提供的微服务部署装置包括:As shown in Figure 6, Figure 6 is a schematic structural diagram of a microservice deployment device provided by an embodiment of the present invention. The microservice deployment device provided by an embodiment of the present invention includes:
主微服务确定模块201,用于在所有微服务中确定出主微服务,将其他微服务确定为副微服务,并将副微服务作为主微服务的依赖项;The main microservice determination module 201 is used to determine the main microservice among all microservices, determine other microservices as secondary microservices, and use the secondary microservices as dependencies of the main microservice;
部署方式设置模块202,用于确定微服务的部署方式;The deployment mode setting module 202 is used to determine the deployment mode of the microservice;
服务扫描模块203,用于在启动主微服务时,通过Spring模块扫描主微服务的代码以及所有微服务的配置文件,配置文件中设置有每个微服务的自动配置类;The service scanning module 203 is used to scan the code of the main microservice and the configuration files of all microservices through the Spring module when starting the main microservice. The automatic configuration class of each microservice is set in the configuration file;
应用部署模块204,用于当部署方式为单体应用部署时,通过Spring模块扫描每个副微服务的自动配置类所在路径的所有代码以获取每个副微服务的代码,根据每个副微服务的代码由Spring模块加载与每个副微服务相对应的功能组件,以及根据主微服务的代码由Spring模块加载与主微服务相对应的功能组件;The application deployment module 204 is used to scan all codes in the path of the automatic configuration class of each secondary microservice through the Spring module to obtain the code of each secondary microservice when the deployment mode is single application deployment. The code of the service is loaded by the Spring module and the functional components corresponding to each secondary microservice, and the functional components corresponding to the main microservice are loaded by the Spring module according to the code of the main microservice;
微服务部署模块205,用于当部署方式为微服务部署时,根据主微服务的代码由Spring模块加载与主微服务相对应的功能组件;The microservice deployment module 205 is used to load functional components corresponding to the main microservice from the Spring module according to the code of the main microservice when the deployment mode is microservice deployment;
微服务执行模块206,用于调用MyBatis模块从已加载的功能组件中获取预设的调用接口实现类,由MyBatis模块通过调用接口实现类获取与功能组件已加载的目标微服务相对应的初始化脚本并缓存,以当目标微服务被外部调用时,由Spring模块执行相对应的功能组件,并由MyBatis模块通过执行初始化脚本从数据库中获取或更新与目标微服务相对应的业务数据,数据库用于储存微服务的业务数据。The microservice execution module 206 is used to call the MyBatis module to obtain the preset calling interface implementation class from the loaded functional component. The MyBatis module obtains the initialization script corresponding to the target microservice that the functional component has loaded by calling the interface implementation class. And cached, so that when the target microservice is called externally, the Spring module executes the corresponding functional component, and the MyBatis module obtains or updates the business data corresponding to the target microservice from the database by executing the initialization script. The database is used Store business data of microservices.
在上述实施例的基础上,还包括数据库配置模块,用于预先对数据库进行配置,在数据库用于储存每个微服务的初始化脚本的储存路径中添加相对应的微服务的名称,以及在数据库的历史记录表所记录的数据中添加相对应的微服务的名称,历史记录表用于记录微服务相对应的初始化脚本的执行记录。On the basis of the above embodiment, a database configuration module is also included, which is used to configure the database in advance, add the name of the corresponding microservice to the storage path used by the database to store the initialization script of each microservice, and add the name of the corresponding microservice to the database. Add the name of the corresponding microservice to the data recorded in the history table. The history table is used to record the execution record of the initialization script corresponding to the microservice.
在上述实施例的基础上,微服务执行模块206具体用于初始化所述MyBatis模块中的SqlSessionFactory;调用MyBatis模块从已加载的功能组件中获取预设的调用接口实现类,由所述MyBatis模块通过所述调用接口实现类获取与功能组件已加载的目标微服务相对应的xml文件路径,并将所述xml文件路径转化为所述SqlSessionFactory所需的目标对象,以使所述SqlSessionFactory根据所述目标对象获取与所述目标微服务相对应的初始化脚本并缓存。On the basis of the above embodiments, the microservice execution module 206 is specifically used to initialize the SqlSessionFactory in the MyBatis module; call the MyBatis module to obtain the preset calling interface implementation class from the loaded functional components, and the MyBatis module passes The calling interface implementation class obtains the xml file path corresponding to the target microservice loaded by the functional component, and converts the xml file path into the target object required by the SqlSessionFactory, so that the SqlSessionFactory can be configured according to the target The object obtains the initialization script corresponding to the target microservice and caches it.
在上述实施例的基础上,每个微服务包括部署包,部署包包括扫描包以及业务包,微服务部署装置还包括:Based on the above embodiment, each microservice includes a deployment package. The deployment package includes a scanning package and a business package. The microservice deployment device also includes:
微服务编写模块,用于预先编写微服务,在微服务的扫描包中标注自动配置类,并添加扫描注解;在微服务的扫描包中标注ServiceScanner类,并添加条件注解;扫描注解用于配置Spring模块扫描非主微服务的自动配置类,条件注解用于配置Spring模块在指定条件下执行与微服务相对应的功能组件的创建与加载。Microservice writing module, used to pre-write microservices, mark the automatic configuration class in the scanning package of the microservice, and add scanning annotations; mark the ServiceScanner class in the scanning package of the microservice, and add conditional annotations; scanning annotations are used for configuration The Spring module scans the automatic configuration classes of non-main microservices, and conditional annotations are used to configure the Spring module to create and load functional components corresponding to the microservice under specified conditions.
在上述实施例的基础上,微服务编写模块在预先编码微服务时,还用于在微服务的部署包中添加配置文件,并在配置文件标注自动配置类。Based on the above embodiment, when pre-coding the microservice, the microservice writing module is also used to add a configuration file to the deployment package of the microservice and mark the automatic configuration class in the configuration file.
在上述实施例的基础上,服务扫描模块203具体用于通过Spring模块扫描主微服务入口类所在的包路径及子路径,得到主微服务的部署包;读取部署包内所有的配置文件。Based on the above embodiment, the service scanning module 203 is specifically used to scan the package path and sub-path where the main microservice entry class is located through the Spring module to obtain the deployment package of the main microservice; read all configuration files in the deployment package.
在上述实施例的基础上,微服务部署模块205具体用于根据主微服务的扫描包由Spring模块加载与自动配置类相对应的bean。Based on the above embodiment, the microservice deployment module 205 is specifically configured to load beans corresponding to the automatic configuration class from the Spring module according to the scanning package of the main microservice.
本发明实施例提供的微服务部署装置包含在微服务部署设备中,且可用于执行上述实施例中提供的微服务部署方法,具备相应的功能和有益效果。The microservice deployment device provided by the embodiment of the present invention is included in the microservice deployment device and can be used to execute the microservice deployment method provided in the above embodiment, and has corresponding functions and beneficial effects.
值得注意的是,上述微服务部署装置的实施例中,所包括的各个单元和模块只是按照功能逻辑进行划分的,但并不局限于上述的划分,只要能够实现相应的功能即可;另外,各功能单元的具体名称也只是为了便于相互区分,并不用于限制本发明的保护范围。It is worth noting that in the above embodiment of the microservice deployment device, the various units and modules included are only divided according to functional logic, but are not limited to the above division, as long as the corresponding functions can be realized; in addition, The specific names of each functional unit are only for the convenience of distinguishing each other and are not used to limit the scope of the present invention.
本实施例还提供了一种微服务部署设备,如图7所示,图7为本发明实施例提供的一种微服务部署设备的结构示意图,微服务部署设备30包括处理器300以及存储器301;This embodiment also provides a microservice deployment device, as shown in Figure 7. Figure 7 is a schematic structural diagram of a microservice deployment device provided by an embodiment of the present invention. The microservice deployment device 30 includes a processor 300 and a memory 301 ;
存储器301用于存储计算机程序302,并将计算机程序302传输给处理器300;The memory 301 is used to store the computer program 302 and transmit the computer program 302 to the processor 300;
处理器300用于根据计算机程序302中的指令执行上述的一种微服务部署方法实施例中的步骤。The processor 300 is configured to execute the steps in the above-mentioned microservice deployment method embodiment according to the instructions in the computer program 302.
示例性的,计算机程序302可以被分割成一个或多个模块/单元,一个或者多个模块/单元被存储在存储器301中,并由处理器300执行,以完成本申请。一个或多个模块/单元可以是能够完成特定功能的一系列计算机程序指令段,该指令段用于描述计算机程序302在微服务部署设备30中的执行过程。For example, the computer program 302 can be divided into one or more modules/units, and one or more modules/units are stored in the memory 301 and executed by the processor 300 to complete the present application. One or more modules/units may be a series of computer program instruction segments capable of completing specific functions. The instruction segments are used to describe the execution process of the computer program 302 in the microservice deployment device 30 .
微服务部署设备30可以是桌上型计算机、笔记本、掌上电脑及云端服务器等计算设备。微服务部署设备30可包括,但不仅限于,处理器300、存储器301。本领域技术人员可以理解,图7仅仅是微服务部署设备30的示例,并不构成对微服务部署设备30的限定,可以包括比图示更多或更少的部件,或者组合某些部件,或者不同的部件,例如微服务部署设备30还可以包括输入输出设备、网络接入设备、总线等。The microservice deployment device 30 can be a computing device such as a desktop computer, a notebook, a handheld computer, a cloud server, etc. The microservice deployment device 30 may include, but is not limited to, a processor 300 and a memory 301 . Those skilled in the art can understand that Figure 7 is only an example of the microservice deployment device 30 and does not constitute a limitation on the microservice deployment device 30. It may include more or fewer components than shown in the figure, or combine certain components. Or different components, for example, the microservice deployment device 30 may also include input and output devices, network access devices, buses, etc.
所称处理器300可以是中央处理单元(Central Processing Unit,CPU),还可以是其他通用处理器、数字信号处理器(Digital Signal Processor,DSP)、专用集成电路(Application Specific Integrated Circuit,ASIC)、现成可编程门阵列(Field-Programmable Gate Array,FPGA)或者其他可编程逻辑器件、分立门或者晶体管逻辑器件、分立硬件组件等。通用处理器可以是微处理器或者该处理器也可以是任何常规的处理器等。The processor 300 may be a central processing unit (CPU), or other general-purpose processor, a digital signal processor (Digital Signal Processor, DSP), an application specific integrated circuit (Application Specific Integrated Circuit, ASIC), Ready-made field-programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or the processor may be any conventional processor, etc.
存储器301可以是微服务部署设备30的内部存储单元,例如微服务部署设备30的硬盘或内存。存储器301也可以是微服务部署设备30的外部存储设备,例如微服务部署设备30上配备的插接式硬盘,智能存储卡(Smart Media Card,SMC),安全数字(SecureDigital,SD)卡,闪存卡(Flash Card)等。进一步地,存储器301还可以既包括微服务部署设备30的内部存储单元也包括外部存储设备。存储器301用于存储计算机程序以及微服务部署设备30所需的其他程序和数据。存储器301还可以用于暂时地存储已经输出或者将要输出的数据。The memory 301 may be an internal storage unit of the microservice deployment device 30 , such as a hard disk or memory of the microservice deployment device 30 . The memory 301 can also be an external storage device of the microservice deployment device 30, such as a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (SD) card, or a flash memory equipped on the microservice deployment device 30. Flash Card, etc. Further, the memory 301 may also include both an internal storage unit of the microservice deployment device 30 and an external storage device. The memory 301 is used to store computer programs and other programs and data required by the microservice deployment device 30 . The memory 301 can also be used to temporarily store data that has been output or is to be output.
所属领域的技术人员可以清楚地了解到,为描述的方便和简洁,上述描述的系统,装置和单元的具体工作过程,可以参考前述方法实施例中的对应过程,在此不再赘述。Those skilled in the art can clearly understand that for the convenience and simplicity of description, the specific working processes of the systems, devices and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be described again here.
在本申请所提供的几个实施例中,应该理解到,所揭露的系统,装置和方法,可以通过其它的方式实现。例如,以上所描述的装置实施例仅仅是示意性的,例如,单元的划分,仅仅为一种逻辑功能划分,实际实现时可以有另外的划分方式,例如多个单元或组件可以结合或者可以集成到另一个系统,或一些特征可以忽略,或不执行。另一点,所显示或讨论的相互之间的耦合或直接耦合或通信连接可以是通过一些接口,装置或单元的间接耦合或通信连接,可以是电性,机械或其它的形式。In the several embodiments provided in this application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are only illustrative. For example, the division of units is only a logical function division. In actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated. to another system, or some features can be ignored, or not implemented. On the other hand, the coupling or direct coupling or communication connection between each other shown or discussed may be through some interfaces, and the indirect coupling or communication connection of the devices or units may be in electrical, mechanical or other forms.
作为分离部件说明的单元可以是或者也可以不是物理上分开的,作为单元显示的部件可以是或者也可以不是物理单元,即可以位于一个地方,或者也可以分布到多个网络单元上。可以根据实际的需要选择其中的部分或者全部单元来实现本实施例方案的目的。A unit described as a separate component may or may not be physically separate. A component shown as a unit may or may not be a physical unit, that is, it may be located in one place, or it may be distributed to multiple network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of this embodiment.
另外,在本发明各个实施例中的各功能单元可以集成在一个处理单元中,也可以是各个单元单独物理存在,也可以两个或两个以上单元集成在一个单元中。上述集成的单元既可以采用硬件的形式实现,也可以采用软件功能单元的形式实现。In addition, each functional unit in various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically alone, or two or more units can be integrated into one unit. The above integrated units can be implemented in the form of hardware or software functional units.
集成的单元如果以软件功能单元的形式实现并作为独立的产品销售或使用时,可以存储在一个计算机可读取存储介质中。基于这样的理解,本发明的技术方案本质上或者说对现有技术做出贡献的部分或者该技术方案的全部或部分可以以软件产品的形式体现出来,该计算机软件产品存储在一个存储介质中,包括若干指令用以使得一台计算机设备(可以是个人计算机,服务器,或者网络设备等)执行本发明各个实施例方法的全部或部分步骤。而前述的存储介质包括:U盘、移动硬盘、只读存储器(ROM,Read-Only Memory)、随机存取存储器(RAM,Random Access Memory)、磁碟或者光盘等各种可以存储计算机程序的介质。Integrated units may be stored in a computer-readable storage medium if they are implemented in the form of software functional units and sold or used as independent products. Based on this understanding, the technical solution of the present invention is essentially or contributes to the existing technology or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium , including several instructions to cause a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods of various embodiments of the present invention. The aforementioned storage media include: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), magnetic disk or optical disk and other media that can store computer programs. .
本发明实施例还提供一种包含计算机可执行指令的存储介质,计算机可执行指令在由计算机处理器执行时用于执行一种微服务部署方法,该方法包括以下步骤:Embodiments of the present invention also provide a storage medium containing computer-executable instructions. When executed by a computer processor, the computer-executable instructions are used to execute a microservice deployment method. The method includes the following steps:
在所有微服务中确定出主微服务,将其他微服务确定为副微服务,并将副微服务作为主微服务的依赖项;Identify the main microservice among all microservices, identify other microservices as secondary microservices, and use the secondary microservices as dependencies of the main microservice;
确定微服务的部署方式;Determine how microservices will be deployed;
在启动主微服务时,通过Spring模块扫描主微服务的代码以及所有微服务的配置文件,配置文件中设置有每个微服务的自动配置类;When starting the main microservice, scan the code of the main microservice and the configuration files of all microservices through the Spring module. The automatic configuration class of each microservice is set in the configuration file;
当部署方式为单体应用部署时,通过Spring模块扫描每个副微服务的自动配置类所在路径的所有代码以获取每个副微服务的代码,根据每个副微服务的代码由Spring模块加载与每个副微服务相对应的功能组件,以及根据主微服务的代码由Spring模块加载与主微服务相对应的功能组件;When the deployment mode is single application deployment, all codes in the path of the automatic configuration class of each secondary microservice are scanned through the Spring module to obtain the code of each secondary microservice. The code of each secondary microservice is loaded by the Spring module. The functional components corresponding to each secondary microservice, and the functional components corresponding to the main microservice are loaded by the Spring module according to the code of the main microservice;
当部署方式为微服务部署时,根据主微服务的代码由Spring模块加载与主微服务相对应的功能组件;When the deployment mode is microservice deployment, the Spring module loads the functional components corresponding to the main microservice according to the code of the main microservice;
调用MyBatis模块从已加载的功能组件中获取预设的调用接口实现类,由MyBatis模块通过调用接口实现类获取与目标微服务相对应的初始化脚本并缓存,以当功能组件已加载的目标微服务被外部调用时,由Spring模块执行相对应的功能组件,并由MyBatis模块通过执行初始化脚本从数据库中获取或更新与目标微服务相对应的业务数据,数据库用于储存微服务的业务数据。Call the MyBatis module to obtain the preset calling interface implementation class from the loaded functional component. The MyBatis module obtains the initialization script corresponding to the target microservice by calling the interface implementation class and caches it so that when the functional component has loaded the target microservice When called externally, the Spring module executes the corresponding functional components, and the MyBatis module obtains or updates the business data corresponding to the target microservice from the database by executing the initialization script. The database is used to store the business data of the microservice.
注意,上述仅为本发明实施例的较佳实施例及所运用技术原理。本领域技术人员会理解,本发明实施例不限于这里所述的特定实施例,对本领域技术人员来说能够进行各种明显的变化、重新调整和替代而不会脱离本发明实施例的保护范围。因此,虽然通过以上实施例对本发明实施例进行了较为详细的说明,但是本发明实施例不仅仅限于以上实施例,在不脱离本发明实施例构思的情况下,还可以包括更多其他等效实施例,而本发明实施例的范围由所附的权利要求范围决定。Note that the above are only the preferred embodiments and the technical principles used in the embodiments of the present invention. Those skilled in the art will understand that the embodiments of the present invention are not limited to the specific embodiments described here. Various obvious changes, readjustments and substitutions can be made to those skilled in the art without departing from the protection scope of the embodiments of the present invention. . Therefore, although the embodiments of the present invention have been described in detail through the above embodiments, the embodiments of the present invention are not limited to the above embodiments, and may also include more other equivalents without departing from the concept of the embodiments of the present invention. embodiments, and the scope of the embodiments of the present invention is determined by the scope of the appended claims.
Claims (10)
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202311520036.8A CN117573111A (en) | 2023-11-14 | 2023-11-14 | A microservice deployment method, device, equipment and storage medium |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202311520036.8A CN117573111A (en) | 2023-11-14 | 2023-11-14 | A microservice deployment method, device, equipment and storage medium |
Publications (1)
Publication Number | Publication Date |
---|---|
CN117573111A true CN117573111A (en) | 2024-02-20 |
Family
ID=89887363
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202311520036.8A Pending CN117573111A (en) | 2023-11-14 | 2023-11-14 | A microservice deployment method, device, equipment and storage medium |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN117573111A (en) |
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN118283100A (en) * | 2024-06-03 | 2024-07-02 | 湖南盛鼎科技发展有限责任公司 | Micro-service system for realizing elastic deployment and deployment method |
-
2023
- 2023-11-14 CN CN202311520036.8A patent/CN117573111A/en active Pending
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN118283100A (en) * | 2024-06-03 | 2024-07-02 | 湖南盛鼎科技发展有限责任公司 | Micro-service system for realizing elastic deployment and deployment method |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
US10162624B1 (en) | Web shell for dynamically generated cross-platform applications | |
US11816492B2 (en) | Third party integration of plugins and widgets | |
CN108027722B (en) | Dynamically updating applications in compilation and deployment | |
CN112256321B (en) | Static library packaging method, device, computer equipment and storage medium | |
US8549514B2 (en) | Distributing customized software products | |
US7684964B2 (en) | Model and system state synchronization | |
US11042387B2 (en) | Deploying cross-platform applications on mobile devices with native and web components | |
US8863082B2 (en) | Transformational context-aware data source management | |
Goodwill | Mastering Jakarta Struts | |
RU2598600C2 (en) | Projection of own application programming interfaces of operating system into other languages | |
US20090254881A1 (en) | Code generation techniques for administrative tasks | |
US10114861B2 (en) | Expandable ad hoc domain specific query for system management | |
US20090125874A1 (en) | Method and system for creating projects in a rational application developer workspace | |
US11237945B2 (en) | Configuration content integration | |
US20130204887A1 (en) | Method and system for simplifying object mapping for a user interface | |
US8201147B2 (en) | Generic XAD processing model | |
CN115629971A (en) | Application development system and development method | |
US20080141219A1 (en) | Multiple inheritance facility for java script language | |
CN100504773C (en) | Application server public service loose coupling access method | |
US20080163166A1 (en) | Technique for integrating a distributed object system component with a service oriented architecture application | |
CN117573111A (en) | A microservice deployment method, device, equipment and storage medium | |
Soni | Full stack angularJS for java developers: Build a full-featured web application from scratch using angularJS with spring RESTful | |
US7707593B2 (en) | Object models enabling hosting content in a plurality of environments | |
US8615736B2 (en) | Module facility for JAVASCRIPT language | |
US8019781B2 (en) | Host context framework |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination |