java 实现 注册中心_Spring Cloud(1)——服务注册中心

本文介绍了如何使用Java和Spring Cloud搭建Eureka服务注册中心。通过创建Maven工程,添加相关依赖,创建启动类并配置application.yml,实现了Eureka Server的启动。启动后,可以在8761端口访问,但目前注册中心尚未有服务注册。

一、简介

Eureka是一个云端服务发现,一个基于 REST 的服务,用于定位服务,以实现云端中间层服务发现和故障转移。用它我们可以实现服务注册与发现功能

二、项目实例

1、创建Maven工程microservice-eureka-server,并在pom.xml中加入以下依赖包

org.springframework.boot

spring-boot-starter-parent

1.4.4.RELEASE

org.springframework.boot

spring-boot-starter-test

test

org.springframework.cloud

spring-cloud-starter-eureka-server

org.springframework.cloud

spring-cloud-dependencies

Camden.SR5

pom

import

2、创建应用启动类EurekaServerApplication.java

package com.baibei.eureka;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**

* @author: 会跳舞的机器人

* @email:2268549298@qq.com

* @date: 17/2/15 下午5:39

* @description:启动主类

*/

@SpringBootApplication

@EnableEurekaServer

public class EurekaServerApplication {

public static void main(String[] args) {

SpringApplication.run(EurekaServerApplication.class, args);

}

}

使用@EnableEurekaServer注解开启服务注册功能

3、配置application.yml

在resource目录下创建application.yml文件,内容如下:

spring:

application:

name: microservice-eureka-server

server:

port: 8761

eureka:

instance:

hostname: eureka-server

client:

registerWithEureka: false

fetchRegistry: false

serviceUrl:

defaultZone: http://eureka-server:8761/eureka/

4、启动运行

运行EurekaServerApplication的main方法,启动成功后,访问:http://localhost:8761/

2ec09d0f947a

Paste_Image.png

从图中我们可以看出,还没有任何服务注册至注册中心。

附项目目录截图:

2ec09d0f947a

Paste_Image.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值