Hertz Documentation

repository·main·Indexed 27 days ago

https://github.com/cloudwego/hertz

Hertz is a high-performance Golang HTTP framework optimized for microservices. It features a layered architecture for extending protocols and network layers, natively supporting HTTP/1.1 and ALPN. The framework includes the hz CLI for scaffolding projects, generating clients, and creating Go models from Thrift or Protobuf IDL files. It offers a wide ecosystem of extensions via Hertz-contrib for observability, authentication, and service discovery, and supports advanced features like Server-Sent Events (SSE) and custom net.Listener integration.

Tokens
19.6K
Snippets
15
Records
173
Agent score
92%

What's inside Hertz

  1. Overview of Hertz

    main
    Hertz is a high-performance Golang microservice HTTP framework. It was originally forked from fasthttp and incorporates design strengths from frameworks like gin and echo. It is designed for high usability, high performance, and high extensibility, making it suitable for microservices with strict performance and customization requirements.
  2. Overview of Hertz HTTP Framework

    main

    Hertz is a high-performance, high-usability, and highly extensible Golang HTTP framework designed for building microservices. It is inspired by frameworks like Gin and Echo but optimized for high-performance requirements.

    Key features include:

    • High Performance: Uses the Netpoll network library by default, offering advantages in QPS and latency compared to the standard Go Net library.
    • Multi-protocol Support: Natively supports HTTP/1.1 and ALPN, with support for custom protocol resolution logic.
    • Network Layer Flexibility: Allows switching between Netpoll and Go Net, or extending the network layer via plugins.
    • Extensibility: Features a layered design that supports extensions for protocols, network libraries, loggers, and monitoring.
  3. Explore Hertz-contrib extensions

    main
    Hertz can be extended using the Hertz-contrib library, a community-maintained collection of extensions. These can be integrated into your Hertz application via options to add functionality such as protocol support, observability, authentication, and more.
  4. Key Features of Hertz

    main

    Hertz provides several core capabilities:

    • High Performance: Uses the Netpoll high-performance network library by default, offering advantages in QPS and latency over the standard Go net library.
    • High Extensibility: Employs a layered design that allows users to extend network libraries, protocols, logging, monitoring, and service discovery.
    • Multi-protocol Support: Natively supports HTTP/1.1 and ALPN. The layered design allows for custom protocol parsing logic.
    • Network Layer Switching: Allows users to switch between Netpoll and the standard Go network library on demand or extend it via plugins.
  5. Configure Service Registration and Discovery (v0.3.0 features)

    main

    Hertz v0.3.0 introduced several components for service discovery and registration:

    • Service Registration: Use the WithRegistry option on the server to register services.
    • Service Discovery: Use the Discovery client middleware to integrate with service discovery systems. The discovery package provides Resolver, Instance, Result, and NewInstance types.
    • Registry: The registry package provides Registry and Info types for managing service registration.
    • Load Balancing: The loadbalance package provides a Loadbalancer interface and NewWeightedBalancer for weighted random load balancing.
  6. Generate Hertz client code from IDL

    main

    Use the client command to generate a Hertz HTTP client based on your service definitions.

    Key Flags:

    • --idl: Path to the IDL file.
    • --base_domain: The default request domain for the generated client (e.g., localhost:8888).
    • --client_dir: The output directory for client code. If omitted, it defaults to a path derived from the IDL namespace.
    • --force_client_dir: If used, the client is output without IDL namespace subdirectories.
    • --force_client: Force regeneration of hertz_client.go even if it exists.
    • --query_enumint: Use numeric values for enum query parameters in the client.
    • --enable_optional: Omit optional Thrift fields from queries if they are not set.
    hz client --idl api.thrift --base_domain localhost:8888
  7. Configure Write, Read, and Dial timeouts

    main

    Hertz provides options to set timeouts for both servers and clients, as well as per-request configurations.

    Server/Client Options: Use WithWriteTimeout to set a write timeout.

    Per-request Options: Use the following options to configure specific requests:

    • WithDialTimeout
    • WithReadTimeout
    • WithWriteTimeout

    Error Handling: When a timeout occurs, the following errors are returned:

    • ErrReadTimeout
    • ErrWriteTimeout
    • ErrDialTimeout