Golang框架Go-zero v1.2.3版本正式发布

Golang框架Go-zero v1.2.3版本正式发布 框架:

  1. 在 rest(Web 框架)中支持 TLS
  2. 在 zrpc 中支持 TLS
  3. 为 Redis 连接支持 TLS
  4. 为 etcd 支持 username/password 认证
  5. 在 rest(Web 框架)中支持 CORS
  6. 支持路由组的 prefix,也支持通过 .api 文件中的 prefix 设置
  7. 支持为特定路由设置独立的 timeout
  8. 支持 NonBlock 依赖检查模式,默认为阻塞模式
  9. 现在可以在 redis、sqlx、mongo、rest、zrpc 中设置统计日志的 slow threshold
  10. 小错误修复和改进

goctl:

  1. 支持在 .api 文件中生成 prefix 代码
  2. 支持多版本模板
  3. 优化请求体为空的语法
  4. 修复 grpc 客户端包生成的问题
  5. goctl 在失败时返回非零状态码
  6. 错误修复和小改进

https://github.com/zeromicro/go-zero


更多关于Golang框架Go-zero v1.2.3版本正式发布的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于Golang框架Go-zero v1.2.3版本正式发布的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


Go-zero v1.2.3版本带来了多项重要更新,以下是关键特性的代码示例:

1. REST框架TLS支持

// etc/rest.yaml
server:
  host: 0.0.0.0
  port: 8080
  certFile: server.crt
  keyFile: server.key

// main.go
import "github.com/zeromicro/go-zero/rest"

func main() {
    // 自动启用TLS
    rest.MustNewServer(c.RestConf).Start()
}

2. zRPC TLS支持

// etc/zrpc.yaml
Target: localhost:8080
CertFile: client.crt
KeyFile: client.key
CACertFile: ca.crt

// 客户端连接
conn := zrpc.MustNewClient(zrpc.RpcClientConf{
    Target: "localhost:8080",
    CertFile: "client.crt",
    KeyFile: "client.key",
    CACertFile: "ca.crt",
})

3. Redis TLS连接

// etc/redis.yaml
host: localhost:6379
tls: true
certFile: redis.crt
keyFile: redis.key
caCertFile: ca.crt

// 代码中使用
import "github.com/zeromicro/go-zero/core/stores/redis"
redisClient := redis.MustNewRedis(redisConf)

4. etcd用户名密码认证

// etc/etcd.yaml
hosts:
  - localhost:2379
user: myuser
pass: mypassword

// 服务发现配置
import "github.com/zeromicro/go-zero/zrpc/resolver"
etcdConf := etcd.EtcdConf{
    Hosts: []string{"localhost:2379"},
    User: "myuser",
    Pass: "mypassword",
}

5. REST CORS支持

// etc/rest.yaml
server:
  host: 0.0.0.0
  port: 8080
  cors:
    maxAge: 3600
    allowCredentials: true
    allowedOrigins:
      - "https://example.com"
    allowedMethods:
      - GET
      - POST
      - PUT
    allowedHeaders:
      - Content-Type
      - Authorization

6. 路由组前缀支持

// api文件示例
syntax = "v1"

@server(
    prefix: /api/v1
)
service user {
    @handler getUser
    get /user/:id (GetUserReq) returns (GetUserResp)
    
    @handler createUser
    post /user (CreateUserReq) returns (CreateUserResp)
}

// 生成的代码自动包含/api/v1前缀

7. 特定路由超时设置

// api文件配置
syntax = "v1"

service user {
    @handler slowOperation
    post /slow (SlowReq) returns (SlowResp)
}

// etc/rest.yaml
server:
  host: 0.0.0.0
  port: 8080
  timeout: 3000  // 默认3秒
  routes:
    - method: post
      path: /slow
      timeout: 10000  // 该路由单独设置10秒超时

8. NonBlock依赖检查模式

// main.go
import "github.com/zeromicro/go-zero/core/proc"

func main() {
    // 设置为非阻塞模式
    proc.SetNonBlock(true)
    
    // 启动服务
    server := rest.MustNewServer(c.RestConf)
    defer server.Stop()
    server.Start()
}

9. 慢查询日志阈值配置

// etc/rest.yaml
server:
  host: 0.0.0.0
  port: 8080
  logSlow: true
  slowThreshold: 2000  // 2秒以上的请求记录为慢查询

// Redis配置
redis:
  host: localhost:6379
  slowThreshold: 100  // 100毫秒以上的Redis操作记录为慢查询

goctl新特性示例

多版本模板支持

# 创建多版本API模板
goctl api new user --template-version=v1
goctl api new user --template-version=v2

# 生成代码时指定版本
goctl api go -api user.api -dir . -style gozero --template-version=v2

请求体为空语法优化

// 之前的写法
type EmptyReq struct{}

// 现在可以直接使用
@handler example
post /empty () returns (Response)

这些更新显著增强了Go-zero框架的安全性和灵活性,特别是TLS支持和细粒度的配置选项。

回到顶部