开发者社区 > 云原生 > Serverless > 正文

如何使用Serverless Devs部署golang进程,golang进程启动的时候需要访问con

如何使用Serverless Devs部署golang进程,golang进程启动的时候需要访问config.yaml,怎么挂载这个配?

展开
收起
Java工程师 2023-04-09 15:16:04 647 0
3 条回答
写回答
取消 提交回答
  • 您可以使用Serverless Devs提供的配置文件来部署golang进程,并且可以通过挂载config.yaml文件来让进程访问配置文件。具体操作步骤为:在Serverless Devs配置文件中添加挂载配置文件的参数,例如:

    - name: my-golang-function
      runtime: custom
      handler: main
      codeUri: ./
      customContainerConfig:
        image: registry.cn-hangzhou.aliyuncs.com/aliyunfc/golang:1.13.8
        command: ["./main"]
        args: ["--config", "/mnt/auto/config.yaml"]
        mountPoints:
          - sourceVolume: my-config-volume
            destinationPath: /mnt/auto
      events:
        - http:
            path: /hello
            method: GET
    

    其中,mountPoints参数用于挂载配置文件,sourceVolume参数指定配置文件所在的本地目录,destinationPath参数指定配置文件在容器中的挂载路径。在golang进程启动时,可以通过--config参数指定配置文件的路径,例如:./main --config /mnt/auto/config.yaml

    2023-04-10 08:51:22
    赞同 展开评论 打赏
  • 坚持这件事孤独又漫长。

    可以通过在Serverless Devs的资源配置文件中指定golang进程启动需要访问的config.yaml文件,然后将该文件挂载至golang进程所在的容器中,以便golang进程能够访问到该配置文件。

    以下是一个简单的示例,供参考:

    • 首先,在当前项目的目录下创建一个config.yaml文件,内容如下:
    app_name: "my_app"
    app_port: 8080
    db_host: "localhost"
    db_port: 3306
    db_name: "my_db"
    db_user: "root"
    db_pass: "password"
    
    • 然后,在resources.yaml文件中进行如下配置:
    ROSTemplateFormatVersion: "2015-09-01"
    Transform: "Aliyun::Serverless-2018-04-03"
    
    Resources:
      my_golang_function:
        Type: "Aliyun::Serverless::Function"
        Properties:
          Handler: "my_golang_function"
          Runtime: "custom"
          CodeUri: "./"
          MemorySize: 128
          Timeout: 60
          EnvironmentVariables:
            CONFIG_PATH: "/mnt/conf/config.yaml"
          Mounts:
            - type: bind
              source: ${PWD}/config.yaml
              target: /mnt/conf/config.yaml
    

    在上述代码中,我们创建了一个名为my_golang_function的Serverless函数,并指定了它的处理程序为my_golang_function。我们使用了custom作为golang进程的运行时,并将代码目录./作为代码包上传至阿里云函数计算平台。我们还指定了该函数执行的内存大小和最长执行时间。

    为了将config.yaml文件挂载到golang进程所在的容器中,我们使用了EnvironmentVariablesMounts两个配置项。其中,EnvironmentVariables用于传递一些环境变量至容器中,我们指定了CONFIG_PATH环境变量的值为/mnt/conf/config.yaml,即我们配置文件的挂载路径;而Mounts则用于在容器中挂载一些文件或目录,我们指定将当前目录下的config.yaml文件挂载到容器中的/mnt/conf/config.yaml路径下。

    • 最后,我们需要在golang进程中读取config.yaml文件。假设我们的golang代码为:
    package main
    
    import (
    	"fmt"
    	"io/ioutil"
    	"log"
    	"os"
    )
    
    func main() {
    	confPath := os.Getenv("CONFIG_PATH")
    	content, err := ioutil.ReadFile(confPath)
    	if err != nil {
    		log.Fatal(err)
    	}
    	fmt.Println(string(content))
    }
    

    我们通过调用os.Getenv函数获取环境变量CONFIG_PATH的值,并通过ioutil.ReadFile函数读取指定路径下的文件内容。最后,我们使用fmt.Println函数将内容输出至控制台。在这个例子中,我们将读取并输出config.yaml文件的全部内容。实际上,在真实的golang应用中,我们可以根据需要自己解析、使用该配置文件。

    2023-04-09 18:38:49
    赞同 展开评论 打赏
  • 给你个模板:https://github.com/VinerFiner/start-cloudreve

    此答案来自钉钉群“阿里函数计算官网客户"

    2023-04-09 16:59:06
    赞同 展开评论 打赏

快速交付实现商业价值。

相关产品

  • 函数计算
  • 相关电子书

    更多
    All in Serverless 阿里云核心产品全面升级 立即下载
    AIGC 浪潮之上,森马的 Serverless 实践之旅 立即下载
    极氪大数据 Serverless 应用实践 立即下载