基于Spring Boot 2.0.2.RELEASE 的 Spring Cloud 速成指南 | 二. Spring Cloud 服务注册中心(Eureka Server)

        服务注册中心(Eureka Server)是整个Spring Cloud项目中的核心模块,作为其他模块之间沟通的桥梁

创建服务注册中心

   右键spring-cloud-parent->New->Module->Spring Initializr->Next


然后Next


勾选Eureka Server    Next->Finish 

结构如下:


修改spring-cloud-parent的pom.xml文件

添加

    <modules>
        <module>eureka-server</module>
    </modules>

修改eureka-server的pom.xml文件

parent改为

    <parent>
        <groupId>com.hongot</groupId>
        <artifactId>spring-cloud-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

删掉和spring-cloud-parent的pom.xml文件重复的内容

修改后的pom.xml

<?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.hongot</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.hongot</groupId>
        <artifactId>spring-cloud-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

</project>

只需在eureka-server的启动application上加@EnableEurekaServer,运行项目时即可启动服务注册中心

package com.hongot.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

把resources下的application.properties改为application.yml

在其中添加如下内容

server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url: 
        default-zone: http://${eureka.instance.hostname}:${server.port}/eureka/

eureka server在默认的情况下也是一个eureka client

register-with-eureka: 为false意味着自身仅作为服务器,不作为客户端; 

fetch-registry: 为false意味着无需注册自身。

启动eureka-server,打开浏览器访问http://localhost:8761 ,界面如下:


源码下载:点击打开链接

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值