美女天下 發表於 2022-5-29 22:07:00

Go微服务框架go-kratos实战学习01:quickstart 快速创建项目

<p>先来感受下用 kratos 快速创建项目</p>
<h2 id="一环境准备">一、环境准备</h2>
<p>1.1 安装依赖软件</p>
<ul>
<li>protoc</li>
<li>protoc-gen-go</li>
</ul>
<p>安装 protoc:</p>
<ul>
<li>到 protobuf release 页面,选择适合自己操作系统的文件包。<br>
或者文档</li>
<li>也可以看 grpc.io 官方安装文档: https://grpc.io/docs/protoc-installation/</li>
</ul>
<p>安装 protoc-gen-go:</p>
<ul>
<li>简单点可以直接用 go install 安装 :<code>go install google.golang.org/protobuf/cmd/protoc-gen-go@latest</code><br>
或者看文档</li>
<li>https://developers.google.com/protocol-buffers/docs/reference/go-generated</li>
</ul>
<p>建议开启 GO111MODULE</p>
<p>一些相关文档:</p>
<ul>
<li>https://developers.google.com/protocol-buffers</li>
<li>https://github.com/protocolbuffers/protobuf</li>
<li>https://grpc.io</li>
<li>https://github.com/grpc/grpc-go</li>
</ul>
<p>1.2 安装 kratos cli</p>
<blockquote>
<p>go install github.com/go-kratos/kratos/cmd/kratos/v2@latest</p>
</blockquote>
<p>CLI 工具使用说明:CLI工具使用</p>
<p>go-kratos 和 Go 版本:</p>
<blockquote>
<p>go 1.17<br>
go-kratos v2.2.1</p>
</blockquote>
<h2 id="二创建和运行项目">二、创建和运行项目</h2>
<p>2.1 通过 <code>kratos new</code> 命令创建项目模板</p>
<p>使用 <code>kratos new</code> 命令创建 quickstart 项目:</p>
<blockquote>
<p>kratos new quickstart</p>
</blockquote>
<p>使用该命令创建项目:</p>
<pre><code class="language-shell">$ kratos new quickstart
🚀 Creating service quickstart, layout repo is https://github.com/go-kratos/kratos-layout.git, please wait a moment.

From https://github.com/go-kratos/kratos-layout
   cf30efc..cc5192fmain       -&gt; origin/main
*          v2.2.1   -&gt; v2.2.1
*          v2.1.3   -&gt; v2.1.3
*          v2.1.4   -&gt; v2.1.4
*          v2.1.5   -&gt; v2.1.5
*          v2.2.0   -&gt; v2.2.0
Updating cf30efc..cc5192f
Fast-forward
.github/workflows/gitee-sync.yml                   |27 +
Makefile                                           |35 +-
README.md                                          |   2 +-
api/helloworld/v1/error_reason.pb.go               | 100 ++--
api/helloworld/v1/error_reason.proto               |11 +-
api/helloworld/v1/error_reason_errors.pb.go      |30 -
api/helloworld/v1/greeter.pb.go                  | 114 ++--

... ...


🍺 Project creation succeeded quickstart
💻 Use the following command to start the project 👇:

$ cd quickstart
$ go generate ./...
$ go build -o ./bin/ ./...
$ ./bin/quickstart -conf ./configs

                        🤝 Thanks for using Kratos
      📚 Tutorial: https://go-kratos.dev/docs/getting-started/start
</code></pre>
<p>如果拉取 github 上的项目模板失败,可以使用 <code>-r</code> 参数指定拉取项目模板地址.</p>
<p>比如拉取 gitee 上的模板:</p>
<blockquote>
<p>kratos new quickstart -r https://gitee.com/go-kratos/kratos-layout.git</p>
</blockquote>
<p>更多命令的使用:kratos 命令使用</p>
<p>2.2 使用 go generate 命令生成相应代码</p>
<p>生成 proto 源码、wire 等等:</p>
<pre><code class="language-shell">$ go generate ./...
go: downloading github.com/go-kratos/kratos/v2 v2.2.1
go: downloading google.golang.org/genproto v0.0.0-20220222213610-43724f9ea8cf
go: downloading github.com/go-logr/logr v1.2.1
go: downloading github.com/go-logr/stdr v1.2.0

... ...
</code></pre>
<p>2.3 运行项目</p>
<p>使用 <code>kratos run</code> 命令运行项目</p>
<pre><code class="language-shell">$ kratos run
INFO msg=config loaded: config.yaml format: yaml
INFO msg= server listening on: [::]:9000
INFO msg= server listening on: [::]:8000
</code></pre>
<p>2.4 测试接口</p>
<p>我使用的 go 写的 curlie:https://github.com/rs/curlie 测试:</p>
<pre><code class="language-shell">$ curliehttp://localhost:8000/helloworld/kratos
HTTP/1.1 200 OK
{
    "message": "Hello kratos"
}
</code></pre>
<h2 id="三kratos-项目布局">三、kratos 项目布局</h2>
<p>基于 kratos-layout 创建的项目,使用的 kratos new 命令:</p>
<blockquote>
<p>kratos new <project-name></project-name></p>
</blockquote>
<p>生成的目录结构图如下:</p>
<pre><code class="language-shell">.
├── Dockerfile
├── LICENSE
├── Makefile
├── README.md
├── api // 下面维护了微服务使用的proto文件以及根据它们所生成的go文件
│&nbsp;&nbsp; └── helloworld
│&nbsp;&nbsp;   └── v1
│&nbsp;&nbsp;         ├── error_reason.pb.go
│&nbsp;&nbsp;         ├── error_reason.proto
│&nbsp;&nbsp;         ├── error_reason.swagger.json
│&nbsp;&nbsp;         ├── greeter.pb.go
│&nbsp;&nbsp;         ├── greeter.proto
│&nbsp;&nbsp;         ├── greeter.swagger.json
│&nbsp;&nbsp;         ├── greeter_grpc.pb.go
│&nbsp;&nbsp;         └── greeter_http.pb.go
├── cmd// 整个项目启动的入口文件
│&nbsp;&nbsp; └── server
│&nbsp;&nbsp;   ├── main.go
│&nbsp;&nbsp;   ├── wire.go// 我们使用wire来维护依赖注入
│&nbsp;&nbsp;   └── wire_gen.go
├── configs// 这里通常维护一些本地调试用的样例配置文件
│&nbsp;&nbsp; └── config.yaml
├── generate.go
├── go.mod
├── go.sum
├── internal// 该服务所有不对外暴露的代码,通常的业务逻辑都在这下面,使用internal避免错误引用
│   ├── biz   // 业务逻辑的组装层,类似 DDD 的 domain 层,data 类似 DDD 的 repo,而 repo 接口在这里定义,使用依赖倒置的原则。
│&nbsp;&nbsp; │&nbsp;&nbsp; ├── README.md
│&nbsp;&nbsp; │&nbsp;&nbsp; ├── biz.go
│&nbsp;&nbsp; │&nbsp;&nbsp; └── greeter.go
│&nbsp;&nbsp; ├── conf// 内部使用的config的结构定义,使用proto格式生成
│&nbsp;&nbsp; │&nbsp;&nbsp; ├── conf.pb.go
│&nbsp;&nbsp; │&nbsp;&nbsp; └── conf.proto
│&nbsp;&nbsp; ├── data// 业务数据访问,包含 cache、db 等封装,实现了 biz 的 repo 接口。我们可能会把 data 与 dao 混淆在一起,data 偏重业务的含义,它所要做的是将领域对象重新拿出来,我们去掉了 DDD 的 infra层。
│&nbsp;&nbsp; │&nbsp;&nbsp; ├── README.md
│&nbsp;&nbsp; │&nbsp;&nbsp; ├── data.go
│&nbsp;&nbsp; │&nbsp;&nbsp; └── greeter.go
│&nbsp;&nbsp; ├── server// http和grpc实例的创建和配置
│&nbsp;&nbsp; │&nbsp;&nbsp; ├── grpc.go
│&nbsp;&nbsp; │&nbsp;&nbsp; ├── http.go
│&nbsp;&nbsp; │&nbsp;&nbsp; └── server.go
│&nbsp;&nbsp; └── service// 实现了 api 定义的服务层,类似 DDD 的 application 层,处理 DTO 到 biz 领域实体的转换(DTO -&gt; DO),同时协同各类 biz 交互,但是不应处理复杂逻辑
│&nbsp;&nbsp;   ├── README.md
│&nbsp;&nbsp;   ├── greeter.go
│&nbsp;&nbsp;   └── service.go
└── third_party// api 依赖的第三方proto
    ├── README.md
    ├── google
    │&nbsp;&nbsp; └── api
    │&nbsp;&nbsp;   ├── annotations.proto
    │&nbsp;&nbsp;   ├── http.proto
    │&nbsp;&nbsp;   └── httpbody.proto
    └── validate
      ├── README.md
      └── validate.proto
</code></pre>
<p>整个项目架构流程图,官方的一个架构图:</p>
<p><img src="https://img2022.cnblogs.com/blog/650581/202205/650581-20220529220545005-1708774495.png" alt="image-20220528195535136" loading="lazy"></p>
<p>上面虽然对代码结构做了文字说明,但是在 internal 里,有 DDD 这个概念,相信很多人看了后,不是很明白。</p>
<p>先来看看 DDD 的分层架构,架构图如下:</p>
<p><img src="https://img2022.cnblogs.com/blog/650581/202205/650581-20220529220544996-1213643788.png" alt="image-20220529175626340" loading="lazy"></p>
<p>再来对比看看 internal 目录里的 biz、data、service、server、conf 这 5 个目录。</p>
<ul>
<li>biz:文档里说了类似 DDD 的 domain 层,也就是 DDD 架构中的领域层。这里还定义了对业务操作的接口。业务逻辑组装。</li>
<li>data:对数据库 db,缓存 cache 的封装,并且实现 biz 中定义的接口。它将领域对象重新拿出来,这里去掉了 DDD 的基础层。</li>
<li>service:实现 api 定义的服务层,类似 DDD 的应用层。处理数据传输对象到 biz(领域实体)的转换。同时协同各类 biz 交互,不应处理复杂逻辑。</li>
<li>server:http 和 grpc 实例的创建和配置,以及注册对应的 service。</li>
</ul>
<blockquote>
<p>service -&gt; biz -&gt; data</p>
</blockquote>
<blockquote>
<p>也可以到公号讨论本文:九卷技术录-go-kratos实战学习01:quickstart 快速创建项目</p>
</blockquote>
<h2 id="四参考">四、参考</h2>
<ul>
<li>https://go-kratos.dev/docs/getting-started/start kratos 项目初始化</li>
<li>https://go-kratos.dev/docs/intro/layout kratos 项目结构</li>
<li>https://developers.google.com/protocol-buffers</li>
<li>https://github.com/protocolbuffers/protobuf</li>
<li>https://grpc.io</li>
<li>https://github.com/grpc/grpc-go</li>
</ul>


</div>
<div id="MySignature" role="contentinfo">
    == just do it ==<br><br>
来源:https://www.cnblogs.com/jiujuan/p/16322725.html
頁: [1]
查看完整版本: Go微服务框架go-kratos实战学习01:quickstart 快速创建项目