Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google app engine Google应用程序引擎模块主机名:不是应用程序引擎上下文_Google App Engine_Go_Google Cloud Platform - Fatal编程技术网

Google app engine Google应用程序引擎模块主机名:不是应用程序引擎上下文

Google app engine Google应用程序引擎模块主机名:不是应用程序引擎上下文,google-app-engine,go,google-cloud-platform,Google App Engine,Go,Google Cloud Platform,我正在尝试在应用程序引擎上发现其他已部署的服务。类似于这篇文章的建议 我的代码是这样的: import ( "fmt" "net/http" "google.golang.org/appengine" ) func ServiceHostname(serviceName string, r *http.Request) (string, error) { ctx := appengine.NewContext(r) hostname, err := a

我正在尝试在应用程序引擎上发现其他已部署的服务。类似于这篇文章的建议

我的代码是这样的:

import (
    "fmt"
    "net/http"

    "google.golang.org/appengine"
)

func ServiceHostname(serviceName string, r *http.Request) (string, error) {
    ctx := appengine.NewContext(r)
    hostname, err := appengine.ModuleHostname(ctx, serviceName, "", "")
    if err != nil {
        return "", fmt.Errorf("unable to find service %s: %v", serviceName, err)
    }
    return hostname, nil
}
我在常规http处理程序中调用此函数。我得到的错误是:
不是应用程序引擎上下文

我的代码和参考文章之间的唯一区别是应用程序内引擎go版本。我使用的是新的
go111
,他使用的是
go1
运行时


您知道如何克服这个问题吗?

您所引用的文章是根据第一代标准环境编写的,第二代(go111)在当时不是:

2018年10月10日

转到运行时注释

应用程序引擎标准环境的 现在是测试版。可以使用的迁移指南

这两代人之间的差异是显著的(对于所有语言,而不仅仅是围棋)。在迁移指南的一节中,我注意到:

  • 使用或您首选的上下文,而不是使用

这可能与您的错误有关。但我实际上不是go用户,这只是一个理论:)

您引用的文章是在考虑第一代标准环境的情况下编写的,第二代(go111)当时不是:

2018年10月10日

转到运行时注释

应用程序引擎标准环境的 现在是测试版。可以使用的迁移指南

这两代人之间的差异是显著的(对于所有语言,而不仅仅是围棋)。在迁移指南的一节中,我注意到:

  • 使用或您首选的上下文,而不是使用

这可能与您的错误有关。但我实际上不是一个go用户,这只是一个理论:)

我找到了解决方案。您需要在主文件中调用
appengine.Main()

因此,所讨论的代码保持不变,您需要注册与
go1.9
运行时相同的处理程序

func main() {
    http.HandleFunc("/serveurl", handle)
    appengine.Main()
}
资料来源:

这在以下章节中有提到:

  • 或者,如果您的服务使用的是
    google.golang.org/appengine
    软件包,则包括对
    appengine.Main()
    的调用

我找到了解决办法。您需要在主文件中调用
appengine.Main()

因此,所讨论的代码保持不变,您需要注册与
go1.9
运行时相同的处理程序

func main() {
    http.HandleFunc("/serveurl", handle)
    appengine.Main()
}
资料来源:

这在以下章节中有提到:

  • 或者,如果您的服务使用的是
    google.golang.org/appengine
    软件包,则包括对
    appengine.Main()
    的调用

据我所知,当AppEngine应用程序的运行时设置为go111,并且应用程序导入任何“google.golang.org/AppEngine”包(如log、urlfetch、memcache等)时,会返回此错误,但应用程序不调用“AppEngine.Main”方法

因此,您可以选择以下任一实现:

import (
    "fmt"
    "net/http"

    "google.golang.org/appengine"
)

func init() {
    // register handlers
    http.HandleFunc(“/“, indexHandler)

    appengine.Main()
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
    ctx := appengine.NewContext(r)
.......handle the request.......
}

func ServiceHostname(serviceName string, r *http.Request) (string, error) {
    ctx := appengine.NewContext(r)
    hostname, err := appengine.ModuleHostname(ctx, serviceName, "", "")
    if err != nil {
        return "", fmt.Errorf("unable to find service %s: %v", serviceName, err)
    }
    return hostname, nil
}
或放弃所有appengine软件包:

import (
    "fmt"
    “log”
    "net/http"
    “os”
)

func main() {
    port := os.Getenv(“PORT”)
    if port == “” {
        port = “8080”
    }

    http.HandleFunc(“/“, indexHandler)

    log.Fatal(http.ListenAndServe(“:”+port, Nil)
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
    ctx := r.Context()
.....handle request....
}


据我所知,当AppEngine应用程序的运行时设置为go111,并且应用程序导入任何“google.golang.org/AppEngine”包(如log、urlfetch、memcache等)时,会返回此错误,但应用程序不调用“AppEngine.Main”方法

因此,您可以选择以下任一实现:

import (
    "fmt"
    "net/http"

    "google.golang.org/appengine"
)

func init() {
    // register handlers
    http.HandleFunc(“/“, indexHandler)

    appengine.Main()
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
    ctx := appengine.NewContext(r)
.......handle the request.......
}

func ServiceHostname(serviceName string, r *http.Request) (string, error) {
    ctx := appengine.NewContext(r)
    hostname, err := appengine.ModuleHostname(ctx, serviceName, "", "")
    if err != nil {
        return "", fmt.Errorf("unable to find service %s: %v", serviceName, err)
    }
    return hostname, nil
}
或放弃所有appengine软件包:

import (
    "fmt"
    “log”
    "net/http"
    “os”
)

func main() {
    port := os.Getenv(“PORT”)
    if port == “” {
        port = “8080”
    }

    http.HandleFunc(“/“, indexHandler)

    log.Fatal(http.ListenAndServe(“:”+port, Nil)
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
    ctx := r.Context()
.....handle request....
}


谢谢你的回答和很好的理解,但这并没有解决我的问题。我曾尝试使用常规上下文作为文章建议,但错误依然存在。感谢您的回答和良好的理解,但这并没有解决我的问题。我尝试使用常规上下文作为文章建议,但错误保持不变。