我正在开发一个托管在谷歌云 Kubernetes 上的应用程序,它使用 Ingress 来管理到每个服务的路由,然后在每个服务内部有一些使用 golang 的额外路由。
我的问题是:当我尝试向 /api/auth/healthz 发送 API 请求时,我希望到达 statusHandler() 函数,而不是我总是点击 requestHandler() 函数,即使我在那里有一个 http.HandleFunc:http.HandleFunc("/healthz", statusHandler)
这是我的代码
func main() {
flag.Parse()
// initialize the "database"
err := store.Initialise()
if err != nil {
glog.Error(err)
glog.Error("Store failed to initialise")
}
defer store.Uninitialise()
// Initialize the default TTL for JWT access tokens to 1 day
tokenTTL = 24 * time.Hour
glog.Infof("JWT access tokens time-to-live set to: %s", tokenTTL.String())
http.HandleFunc("/healthz", statusHandler)
http.HandleFunc("/", requestHandler)
glog.Infof("Authentication service started on port 8080...\n")
http.ListenAndServe(":8080", nil)
}
func statusHandler(w http.ResponseWriter, r *http.Request) {
glog.Infof("Reached the status handler")
statusObj := serviceStatus{
GitSHA: os.Getenv("GIT_SHA"),
BuildTimestamp: os.Getenv("BUILD_TIMESTAMP"),
DeployEnv: os.Getenv("DEPLOY_ENV"),
}
statusJSON, _ := json.Marshal(statusObj)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "%s", statusJSON)
}
func requestHandler(w http.ResponseWriter, r *http.Request) {
glog.Infof("Reached the request handler")
...
}
这是我的 Ingress 代码:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
# https://github.com/kubernetes/ingress-nginx/blob/master/docs/user-guide/annotations.md
annotations:
kubernetes.io/ingress.class: nginx
certmanager.k8s.io/cluster-issuer: letsencrypt-staging
nginx.ingress.kubernetes.io/enable-cors: "false"
nginx.ingress.kubernetes.io/cors-enable-origin: "*"
nginx.ingress.kubernetes.io/auth-type: "basic"
nginx.ingress.kubernetes.io/auth-secret: "ci-ingress-auth"
nginx.ingress.kubernetes.io/proxy-body-size: "4g"
name: ci-api-ingress
namespace: ci
spec:
rules:
- host: ci.omitted.io
http:
paths:
- backend:
serviceName: auth-service << This is the service I am working on
servicePort: 8080
path: /api/auth
- backend:
serviceName: data-import-service
servicePort: 8080
path: /api/data-import
- backend:
serviceName: log-service
servicePort: 8080
path: /api/log
- backend:
serviceName: project-service
servicePort: 8080
path: /api/project
- backend:
serviceName: orchestration-service
servicePort: 8080
path: /api/orchestration
- backend:
serviceName: public
servicePort: 80
path: /
基于 Kubernetes' documentation这应该像他们所拥有的那样工作,类似于我的实现:
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
duration := time.Now().Sub(started)
if duration.Seconds() > 10 {
w.WriteHeader(500)
w.Write([]byte(fmt.Sprintf("error: %v", duration.Seconds())))
} else {
w.WriteHeader(200)
w.Write([]byte("ok"))
}
})
最佳答案
根据您的入口规则,请求的路径应该是:
/api/auth/healthz
不是/api/auth-service/healthz
入口路径/api/auth/保存在传递给应用程序服务器的请求 uri 中。
添加rewrite-target到入口注释将确保路径按照您的预期传递到您的底层服务器,例如作为/<path>而不是 /api/auth/<path> .检查应用程序服务器日志应该表明是这种情况。
ingress.kubernetes.io/rewrite-target: /
关于go - http.HandleFunc 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49481834/
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟此方法:defmethod_to_testurl=URI.parseurireq=Net::HTTP::Post.newurl.pathres=Net::HTTP.start(url.host,url.port)do|http|http.requestreq,foo:1endresend这是RSpec:let(:uri){'http://example.com'}specify'HTTPcall'dohttp=mock:httpNet::HTTP.stub!(:start).and_yieldhttphttp.shou
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request
在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo
我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur
使用Ruby1.9.2运行IDE提示说需要gemruby-debug-base19x并提供安装它。但是,在尝试安装它时会显示消息Failedtoinstallgems.Followinggemswerenotinstalled:C:/ProgramFiles(x86)/JetBrains/RubyMine3.2.4/rb/gems/ruby-debug-base19x-0.11.30.pre2.gem:Errorinstallingruby-debug-base19x-0.11.30.pre2.gem:The'linecache19'nativegemrequiresinstall
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
我知道全局变量$!包含最新的异常对象,但我对下面的语法感到困惑。谁能帮助我理解以下语法?rescue$! 最佳答案 此构造可防止异常停止您的程序并使堆栈跟踪冒泡。它还会将该异常作为值返回,这很有用。a=get_me_datarescue$!在此行之后,a将保存请求的数据或异常。然后您可以分析该异常并采取相应措施。defget_me_dataraise'Nodataforyou'enda=get_me_datarescue$!puts"Executioncarrieson"pa#>>Executioncarrieson#>>#更现实的