trpc.group/trpc-go/trpc-go@v1.0.2/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 ip: String(ipv4 or ipv6) 114 # Required, the service name, used for service discovery 115 name: String 116 # Optional, the network card bound to the service, it will take effect only when the IP is empty 117 nic: String 118 # Optional, the port bound to the service, it is required when the address is empty 119 port: Integer 120 # Optional, the address that the service listens to, use ip:port when it is empty, and ignore ip:port when it is not empty 121 address: String 122 # Optional, network type, when it is empty, use server.network 123 network: String(tcp, tcp4, tcp6, udp, udp4, udp6) 124 # Optional, protocol type, when it is empty, use server.protocol 125 protocol: String(trpc, grpc, http, etc.) 126 # Optional, the timeout time for the service to process the request, in milliseconds 127 timeout: Integer 128 # Optional, long connection idle time, in milliseconds 129 idletime: Integer 130 # Optional, which regitration center to use such as polaris 131 registry: String 132 # Optional, list of interceptors, lower priority than server.filter 133 filter: [String] 134 # 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 135 tls_key: String 136 # 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 137 tls_cert: String 138 # Optional, if you enable reverse authentication, you need to provide the CA certificate of the client 139 ca_cert: String 140 # Optional, whether to enable asynchronous processing in the server, the default is true 141 server_async: Boolean 142 # 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 143 max_routines: Integer 144 # Optional, enable the server to send packets in batches (writev system call), the default is false 145 writev: Boolean 146 # Optional, management functions frequently used by the service 147 admin: 148 # Optional, the IP bound by admin, the default is localhost 149 ip: String 150 # Optional, network card name, when the IP field is empty, it will try to get the IP from the network card 151 nic: String 152 # Optional, the port bound by admin, if it is 0, which is the default value, the admin function will not be enabled 153 port: Integer 154 # Optional, read timeout time, the unit is ms, the default is 3000ms 155 read_timeout: Integer 156 # Optional, write timeout time, the unit is ms, the default is 3000ms 157 write_timeout: Integer 158 # Optional, whether to enable TLS, currently not supported, setting it to true will directly report an error 159 enable_tls: Boolean 160 161 # Client configuration 162 client: 163 # Optional, if it is empty, use global.namespace 164 namespace: String 165 # Optional, network type, when the service is not configured with network, this field shall prevail 166 network: String(tcp, tcp4, tcp6, udp, udp4, udp6) 167 # Optional, protocol type, when the service is not configured with protocol, this field shall prevail 168 protocol: String(trpc, grpc, http, etc.) 169 # Optional, interceptor configuration shared by all services 170 filter: [String] 171 # Optional, client timeout time, when the service is not configured with timeout, this field shall prevail, the unit is millisecond 172 timeout: Integer 173 # Optional, service discovery strategy, when the service is not configured with discovery, this field shall prevail 174 discovery: String 175 # Optional, load balancing strategy, when the service is not configured with loadbalance, this field shall prevail 176 loadbalance: String 177 # Optional, circuit breaker policy, when the service is not configured with circuitbreaker, this field shall prevail 178 circuitbreaker: String 179 # Required, list of called services 180 service: 181 - # Callee service name 182 # If pb is used, the callee must be be consistent with the service name defined in pb 183 # Fill in at least one name and callee, if it is empty, use the name field 184 callee: String 185 # callee service name, Commonly used for service discovery 186 # Fill in at least one name and callee, if it is empty, use the callee field 187 name: String 188 # Optional, environment name, used for service routing 189 env_name: String 190 # Optional, set name, used for service routing 191 set_name: String 192 # Optional, whether to disable service routing, the default is false 193 disable_servicerouter: Boolean 194 # Optional, when empty, use client.namespace 195 namespace: String 196 # Optional, target service, when not empty, the selector will take the information in target as the standard 197 target: String(type:endpoint[,endpoint...]) 198 # Optional, the password of the callee service 199 password: String 200 # Optional, the service discovery strategy 201 discovery: String 202 # Optional, the load balancing strategy 203 loadbalance: String 204 # Optional, the circuit breaker strategy 205 circuitbreaker: String 206 # Optional, network type, when it is empty, use client.network 207 network: String(tcp, tcp4, tcp6, udp, udp4, udp6) 208 # Optional, timeout time, when it is empty, use client.timeout, the unit is millisecond 209 timeout: Integer 210 # Optional, protocol type, when it is empty, use client.protocol 211 protocol: String(trpc, grpc, http, etc.) 212 # Optional, serialization protocol, the default is -1, which is without setting 213 serialization: Integer(0=pb, 1=JCE, 2=json, 3=flat_buffer, 4=bytes_flow) 214 # Optional, compression protocol, the default is 0, which is no compression 215 compression: Integer(0=no_compression, 1=gzip, 2=snappy, 3=zlib) 216 # Optional, client private key, must be used with tls_cert 217 tls_key: String 218 # Optional, client public key, must be used with tls_key 219 tls_cert: String 220 # Optional, the server CA certificate path, when it is none, skip the authentication of the server 221 ca_cert: String 222 # Optional, service name when verifying TLS 223 tls_server_name: String 224 # Optional, list of interceptors, lower priority than client.filter 225 filter: [String] 226 # Plugin configuration 227 plugins: 228 # Plugin type 229 ${type}: 230 # Plugin name 231 ${name}: 232 # Plugin detailed configuration, please refer to the description of each plugin for details 233 Object 234 ``` 235 236 ## Create Configuration 237 238 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. 239 240 ### Create Configurations through Tools 241 242 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. 243 244 ```shell 245 # generate the stub code and the framework configuration file "trpc_go.yaml" through PB file 246 trpc create -p helloworld.proto 247 ``` 248 249 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. 250 251 ### Create Configurations through the Operation Platform 252 253 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. 254 255 ### Use Environment Variables to Substitute the Configurations 256 257 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. 258 259 For the use of environment variables, first use `${var}` to represent variable parameters in the configuration file, such as: 260 261 ```yaml 262 server: 263 app: ${app} 264 server: ${server} 265 service: 266 - name: trpc.test.helloworld.Greeter 267 ip: ${ip} 268 port: ${port} 269 ``` 270 271 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. 272 273 As shown in the above configuration content, the environment variables need to be pre-set with the following data: 274 275 ```shell 276 export app=test 277 export server=helloworld 278 export ip=1.1.1.1 279 export port=8888 280 ``` 281 282 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 `$`. 283 284 285 ## Example 286 287 [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)