go-micro kubernetes greeter example - 无法达到greeter api服务

时间:2018-04-20 07:36:11

标签: go kubernetes go-micro micro

我正试图在Kubernetes https://github.com/micro/examples/tree/master/greeter

上使用这个微观的示例

我可以在docker中本地运行它。但是当我尝试通过Kubernetes(http:// {{external-ip}} / greeter / say / hello)访问greeter api服务时,我收到错误:{“id”:“go.micro.api”, “code”:500,“detail”:“not found”,“status”:“Internal Server Error”}

为了排除故障我简化了方案,我只想通过micro api拨打一个go-micro api服务。以下是我的设置:

api.go

package main

import (
    "github.com/micro/go-micro"
    api "github.com/micro/micro/api/proto"
    "log"
    k8s "github.com/micro/kubernetes/go/micro"

    "context"
)

type Say struct {
}

//I just want to access this via the micro api on k8s via services external ip
func (s *Say) Hello(ctx context.Context, req *api.Request, rsp *api.Response) error {
    rsp.StatusCode = 200
    rsp.Body = "Hello"
    return nil
}

func main() {
    service := k8s.NewService(
        micro.Name("default.greeter-api"),
    )

    service.Init()

    service.Server().Handle(
        service.Server().NewHandler(
            &Say{},
        ),
    )

    if err := service.Run(); err != nil {
        log.Fatal(err)
    }
}

微API-deployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: micro
spec:
  replicas: 1
  selector:
    matchLabels:
      app: micro
  template:
    metadata:
      labels:
        app: micro
    spec:
        containers:
        - name: micro
          image: microhq/micro:kubernetes
          args:
            - "api"
            - "--handler=rpc"
            - "--namespace=default"
          env:
          - name: MICRO_API_ADDRESS
            value: ":80"
          ports:
          - containerPort: 80
            name: api-port

微API-svc.yml

apiVersion: v1
kind: Service
metadata:
  name: micro
spec:
  type: LoadBalancer
  ports:
  - name: api-http
    port: 80
    targetPort: "api-port"
    protocol: TCP
  selector:
    app: micro

迎接-deployment.yml

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: greeter-api
spec:
  replicas: 1
  selector:
    matchLabels:
      app: greeter-api
  template:
    metadata:
      labels:
        app: greeter-api
    spec:
        containers:
        - name: greeter-api-service
          image: greeter-api:latest
          imagePullPolicy: Always
          command: [
            "./greet",
            "--selector=static",
            "--server_address=:8080",
          ]
          ports:
          - containerPort: 8080
            name: greet-port

迎接-svc.yml

apiVersion: v1
kind: Service
metadata:
  name: greet
  labels:
    app: greet
spec:
  ports:
  - port: 8080
    protocol: TCP
  selector:
    app: greet

1 个答案:

答案 0 :(得分:1)

你的配置一切都很好。

  

http:// {{external-ip}} / greeter / say / hello),我收到错误:   {" ID":" go.micro.api""代码" 500"细节":"不   发现","状态":"内部服务器错误"}

您错过了请求中的端口号8080,并尝试拨打rpc服务而不是greeter-api