php consul 服务注册,SpringCloud教程之服务注册(consul)

这篇文章主要介绍 spring cloud consul 组件,它是一个提供服务发现和配置的工具。consul具有分布式、高可用、高扩展性。

一、consul 简介

consul 具有以下性质:服务发现:consul通过http 方式注册服务,并且服务与服务之间相互感应。

服务健康监测

key/value 存储

多数据中心

consul可运行在mac windows linux 等机器上。

二、consul安装

linux$ 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下安装

三、构建工程

构建一个consul-miya的springboot工程,导入依赖pring-cloud-starter-consul-discovery,其依赖文件:<?xml version="1.0" encoding="UTF-8"?>

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

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

在其入口文件ConsulMiyaApplication加入注解@EnableDiscoveryClient,开启服务发现:@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: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

启动工程,访问localhost:8500,可以发现consul-miya被注册了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值