This article focuses on the spring Cloud Consul component, a tool that provides service discovery and configuration. The consul is distributed, highly available, and highly scalable.
I. Introduction of Consul
Consul has the following properties:
- Service discovery: Consul registers the service via HTTP, and the service and service are mutually sensed.
- Service Health Monitoring
- Key/value Storage
- Multi-Data center
The consul can be run on Mac Windows Linux and other machines.
Second, 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
Third, construction project
Build a Consul-miya springboot project, import dependent Pring-cloud-starter-consul-discovery, and its dependent files:
<?xml version= "1.0" encoding= "UTF-8"?><Projectxmlns="http://maven.apache.org/POM/4.0.0"Xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" ><modelversion>4.0.0</Modelversion><Groupid>com.forezp</Groupid><Artifactid>consul-miya</Artifactid><Version>0.0.1-snapshot</Version><Packaging>jar</Packaging><Name>consul-miya</Name><Description>demo Project for Spring Boot</Description><Parent><Groupid>org.springframework.boot</Groupid><Artifactid>spring-boot-starter-parent</Artifactid><Version>1.5.2.release</Version><Relativepath/><!--lookup parent from repository--</Parent><Properties><Project.build.sourceencoding>utf-8</Project.build.sourceencoding><Project.reporting.outputencoding>utf-8</Project.reporting.outputencoding><java.version>1.8</Java.version></Properties><Dependencies><Dependency><Groupid>org.springframework.cloud</Groupid><Artifactid>spring-cloud-starter-consul-discovery</Artifactid></Dependency><Dependency><Groupid>org.springframework.boot</Groupid><Artifactid>spring-boot-starter-web</Artifactid></Dependency><Dependency><Groupid>org.springframework.boot</Groupid><Artifactid>spring-boot-starter-test</Artifactid><Scope>test</Scope></Dependency></Dependencies><Dependencymanagement><Dependencies><Dependency><Groupid>org.springframework.cloud</Groupid><Artifactid>spring-cloud-dependencies</Artifactid><Version>dalston.release</Version><Type>pom</Type><Scope>import</Scope></Dependency></Dependencies></dependencymanagement> <build> <plugins> <plugin> < groupid>org.springframework.boot</ groupid> <artifactId> Spring-boot-maven-plugin</artifactid> </plugin> </ plugins> </build></PROJECT>
In its entry file consulmiyaapplication add annotation @enablediscoveryclient to open service discovery:
@SpringBootApplication @EnableDiscoveryClient @RestController public class consulmiyaapplication { @RequestMapping ("/hi ") public string home () {return "Hi, i ' m Miya";} public static void Main (string[] args) {new Springapplicationbuilder (Consulmiyaapplication.class). Web ( Span class= "Hljs-keyword" >true). Run (args); }}
The port of the consul service specified in its profile application.yml is 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 Consul-miya is registered.
Springcloud Learning and Growth 14 service registration (consul)