当前位置:网站首页>Openfaas practice 4: template operation

Openfaas practice 4: template operation

2022-04-23 15:01:00 InfoQ

  • go
  • java11
  • java11-vert-x
  • node
  • node12
  • php7
  • python
  • python3
  • python3-debian
  • ruby

  • With templates , You can create functions , The following command creates the name java-function Function of :

faas-cli new --lang java11 java-function

After success , Modify this file to add business code :./src/main/Handler.java;

  • Go ahead faas template store list When checking , Pay attention to the SOURCE Field , If it is openfaas-incubator, for example rust-http, Use the following command to download ( There's an extra prefix openfaas-incubator):

faas-cli template store pull openfaas-incubator/rust-http

The above is the basic operation of the template , Now you may have questions : Those are official templates , How to get the template of the third party ? In addition, if I want to make my own template for others to use , And how to operate ? These problems , And then I'll answer them one by one ;

[](() Using third party templates

  • I am here GitHub A third-party template warehouse has been built on , See how to use , Using other people's third-party templates is the same routine ;
  • The so-called template warehouse , Its essence is still ordinary GitHub Warehouse , It's just that the content should be in line with OpenFaaS The requirements of ;
  • My template repository address is :https://github.com/zq2599/openfaas-templates , Here's the picture :

![ Insert picture description here ](https://img-blog.csdnimg.cn/20201122111825108.jpg?x-oss-process=image/watermark,type_Zm 
《 A big factory Java Analysis of interview questions + Back end development learning notes + The latest architecture explanation video + Practical project source code handout 》 Free open source Prestige search official account 【 Advanced programming 】
 FuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2JvbGluZ19jYXZhbHJ5,size_16,color_FFFFFF,t_70#pic_center)

  • Download the above template repository command ( Be careful , Find a clean folder and execute the command ):

faas template pull https://github.com/zq2599/openfaas-templates

It can be seen that the warehouse address is placed at the end of the command as a parameter

  • The console operates as follows , You can see that you downloaded a template Catalog , There are two templates inside :dockerfile and java11extend:

[root@node1 333]# faas template pull https://github.com/zq2599/openfaas-templates

Fetch templates from repository: https://github.com/zq2599/openfaas-templates at master

2020/11/22 11:19:53 Attempting to expand templates from https://github.com/zq2599/openfaas-templates

2020/11/22 11:19:58 Fetched 2 template(s) : [dockerfile java11extend] from https://github.com/zq2599/openfaas-templates

[root@node1 333]# ls

template

[root@node1 333]# tree template/

template/

├── dockerfile

│ ├── function

│ │ └── Dockerfile

│ └── template.yml

└── java11extend

├── build.gradle

├── Dockerfile

├── function

│ ├── build.gradle

│ ├── gradle

│ │ └── wrapper

│ │ ├── gradle-wrapper.jar

│ │ └── gradle-wrapper.properties

│ ├── gradlew

│ ├── gradlew.bat

│ ├── settings.gradle

│ └── src

│ ├── main

│ │ └── java

│ │ └── com

│ │ └── openfaas

│ │ └── function

│ │ └── Handler.java

│ └── test

│ └── java

│ └── HandlerTest.java

├── gradle

│ └── wrapper

│ ├── gradle-wrapper.jar

│ └── gradle-wrapper.properties

├── README.md

├── settings.gradle

└── template.yml

16 directories, 17 files

  • perform faas new --list Look at the templates , Sure enough GitHub Two in the warehouse :

[root@node1 333]# faas new --list

Languages available as templates:

  • dockerfile
  • java11extend

  • Use templates to create functions :

faas-cli new java11extend-function --lang java11extend -p bolingcavalry

  • Making mirrors :

faas-cli build -f ./java11extend-function.yml

The console outputs the following information , It indicates that the image has been made successfully :

Step 29/30 : HEALTHCHECK --interval=5s CMD [ -e /tmp/.lock ] || exit 1

---> Running in 6b68ca763980

Removing intermediate container 6b68ca763980

---> 50cea9002e9c

Step 30/30 : CMD ["fwatchdog"]

---> Running in c2f87a87c8f4

Removing intermediate container c2f87a87c8f4

---> 8094a5064a20

Successfully built 8094a5064a20

Successfully tagged bolingcavalry/java11extend-function:latest

Image: bolingcavalry/java11extend-function:latest built.

[0] < Building java11extend-function done in 81.75s.

[0] Worker done.

Total build time: 81.76s

  • Next, push the image to the image warehouse 、 Deployment function 、 Please refer to the previous article for verification and other operations [《OpenFaaS The third part of the actual battle :Java function 》]((), There are detailed steps , Not in this article , The following image shows that the functions made by this template can be deployed and run normally :

null

[](() Make your own template warehouse

Experienced the actual combat ahead , For how to make template warehouse , I believe you have the answer in your heart , Here is a brief summary of the principles to be followed :

  • stay GitHub There's a public warehouse on ;
  • All content must be placed in master Branch ;
  • The warehouse should be named template Folder ;
  • template In the folder , Is each template's own folder , Here's the picture , It's two templates :

null
  • As for the contents of each template's folder , This involves template development , There's too much about it , Not in this article , Just the point :template.yml Is a function description file , There must be , And then there was Dockerfile, This is used to build a mirror image , As for other content, it is related to the characteristics of various templates , When you make a mirror image, you use , With java11 For example , contain gradle、java Related documents , It's all building java You'll use , and java The construction result of the project will be put into docker In the mirror , read Dockerfile You can see all this in detail ;
  • Now in order to make templates quickly , You can copy the content of the official template , You can use it if you change it , Address :https://github.com/openfaas/templates
  • When the warehouse is ready , Anyone can download the repository using commands like this :

faas template pull https://github.com/zq2599/openfaas-templates

[](() Be careful

Finally, there are two warm reminders , Please note that :

  • Now? GitHub New warehouse on , The default branch is no longer master 了 , It is mian, but fass-cli What the order says is master Branch , So please make sure that the contents of your warehouse files are in master Branch , instead of main
  • When executing the command to view the official template , You can see java8 The template of , The red box is shown below , I thought it was java The good news for developers :

null
Although you can see in the list java8, however OpenFaaS The official document announced that java8 The template has been abandoned , Not recommended , The red box is shown below , Document address :https://docs.openfaas.com/cli/templates/#java

null
  • These are the template related operations , This article is very important as a foundation , It is suggested that you should be familiar with the relevant commands , Lay a good foundation for further study ;

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231416491186.html