Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to implement Service Registration consul in Spring Cloud

Shulou Source: shulou.com Published: 2022-06-01 08:17:27 09月25日 Update

This article is about how to implement service registration consul in Spring Cloud. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

A brief introduction to consul

Consul has the following properties:

Service Discovery: consul registers services through http, and services and services interact with each other.

Service health monitoring

Key/value storage

Multiple data centers

Consul can run on machines such as mac windows linux.

II. Consul installation

Linux

$mkdir-p $GOPATH/src/github.com/hashicorp & & cd $! $git clone https://github.com/hashicorp/consul.git$ cd consul$ make bootstrap$ make bootstrap

Install under windows:

See how to install consul under windows

III. Construction project

Build a springboot project for consul-miya, and import the dependent pring-cloud-starter-consul-discovery, which depends on the file:

4.0.0 com.forezp consul-miya 0.0.1-SNAPSHOT jar consul-miya Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.2.RELEASE UTF-8 UTF-8 1.8 org.springframework.cloud spring-cloud-starter-consul -discovery org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies Dalston.RELEASE Pom import org.springframework.boot spring-boot-maven-plugin

Add the annotation @ EnableDiscoveryClient to its entry file ConsulMiyaApplication to enable service discovery:

@ SpringBootApplication@EnableDiscoveryClient@RestControllerpublic class ConsulMiyaApplication {@ RequestMapping ("/ hi") public String home () {return "hi, IBM miya";} public static void main (String [] args) {new SpringApplicationBuilder (ConsulMiyaApplication.class) .web (true) .run (args);}}

Specify the port of the consul service in its configuration file application.yml as 8500:

Spring: cloud: consul: host: localhost port: 8500 discovery: healthCheckPath: ${management.contextPath} / health healthCheckInterval: 15s instance-id: consul-miya application: name: consul-miyaserver: port: 8502

Start the project, visit localhost:8500, and you can find that consul-miya is registered.

Thank you for reading! This is the end of the article on "how to implement service registration consul in Spring Cloud". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Tags: Services engineering documents content more articles UTF-8 good practical healthy between portals properties data centers articles methods machines notes check it out Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi macOS MariaDB Linux NVidia