In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to carry out the order application of Serverless in Springboot, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Preface
This is an order background application developed by JAVA (that's right! It is the order backend system that makes countless college students miserable), combined with the serverless idea of Serverless, try to deploy Springboot through the combination of cloud function + API gateway + cloud database.
Author: Freeeeeedom
The application provides complete user login verification, interface data verification, order flow (CRUD) and other powerful features, and can also simulate the API gateway to call cloud functions during local development and debugging (local Java development cloud deployment is not a problem), but also compatible with cloud message queue CMQ calls, so that subsequent development to introduce cloud middleware.
At the same time, this deployment method also allows other Springboot to be quickly converted to cloud function deployment.
In response to the environmental protection plan of the country's 14th five-year Plan, we specially studied the legendary Serverless scheme (provincial server), so we made this attempt.
Language and framework
JAVA is the best in the world, of course, c/c++/c#/node/python/go/php/vb is also good.
What else can you choose from the monolithic application of JAVA? It can only be Springboot
Deployment preparation
Sign up for a Tencent Cloud account
Activate the following product permissions (Cloud function, API Gateway, object Storage)
If financial resources permit, you can also buy database services (because I bought these two for a long time when I was young and frivolous at a discount).
Mysql database
Redis database
Deployment plan
As far as the order application is concerned, the interface of restful must be provided, so the mode of cloud function + API gateway is adopted to provide the interface in Unified VPC, so the following solution is proposed:
The application body is deployed in the cloud function
Use the API gateway as the function entry
The page is deployed using object storage.
On the other hand, the database uses the cloud database under the same vpc (only mysql and redis have been tried because of limited financial resources. Theoretically, everything else should be feasible)
Attempt to deploy
To deploy the JAVA project to SCF, first understand what SCF is (the following is extracted from the Wechat open document)
Cloud functions are functions that run on the cloud (server side). In physical design, a cloud function can be composed of multiple files, occupying a certain amount of CPU memory and other computing resources. Each cloud function is completely independent and can be deployed in different regions. Developers do not need to buy or build servers, they just need to write function code and deploy it to the cloud to call Mini Program, and cloud functions can also call each other.
In fact, the cloud function is to split the business into function granularity and deploy on the cloud, so write a simple demo to deploy to the cloud function, and match it with the API gateway to try to call it.
/ * * pure javascf rapid development deployment (without springboot) * * @ author Freeeeeedom * @ date 10:31 on 2020-10-24 * / public class Scf {/ * * log Object * / private static Logger log = LoggerFactory.getLogger (Scf.class); private static DruidDataSource dataSource1 = new DruidDataSource (); static {/ / here load or modify data source multi-data source configuration multiple dataSource1.setUsername ("Freeeeeedom") DataSource1.setUrl ("jdbc:mysql://Freeeeeedom?autoReconnectForPools=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"); dataSource1.setPassword ("Freeeeeedom"); dataSource1.setMinIdle (1); dataSource1.setMaxActive (5); dataSource1.setMaxWait (10000); dataSource1.setValidationQuery ("SELECT 1 from dual"); log.info ("data Source load ok~") } / * pure scf entry parameter * * @ param insertParam input parameter * @ return java.lang.Object execution result * @ author Freeeeeedom * @ date 10:31 on 2020-10-24 * / public Object pure (Map insertParam) {log.info ("param: {}", gson.toJson (insertParam); Gson gson = new GsonBuilder (). DisableHtmlEscaping (). Create () Try {Class.forName ("com.mysql.jdbc.Driver");} catch (ClassNotFoundException e) {log.error ("Internal handling exception", e);} Map response = new HashMap (); JdbcTemplate jdbcTemplate = new JdbcTemplate (); jdbcTemplate.setDataSource (dataSource1); Map order = jdbcTemplate.queryForMap ("select order_id,create_time from `order` limit 1") Log.info (order.toString ()); return buildResponse (gson, gson.toJson (order), response);} private Object buildResponse (Gson gson, String json, Map response) {Map headers = new HashMap (1); headers.put ("Content-Type", "application/json"); response.put ("statusCode", HttpStatus.OK.value ()); response.put ("headers", headers) Response.put (body, json); return gson.toJson (response);}}
You just need to package the code and set the entry function to scf.Scf::pure to receive the data, and then query the id and creation time of the first order from the database and the ability to return:
Every time a cloud function is triggered through the API gateway, the pure method is triggered (Caller > call API Gateway > Cloud function-- > pure). However, it is found that the data source initialization of static will not be loaded repeatedly, which lays the foundation for springboot deployment.
Among them, the parameters brought by the API gateway are printed by log, directly copied into json, and then simulated by the main function, which realizes the local simulation of the call after serverless deployment.
Log.info ("param: {}", gson.toJson (insertParam)
With these foundations, you only need to have an entry class to simulate the loading started by springboot, and then map the entry parameters from the API gateway to implement the deployment of springboot on the cloud function (in fact, the super plus version of the above SCF class).
* * API gateway configuration * *
The path parameter here corresponds to the mapping path in springboot
Local debugging
With the above demo, we can see that it is no longer a problem for us to simulate cloud deployment. So how do you debug locally? The answer is simple: just start springboot and set it to normal.
Yes, it can be played directly with the native springboot. Deploying springboot to a cloud function is actually a plug-in to the startup class of springboot (the design pattern is called adapter pattern? (+ _ +)?
Function
Complete springboot, can be done with springboot can be achieved, I just wrote some small functions to verify this application.
[X] connect to the local server database
[X] Cloud database connection
[X] vpc database connection
[X] external interface call (send SMS verification code)
[X] implement simple order flow (crud)
[X] achieve simple login capabilities
[X] achieve simple data validation capabilities
The function of the whole project is simple but there is a lot of code.
Safety
First of all, the words "serverless", "Tencent" and "cloud service" are enough to represent security, but I tried to add something for the sake of functional integrity.
In this system, I chose to add a signature in header to verify the data. What is the reason? it is easy to operate and effective. Encryption means and schemes aside, from a process point of view, it is very convenient:
Get the header,body from the API gateway call parameters
Verify the validity of the data
Request to transfer to business module
Verify the validity of the data
Parameters enter the function module
Verify the validity of the data
.
In fact, only 123 steps are the most effective, and the next 45678 if you want to. Not to mention the authentication function provided by the API gateway itself.
Performance
Memory is only 300mb for an order system with a single request plus JVM, while the memory of a single function execution of a cloud function can be pulled to 3GB, so even a small amount of distributed computing should not be a big problem.
If there is concurrency, the maximum number of preset concurrency on the cloud function is 200. what about the order system, QPS1000?10000?100000? Ezpz is hundreds of thousands of orders of magnitude better than your own cabinet server.
There is not enough memory to expand the server capacity. It doesn't exist.
Last
Generate a VUE project, change the link tuning page, and then upload it to the bucket, click to open CDN ~ ("▽") ~ * perfect!
Aware of the progress of science and technology, the development of the times, the strength of Serverless.
The above content is how to carry out the order application of Serverless in Springboot. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.