3. 服务注册与发现之server springcloud-eureka-server

1. 创建空的子的项目 springcloud-eureka-server

2. 修改 pom.xml

增加

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

增加后的 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">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.xnx3.springcloud</groupId>
    <artifactId>springcloud-main</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <groupId>com.xnx3.springcloud.eurekaserver</groupId>
  <artifactId>springcloud-eureka-server</artifactId>
  
  <!-- 以下是增加的 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  <!-- 以上是增加的 -->
  
</project>

3. 添加启动类 com.xnx3.springcloud.eureka.EurekaApplication

package com.xnx3.springcloud.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}

4. 添加配置文件 application.properties

src/main/resources/ 下增加配置文件 application.properties

server.port=8081

eureka.instance.hostname=localhost
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl=defaultZone\: http\://${eureka.instance.hostname}\:${server.port}/eureka/

5. 启动

访问 localhost:8081

输入图片说明

完毕!

猜你喜欢

转载自my.oschina.net/u/698996/blog/1800170