Service discovery is one of the key principles of micro-service architecture. Trying to configure each client or some form of convention can be very difficult and very fragile. Consul provides service discovery services through HTTP APIs and DNS. Spring Cloud Consul uses the HTTP API for service registration and discovery. This does not prevent non-spring cloud applications from taking advantage of the DNS interface. The consul proxy server runs in a cluster that communicates through the gossip protocol and uses the Raft protocol protocol.
How to activate
To activate Consul service discovery, use the initiator of the group Org.springframework.cloud and artifact ID spring-cloud-starter-consul-discovery. For more information about using the current Spring Cloud Publishing list settings to build your system, see the Spring Cloud project page.
Register Consul
When a client registers consul, it provides metadata about itself, such as host and Port, ID, name, and label. An HTTP check is created by default, and consul hits the/health endpoint every 10 seconds. If the health check fails, the service instance is marked as critical.
Example Consul client:
@SpringBootApplication@EnableDiscoveryClient@RestControllerpublic class Application { @RequestMapping("/") public String home() { return "Hello world"; } public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); }}
(That is, the fully normal spring boot application). If the consul client is located in a location other than localhost:8500, it needs to be configured to locate the client. Cases:
application.ymlspring: cloud: consul: host: localhost port: 8500
Warning
If you use spring Cloud Consul Config, the above values will need to be placed in bootstrap.yml instead of APPLICATION.YML.
The default service name from environment, the instance ID and port are ${spring.application.name},spring context ID and ${server.port}, respectively.
@EnableDiscoveryClient set the application to Consul "service" (that is, registering itself) and "client" (that is, you can query consul to find other services).
Spring Cloud consul-Service discovery and Consul