trpc.group/trpc-go/trpc-go@v1.0.3/docs/user_guide/framework_conf.md (about)

     1  English | [中文](framework_conf.zh_CN.md)
     2  
     3  # tRPC-Go Framework Configuration
     4  
     5  ## Introduction
     6  
     7  The tRPC-Go framework configuration is a configuration file defined by the framework and used for framework initialization. The core of the tRPC framework adopts a plug-in architecture, which components all core functions, and connects all component functions in series through the interface-based programming thinking. Each component is associated with the plug-in SDK through configuration. The tRPC framework provides the `trpc_go.yaml` framework configuration file by default, which gathers the configuration of all basic components into the framework configuration file and passes it to the components when the service starts. In this way, each components don't have to manage their own configuration independently.
     8  
     9  Through the introduction of this article, I hope to help users understand the following:
    10  - Components of framework configuration
    11  - How to obtain the implication, value range, and default value of configuration parameters
    12  - How to generate and manage configuration files
    13  - How to use the framework configuration, whether it can be configured dynamically
    14  
    15  ## How To Use
    16  
    17  First of all, the tRPC-Go framework does not support the dynamic update of the framework configuration. After modifying the framework configuration, the user needs to **restart the service** to take effect. There are three ways to set the frame configuration.
    18  
    19  ### Use Configuration File
    20  
    21  **Recommended approach** This method use the framework configuration file. When `trpc.NewServer()` starts, it will first parse the framework configuration file, automatically initialize all configured plug-ins, and start the service. It is recommended that other initialization logic be placed after `trpc.NewServer()` to ensure that the framework functions have been initialized. The default framework configuration file name of tRPC-Go is `trpc_go.yaml`, and the default path is the working path of the current program startup. The configuration path can be specified by the `-conf` command line parameter.
    22  
    23  ```go
    24  // Initialize tRPC service by using the framework configuration file.
    25  func NewServer(opt ...server.Option) *server.Server
    26  ```
    27  
    28  ### Build Configuration Data
    29  
    30  **Not recommended approach** This method does not require a framework configuration file, but user needs to assemble the startup parameter `*trpc.Config` by himself. The disadvantage of using this method is that the flexibility of configuration changes is poor. Any configuration modification requires code changes, and the decoupling of configuration and program code cannot be achieved.
    31  
    32  ```go
    33  // User builds cfg framework configuration data, and initialize tRPC service.
    34  func NewServerWithConfig(cfg *Config, opt ...server.Option) *server.Server;
    35  ```
    36  
    37  ### Modify Configuration with Option
    38  
    39  Both of these methods provide `Option` parameters to change local parameters. `Option` configuration takes precedence over framework configuration file configuration and `Config` configuration data. An example of using `Option` to modify the framework configuration is as follows.
    40  
    41  ```go
    42  import (
    43    trpc "trpc.group/trpc-go/trpc-go"
    44      server "trpc.group/trpc-go/trpc-go/server"
    45  )
    46  func main() {
    47      s := trpc.NewServer(server.WithEnvName("test"), server.WithAddress("127.0.0.1:8001"))
    48      //...
    49  }
    50  ```
    51  
    52  > In the rest of this article, we will only discuss the framework configuration file pattern.
    53  
    54  
    55  ## Configuration Design
    56  
    57  ### The Overall Structure
    58  
    59  The framework configuration file design is mainly divided into four groups.
    60  
    61  | Group | Description |
    62  | ------ | ------ |
    63  | global | The global configuration defines general configurations such as environment-related. |
    64  | server | The server configuration defines the general configuration of the program as a server, including application name, program name, configuration path, interceptor list, `Naming Service` list, etc. |
    65  | client | The client configuration defines the general configuration of the program as a client, including interceptor list, the configuration of the `Naming Service` list to be accessed, etc. It is recommended to use the configuration center first for client configuration, and then the `client` configuration in the framework configuration file. |
    66  | plugins | The plugin configuration collects all the configurations that use plugins. Since `plugins` use a `map` to manage out of order, the framework will randomly pass the plugin configurations to the SDK one by one at startup, and start the plugins. The plugin configuration format is determined by the plugin itself. |
    67  
    68  ### Configuration Details
    69  
    70  ``` yaml
    71  # In the following configurations, unless otherwise specified: String type defaults to ""; Integer type defaults to 0; Boolean type defaults to false; [String] type defaults to [].
    72  
    73  # Global configuration
    74  global:
    75    # Required, usually use Production or Development
    76    namespace: String
    77    # Optional, environment name
    78    env_name: String
    79    # Optional, container name
    80    container_name: String
    81    # Optional, when the server IP is not configured, use this field as the default IP
    82    local_ip: String(ipv4 or ipv6)
    83    # Optional, whether to enable the set function for service discovery, the default is N (note that its type is String, not Boolean)
    84    enable_set: String(Y, N)
    85    # Optional, the name of the set group
    86    full_set_name: String([set name].[set region].[set group name])
    87    # Optional, the size of the network receiving buffer (unit B). <=0 means disabled, the default value 4096 is used when leave the field blank
    88    read_buffer_size: Integer
    89  
    90  # Server configuration
    91  server:
    92    # Required, the application name of the service
    93    app: String
    94    # Required, the service name of the service
    95    server: String
    96    # Optional, the path to the binary file
    97    bin_path: String
    98    # Optional, the path to the data file
    99    data_path: String
   100    # Optional, the path to the configuration file
   101    conf_path: String
   102    # Optional, network type, when the service is not configured with network, this field is valid, and the default is tcp
   103    network: String(tcp, tcp4, tcp6, udp, udp4, udp6)
   104    # Optional, protocol type, when the service is not configured with protocol, this field is valid, and the default is trpc
   105    protocol: String(trpc, grpc, http, etc.)
   106    # Optional, interceptor configuration shared by all services
   107    filter: [String]
   108    # Required, the service list
   109    service:
   110      - # Optional, whether to prohibit inheriting the upstream timeout time, used to close the full link timeout mechanism, the default is false
   111        disable_request_timeout: Boolean
   112        # Optional, the IP address of the service monitors, if it is empty, it will try to get the network card IP, if it is still empty, use global.local_ip
   113        # To listen on all addresses, please use "0.0.0.0" (IPv4) or "::" (IPv6).
   114        ip: String(ipv4 or ipv6)
   115        # Required, the service name, used for service discovery
   116        name: String
   117        # Optional, the network card bound to the service, it will take effect only when the IP is empty
   118        nic: String
   119        # Optional, the port bound to the service, it is required when the address is empty
   120        port: Integer
   121        # Optional, the address that the service listens to, use ip:port when it is empty, and ignore ip:port when it is not empty
   122        address: String
   123        # Optional, network type, when it is empty, use server.network
   124        network: String(tcp, tcp4, tcp6, udp, udp4, udp6)
   125        # Optional, protocol type, when it is empty, use server.protocol
   126        protocol: String(trpc, grpc, http, etc.)
   127        # Optional, the timeout time for the service to process the request, in milliseconds
   128        timeout: Integer
   129        # Optional, long connection idle time, in milliseconds
   130        idletime: Integer
   131        # Optional, which regitration center to use such as polaris
   132        registry: String
   133        # Optional, list of interceptors, lower priority than server.filter
   134        filter: [String]
   135        # Optional, the TLS private key that the server needs to provide, when both tls_key and tls_cert are not empty, the TLS service will be enabled
   136        tls_key: String
   137        # Optional, the TLS public key that the server needs to provide, when both tls_key and tls_cert are not empty, the TLS service will be enabled
   138        tls_cert: String
   139        # Optional, if you enable reverse authentication, you need to provide the CA certificate of the client
   140        ca_cert: String
   141        # Optional, whether to enable asynchronous processing in the server, the default is true
   142        server_async: Boolean
   143        # Optional, when the service is in asynchronous processing mode, the maximum number of coroutines limited, if not set or <=0, use the default value: 1<<31 - 1. Asynchronous mode takes effect, synchronous mode does not take effect
   144        max_routines: Integer
   145        # Optional, enable the server to send packets in batches (writev system call), the default is false
   146        writev: Boolean
   147    # Optional, management functions frequently used by the service
   148    admin:
   149      # Optional, the IP bound by admin, the default is localhost
   150      ip: String
   151      # Optional, network card name, when the IP field is empty, it will try to get the IP from the network card
   152      nic: String
   153      # Optional, the port bound by admin, if it is 0, which is the default value, the admin function will not be enabled
   154      port: Integer
   155      # Optional, read timeout time, the unit is ms, the default is 3000ms
   156      read_timeout: Integer
   157      # Optional, write timeout time, the unit is ms, the default is 3000ms
   158      write_timeout: Integer
   159      # Optional, whether to enable TLS, currently not supported, setting it to true will directly report an error
   160      enable_tls: Boolean
   161  
   162  # Client configuration
   163  client:
   164    # Optional, if it is empty, use global.namespace
   165    namespace: String
   166    # Optional, network type, when the service is not configured with network, this field shall prevail
   167    network: String(tcp, tcp4, tcp6, udp, udp4, udp6)
   168    # Optional, protocol type, when the service is not configured with protocol, this field shall prevail
   169    protocol: String(trpc, grpc, http, etc.)
   170    # Optional, interceptor configuration shared by all services
   171    filter: [String]
   172    # Optional, client timeout time, when the service is not configured with timeout, this field shall prevail, the unit is millisecond
   173    timeout: Integer
   174    # Optional, service discovery strategy, when the service is not configured with discovery, this field shall prevail
   175    discovery: String
   176    # Optional, load balancing strategy, when the service is not configured with loadbalance, this field shall prevail
   177    loadbalance: String
   178    # Optional, circuit breaker policy, when the service is not configured with circuitbreaker, this field shall prevail
   179    circuitbreaker: String
   180    # Required, list of called services
   181    service:
   182      - # Callee service name
   183        # If pb is used, the callee must be be consistent with the service name defined in pb
   184        # Fill in at least one name and callee, if it is empty, use the name field
   185        callee: String
   186        # callee service name, Commonly used for service discovery
   187        # Fill in at least one name and callee, if it is empty, use the callee field
   188        name: String
   189        # Optional, environment name, used for service routing
   190        env_name: String
   191        # Optional, set name, used for service routing
   192        set_name: String
   193        # Optional, whether to disable service routing, the default is false
   194        disable_servicerouter: Boolean
   195        # Optional, when empty, use client.namespace
   196        namespace: String
   197        # Optional, target service, when not empty, the selector will take the information in target as the standard
   198        target: String(type:endpoint[,endpoint...])
   199        # Optional, the password of the callee service
   200        password: String
   201        # Optional, the service discovery strategy
   202        discovery: String
   203        # Optional, the load balancing strategy
   204        loadbalance: String
   205        # Optional, the circuit breaker strategy
   206        circuitbreaker: String
   207        # Optional, network type, when it is empty, use client.network
   208        network: String(tcp, tcp4, tcp6, udp, udp4, udp6)
   209        # Optional, timeout time, when it is empty, use client.timeout, the unit is millisecond
   210        timeout: Integer
   211        # Optional, protocol type, when it is empty, use client.protocol
   212        protocol: String(trpc, grpc, http, etc.)
   213        # Optional, serialization protocol, the default is -1, which is without setting
   214        serialization: Integer(0=pb, 1=JCE, 2=json, 3=flat_buffer, 4=bytes_flow)
   215        # Optional, compression protocol, the default is 0, which is no compression
   216        compression: Integer(0=no_compression, 1=gzip, 2=snappy, 3=zlib)
   217        # Optional, client private key, must be used with tls_cert
   218        tls_key: String
   219        # Optional, client public key, must be used with tls_key
   220        tls_cert: String
   221        # Optional, the server CA certificate path, when it is none, skip the authentication of the server
   222        ca_cert: String
   223        # Optional, service name when verifying TLS
   224        tls_server_name: String
   225        # Optional, list of interceptors, lower priority than client.filter
   226        filter: [String]
   227  # Plugin configuration
   228  plugins:
   229    # Plugin type
   230    ${type}:
   231      # Plugin name
   232      ${name}:
   233        # Plugin detailed configuration, please refer to the description of each plugin for details
   234        Object
   235  ```
   236  
   237  ## Create Configuration
   238  
   239  We have introduced that the startup of the program initializes the framework by reading the framework configuration file. So how to generate framework configuration file? This section introduces the following three common methods.
   240  
   241  ### Create Configurations through Tools
   242  
   243  The framework configuration file can be automatically generated by [trpc-cmdline](https://github.com/trpc-group/trpc-cmdline) tool. The services defined in the PB file are automatically added to the configuration file. 
   244  
   245  ```shell
   246  # generate the stub code and the framework configuration file "trpc_go.yaml" through PB file
   247  trpc create -p helloworld.proto
   248  ```
   249  
   250  It should be emphasized that the configuration generated by the tool is only a template configuration, and users need to modify the configuration content according to their own needs.
   251  
   252  ### Create Configurations through the Operation Platform
   253  
   254  For large complex systems, the best practice is to manage the framework configuration files uniformly through the service operation platform, and the platform will generate the framework configuration files uniformly and deliver them to the machines where the program will run.
   255  
   256  ### Use Environment Variables to Substitute the Configurations
   257  
   258  tRPC-Go also provides the golang template to generate framework configuration: it supports automatic replacement of framework configuration placeholders by reading environment variables. Create a configuration file template through tools or operating platforms, and then replace the environment variable placeholders in the configuration file with environment variables.
   259  
   260  For the use of environment variables, first use `${var}` to represent variable parameters in the configuration file, such as:
   261  
   262  ```yaml
   263  server:
   264    app: ${app}
   265    server: ${server}
   266    service: 
   267      - name: trpc.test.helloworld.Greeter
   268        ip: ${ip}
   269        port: ${port}
   270  ```
   271  
   272  When the framework starts, it will first read the text content of the configuration file `trpc_go.yaml`. When the placeholder is recognized, the framework will automatically read the corresponding value from the environment variable.
   273  
   274  As shown in the above configuration content, the environment variables need to be pre-set with the following data:
   275  
   276  ```shell
   277  export app=test
   278  export server=helloworld
   279  export ip=1.1.1.1
   280  export port=8888
   281  ```
   282  
   283  Since the framework configuration will parse the `$` symbol, when configuring the user, do not include the `$` character except for placeholders, such as passwords such as redis/mysql, do not include `$`.
   284  
   285  
   286  ## Example
   287  
   288  [https://github.com/trpc-group/trpc-go/blob/main/testdata/trpc_go.yaml](https://github.com/trpc-group/trpc-go/blob/main/testdata/trpc_go.yaml)