企業分佈式微服務雲SpringCloud SpringBoot mybatis (十四)服務註冊(consul)

這篇文章主要介紹 spring cloud consul 組件,它是一個提供服務發現和配置的工具。consul具備分佈式、高可用、高擴展性。java

1、consul 簡介

consul 具備如下性質:linux

  • 服務發現:consul經過http 方式註冊服務,而且服務與服務之間相互感應。
  • 服務健康監測
  • key/value 存儲
  • 多數據中心

consul可運行在mac windows linux 等機器上。git

2、consul安裝

linuxgithub

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

windows下安裝: 
見consul怎麼在windows下安裝web

3、構建工程

構建一個consul-miya的springboot工程,導入依賴pring-cloud-starter-consul-discovery,其依賴文件:spring

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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><br>

在其入口文件ConsulMiyaApplication加入註解@EnableDiscoveryClient,開啓服務發現:apache

@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(true).run(args);
    }
}

在其配置文件application.yml指定consul服務的端口爲8500:bootstrap

spring:
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        healthCheckPath: ${management.contextPath}/health
        healthCheckInterval: 15s
        instance-id: consul-miya
  application:
    name: consul-miya
server:
  port: 8502<br>

啓動工程,訪問localhost:8500,能夠發現consul-miya被註冊了。windows

架構代碼以下:springboot

"分佈式b2b <wbr

Spring Cloud大型企業分佈式微服務雲架構源碼請加企鵝求求:一七九一七四三三八零

相關文章
相關標籤/搜索