kraots2.0 在windows 环境搭建开发环境

  • 首先需要准备的东西
    • go 
    • protoc 直接去这里下 https://github.com/protocolbuffers/protobuf/releases 然后把exe 文件放置到go Path目录
    • protoc-gen-go 直接通过go install 命令进行安装
    • 使用Chocolatey 安装make
  •  安装 kratos 命令工具
    •  go install github.com/go-kratos/kratos/cmd/kratos/v2@latest
    • 这里会有一些奇葩的path 问题。但是我们不需要关注,如果install命令执行没有问题既可以使用everyThing 这个软件加上.exe后缀 查找到build之后的exe文件。把它放置到go Path目录让系统可以识别命令即可。
    • 还有很多问题都是环境变量导致的。 使用  Get-Command 命令查看当前命令执行的是哪个目录的exe 
  • 使用kratos new 命令创建项目
  • 使用make 在windows环境进行build项目 
    • 这里核心的一个问题是官方提供的makefile 文件中 使用的是gitbash 的find.exe 来替代windos环境的find命令。 
    • 容易出现文件夹带空格等一系列问题。 我的解决方案是把git find.exe 在系统中的环境变量优先于 /system32 中的find.exe 这样就不用担心find 命令 windows和liunx的差异问题了
    •  修改完find的默认执行方式之后就不存在系统的差异了下面这段make 脚本可以直接修改成为 :

      ifeq ($(GOHOSTOS), windows)
          #the `find.exe` is different from `find` in bash/shell.
          #to see https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/find.
          #changed to use git-bash.exe to run find cli or other cli friendly, caused of every developer has a Git.
          #Git_Bash= $(subst cmd\,bin\bash.exe,$(dir $(shell where git)))
          Git_Bash=$(subst \,/,$(subst cmd\,bin\bash.exe,$(dir $(shell where git))))
          INTERNAL_PROTO_FILES=$(shell $(Git_Bash) -c "find internal -name *.proto")
          API_PROTO_FILES=$(shell $(Git_Bash) -c "find api -name *.proto")
      else
          INTERNAL_PROTO_FILES=$(shell find internal -name *.proto)
          API_PROTO_FILES=$(shell find api -name *.proto)
      endif
      
      可以直接修改成为以下内容
          INTERNAL_PROTO_FILES=$(shell find internal -name *.proto)
          API_PROTO_FILES=$(shell find api -name *.proto)

       在 make generate 的时候可能会出现问题 可能是go env 的设置问题。 如果是windows环境配置的是liunx 的时候寻找包的目录可能会有问题。

       go env -w GOOS=windows
    • 如果要打Linux 的包的话使用以下命令, 但是在generate 的步骤GOOS 配置最好和本机系统保持一致
      go env -w GOOS=windows 进行go build 即可
    • 使用wire尽量不要编辑wire_gen.go文件这个文件尽量使用命令来修改, 我们应该修改的是wire.go 这个文件。

      最后执行make all 进行项目代码生成 ,途中如果报找不到protc命令 使用go install 安装对应的命令即可。 

posted @ 2024-02-01 17:31  雨V幕  阅读(95)  评论(0编辑  收藏  举报