Springcloud Tutorials | 14th article: Service Registration (consul)

Source: Internet
Author: User

Copyright NOTICE: This article for Bo Master original article, welcome reprint, reprint Please indicate the author, original hyperlink, Bo main address: Http://blog.csdn.net/forezp. http://blog.csdn.net/forezp/article/details/70245644

Directory (?) [+]

Reprint please indicate the source:
http://blog.csdn.net/forezp/article/details/70245644
This article is from Fang Zhibong's blog

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
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

Install under Windows:
See Consul How to install under Windows

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>          
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

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); }} 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

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
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

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

Springcloud Tutorials | 14th article: Service Registration (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.