Spring Cloud consul-Service discovery and Consul

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.