springcloud系列4——创建一个Eureka Server

创建一个Eureka Server

创建一个子模块microserver-discovery-eureka,该模块属于microserver-spring-cloud(看第一篇)。

pom.xml

<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">
    <parent>
        <artifactId>microserver-spring-cloud</artifactId>
        <groupId>com.tommy.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.tommy.springcloud</groupId>
    <artifactId>microservice-discovery-eureka</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>microservice-discovery-eureka</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
    </dependencies>
</project>


注意:microserver-spring-cloud的pom.xml中增加springcloud依赖:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Edgware.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

应用启动类

package com.tommy.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
 * Hello world!
 *
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication
{
    /**
     * 注意:
     * 1.启动之后访问的地址是:http://localhost:8761/,不是http://localhost:8761/eureka。
     * 2.注意父pom.xml中spring-cloud-dependencies的版本,可能导致子工程中找不到对应版本。这里使用的版本Edgware.SR1是Ok的。
     * @param args
     */
    public static void main( String[] args )
    {
        SpringApplication.run(EurekaApplication.class,args);
    }
}

通过@EnableEurekaServer注解启动一个服务注册中心

application.yml

server:
  port: 8761
eureka:
  client:
    # 表示是否将自己注册到Eureka Server,默认为true.由于当前应用就是Eureka Server,故而设置为false.
    register-with-eureka: false
    # 表示是否从Eureka Server获取注册信息,默认为true.因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,这里设置为false
    fetch-registry: false
    service-url:
      # 设置Eureka Server的地址,查询服务和注册服务都需要依赖这个地址.默认是http://localhost:8761/eureka/;多个地址可使用','风格.
      defaultZone: http://localhost:8761/eureka

运行EurekaApplication,在浏览器输入http://localhost:8761/,可以看到下面的页面,还没有任何服务。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值