key / value effect
- Dynamically modify the configuration file
- Support Services Collaborative
- Established leader election
- Provide service discovery
- Integrated health check
In addition to providing comprehensive health service discovery and inspection, Consul also provides an easy to use key / value store. This can be used to store dynamic configuration, help coordinate services, establish leadership election, and anything else that enable other developers may want to build.
There are two methods you can use: through HTTP API and through CLI API.
First, the operator using the CLI API key / value
1, consul kv get inquiries
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv get redis/config/minconns Error! No key exists at: redis/config/minconns
You will see no results are returned, because the KV storage is not the key to return an error, then we will insert or "put" a value to KV store.
2, consul kv put increasing key / value
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv put redis/config/minconns 1 Success! Data written to: redis/config/minconns
Now query the key again you will see the following results:
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv get redis/config/minconns 1
Consul retain the additional metadata in the field, you can use the -detailed flag to retrieve detailed information:
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv get -detailed redis/config/minconns CreateIndex 74 Flags 0 Key redis/config/minconns LockIndex 0 ModifyIndex 74 Session - Value 1
On the web UI can see the key used to create the CLI API
Create a "duan" in the web UI key:
Through the CLI API query results:
设置值的时候,还可以使用-flags标志
- -flags=<uint>
Unsigned integer value to assign to this key-value pair. This value is not read by Consul, so clients can use this value however makes sense for their use case. The default value is 0 (no flags).
flags used to make the client a custom logo, consul does not use it, you can easily define your own program
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv put -flags=42 redis/config/users/admin abcd1234 Success! Data written to: redis/config/users/admin
Setting flag is 42, set to what you want to set anything. All keys are set to support a 64-bit integer value.
3, consul kv get -recurse list query
Use -recurse option to list all KV store keys, the results returned will be in alphabetical order.
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv get -recurse redis/config/minconns:1 redis/config/users/admin:abcd1234
4, consul kv delete delete
Use the delete command to delete stored KV specified key.
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv delete redis/config/minconns Success! Deleted key: redis/config/minconns
You can also use the recursive option recurse option to delete all the keys with a prefix:
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv delete -recurse redis Success! Deleted keys with prefix: redis
To update the value of an existing key, a new value can be put on the same path.
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv put foo bar Success! Data written to: foo D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv get foo bar D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv put foo zip Success! Data written to: foo
Consul atom can be used provided Check_And_Set update operation. -Cas mark required for the implementation of CAS operations. As for what is CAS, your own Baidu Bar
- -modify-index=<uint>
Unsigned Integer Representing at The ModifyIndex of IS at The Key This Used in Combination with Flag at The -CAS..
First read detailed information about the foo key of
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv get -detailed foo CreateIndex 131 Flags 0 Key foo LockIndex 0 ModifyIndex 133 Session - Value zip
See foo ModifyIndex index number is 133. CAS operation is then used to modify the way it
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv put -cas -modify-index=133 foo bar Success! Data written to: foo
Modified successfully, then check
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv get -detailed foo CreateIndex 131 Flags 0 Key foo LockIndex 0 ModifyIndex 141 Session - Value bar
ModifyIndex become a 141. Still use the above command to try to modify
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv put -cas -modify-index=133 foo bar Error! Did not write to foo: CAS failed
Failed. The first reason is the CAS operation is successful because the value ModifyIndex is 141, we enter are -modify-index = 133.
The second operation fails, ModifyIndex has become 141, and we also used -modify-index = 133, Check_And_SetS in Check this step has failed, will not be Set up.
Second, the use http API operation key / value
2.1 All Key / value http://127.0.0.1:8500/v1/kv/?recurse
Description:
- Use? Recurse parameter to specify view multiple KV
- No value --404
2.2, add key / value
Description: flags-- for any a KV add a meaningful metadata.
Note: The top of this is a problem, be sure to note the flags rather than flag.
2.3 to view a single key / value
Description: value is base64 encoded test (using base64 encoding is to allow the non-UTF-8 characters)
2.4, modify key / value
If the value is equal to the cas ModifyIndex, the amendment is successful, if not equal, then the modification failed.
2.5, delete key / value
2.5.1, delete single KV
2.5.2, (KV within the specified range prefix) to delete a range of KV
Description:
- Specifies to delete the prefix KV and K (zjg)
- Multiple operations must have? Recurse parameter
Third, the use of Consul key / value config server stores the alternative
Since consul comes kv storage, you can replace config server.
Proceed as follows:
First, the first add-dependent jar
//compile 'org.springframework.cloud:spring-cloud-starter-config' compile 'org.springframework.cloud:spring-cloud-starter-consul-config'
Before removing the config server dependent, dependent replaced consul-config can.
Second, modify bootstrap.yml file
1 spring: 2 ... 3 cloud: 4 consul: 5 host: 127.0.0.1 6 port: 8500 7 discovery: 8 tags: version=1.0,author=yjmyzz 9 healthCheckPath: /info.json 10 healthCheckInterval: 5s 11 instanceId: ${spring.application.name}:${spring.cloud.client.ipAddress} 12 enabled: true 13 config: 14 enabled: true 15 format: YAML 16 prefix: config 17 defaultContext: application 18 profileSeparator: ',' 19 data-key: data 20 # config: 21 # label: dev 22 # discovery: 23 # enabled: true 24 # service-id: my-config-server 25 # fail-fast: true 26 # retry: 27 # max-interval: 1500 28 # max-attempts: 5 29 # multiplier: 1.2
The key is the line 13-19, explain:
15 line format: YAML value represents the contents of the key-value consul is used YAML format
16 line prefix: config file consul expressed storing configuration on the root folder named config
17 line defaultContext: application represents the corresponding application configuration file name (eg: your service if you plan named myApp, where the application will be replaced myApp)
Line 18 profileSeparator: ',' indicates if there is a plurality of profile (eg: dev development environment, the test environment Test ...), between the key and the name of the profile DefaultContext, what is represented delimiter (here a bit confusing, will be explained in detail later)
19 line data-key: data represents the key value of the node name of the last layer, typically default data
Three, consul create kv node configuration
Many articles, including official documents speak of this step are unclear, the key is naming the name of the node to be the same as bootstrap.yml configuration, for example, we want to create a configuration test environment, key name can be taken as:
config/application,test/data
Here every part, every previous step bootstrap.yml the agreement referred to in the above figure 5 haircut, we combine the experience step explanation about 15-19 rows.
Value is then part of the value, according to the configuration content filling into yml line format:
tips: Usually the development, generally used consul dev mode, kv storage is not persistent store development mode, all-in memory (to restart the consul lost!), it is generally recommended yml profile content, stored in a separate project file, when you start debugging, configuration files directly to the content can be posted to the Value box.
Well, now you can try to start the next, well, then, should be on it, it is not very simple, the key save the config server deployment, to help the company save a machine, do not forget to let the leadership of you, oh plus performance ^ _ ^
If you want a way to code read / write KV store, you can use the following ways:
read:
curl http://localhost:8500/v1/kv/config/application,dev/data?raw=true
key / value effect
- Dynamically modify the configuration file
- Support Services Collaborative
- Established leader election
- Provide service discovery
- Integrated health check
In addition to providing comprehensive health service discovery and inspection, Consul also provides an easy to use key / value store. This can be used to store dynamic configuration, help coordinate services, establish leadership election, and anything else that enable other developers may want to build.
There are two methods you can use: through HTTP API and through CLI API.
First, the operator using the CLI API key / value
1, consul kv get inquiries
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv get redis/config/minconns Error! No key exists at: redis/config/minconns
You will see no results are returned, because the KV storage is not the key to return an error, then we will insert or "put" a value to KV store.
2, consul kv put increasing key / value
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv put redis/config/minconns 1 Success! Data written to: redis/config/minconns
Now query the key again you will see the following results:
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv get redis/config/minconns 1
Consul retain the additional metadata in the field, you can use the -detailed flag to retrieve detailed information:
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv get -detailed redis/config/minconns CreateIndex 74 Flags 0 Key redis/config/minconns LockIndex 0 ModifyIndex 74 Session - Value 1
On the web UI can see the key used to create the CLI API
Create a "duan" in the web UI key:
Through the CLI API query results:
设置值的时候,还可以使用-flags标志
- -flags=<uint>
Unsigned integer value to assign to this key-value pair. This value is not read by Consul, so clients can use this value however makes sense for their use case. The default value is 0 (no flags).
flags used to make the client a custom logo, consul does not use it, you can easily define your own program
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv put -flags=42 redis/config/users/admin abcd1234 Success! Data written to: redis/config/users/admin
Setting flag is 42, set to what you want to set anything. All keys are set to support a 64-bit integer value.
3, consul kv get -recurse list query
Use -recurse option to list all KV store keys, the results returned will be in alphabetical order.
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv get -recurse redis/config/minconns:1 redis/config/users/admin:abcd1234
4, consul kv delete delete
Use the delete command to delete stored KV specified key.
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv delete redis/config/minconns Success! Deleted key: redis/config/minconns
You can also use the recursive option recurse option to delete all the keys with a prefix:
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv delete -recurse redis Success! Deleted keys with prefix: redis
To update the value of an existing key, a new value can be put on the same path.
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv put foo bar Success! Data written to: foo D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv get foo bar D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv put foo zip Success! Data written to: foo
Consul atom can be used provided Check_And_Set update operation. -Cas mark required for the implementation of CAS operations. As for what is CAS, your own Baidu Bar
- -modify-index=<uint>
Unsigned Integer Representing at The ModifyIndex of IS at The Key This Used in Combination with Flag at The -CAS..
First read detailed information about the foo key of
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv get -detailed foo CreateIndex 131 Flags 0 Key foo LockIndex 0 ModifyIndex 133 Session - Value zip
See foo ModifyIndex index number is 133. CAS operation is then used to modify the way it
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv put -cas -modify-index=133 foo bar Success! Data written to: foo
Modified successfully, then check
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv get -detailed foo CreateIndex 131 Flags 0 Key foo LockIndex 0 ModifyIndex 141 Session - Value bar
ModifyIndex become a 141. Still use the above command to try to modify
D:\soft\worksoft\consul_1.0.6_windows_amd64>consul kv put -cas -modify-index=133 foo bar Error! Did not write to foo: CAS failed
Failed. The first reason is the CAS operation is successful because the value ModifyIndex is 141, we enter are -modify-index = 133.
The second operation fails, ModifyIndex has become 141, and we also used -modify-index = 133, Check_And_SetS in Check this step has failed, will not be Set up.
Second, the use http API operation key / value
2.1 All Key / value http://127.0.0.1:8500/v1/kv/?recurse
Description:
- Use? Recurse parameter to specify view multiple KV
- No value --404
2.2, add key / value
Description: flags-- for any a KV add a meaningful metadata.
Note: The top of this is a problem, be sure to note the flags rather than flag.
2.3 to view a single key / value
Description: value is base64 encoded test (using base64 encoding is to allow the non-UTF-8 characters)
2.4, modify key / value
If the value is equal to the cas ModifyIndex, the amendment is successful, if not equal, then the modification failed.
2.5, delete key / value
2.5.1, delete single KV
2.5.2, (KV within the specified range prefix) to delete a range of KV
Description:
- Specifies to delete the prefix KV and K (zjg)
- Multiple operations must have? Recurse parameter
Third, the use of Consul key / value config server stores the alternative
Since consul comes kv storage, you can replace config server.
Proceed as follows:
First, the first add-dependent jar
//compile 'org.springframework.cloud:spring-cloud-starter-config' compile 'org.springframework.cloud:spring-cloud-starter-consul-config'
Before removing the config server dependent, dependent replaced consul-config can.
Second, modify bootstrap.yml file
1 spring: 2 ... 3 cloud: 4 consul: 5 host: 127.0.0.1 6 port: 8500 7 discovery: 8 tags: version=1.0,author=yjmyzz 9 healthCheckPath: /info.json 10 healthCheckInterval: 5s 11 instanceId: ${spring.application.name}:${spring.cloud.client.ipAddress} 12 enabled: true 13 config: 14 enabled: true 15 format: YAML 16 prefix: config 17 defaultContext: application 18 profileSeparator: ',' 19 data-key: data 20 # config: 21 # label: dev 22 # discovery: 23 # enabled: true 24 # service-id: my-config-server 25 # fail-fast: true 26 # retry: 27 # max-interval: 1500 28 # max-attempts: 5 29 # multiplier: 1.2
The key is the line 13-19, explain:
15 line format: YAML value represents the contents of the key-value consul is used YAML format
16 line prefix: config file consul expressed storing configuration on the root folder named config
17 line defaultContext: application represents the corresponding application configuration file name (eg: your service if you plan named myApp, where the application will be replaced myApp)
Line 18 profileSeparator: ',' indicates if there is a plurality of profile (eg: dev development environment, the test environment Test ...), between the key and the name of the profile DefaultContext, what is represented delimiter (here a bit confusing, will be explained in detail later)
19 line data-key: data represents the key value of the node name of the last layer, typically default data
Three, consul create kv node configuration
Many articles, including official documents speak of this step are unclear, the key is naming the name of the node to be the same as bootstrap.yml configuration, for example, we want to create a configuration test environment, key name can be taken as:
config/application,test/data
Here every part, every previous step bootstrap.yml the agreement referred to in the above figure 5 haircut, we combine the experience step explanation about 15-19 rows.
Value is then part of the value, according to the configuration content filling into yml line format:
tips: Usually the development, generally used consul dev mode, kv storage is not persistent store development mode, all-in memory (to restart the consul lost!), it is generally recommended yml profile content, stored in a separate project file, when you start debugging, configuration files directly to the content can be posted to the Value box.
Well, now you can try to start the next, well, then, should be on it, it is not very simple, the key save the config server deployment, to help the company save a machine, do not forget to let the leadership of you, oh plus performance ^ _ ^
If you want a way to code read / write KV store, you can use the following ways:
read:
curl http://localhost:8500/v1/kv/config/application,dev/data?raw=true