创建一个maven项目

eureka配置快速入门

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.cyy</groupId>
    <artifactId>eurekaService</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <!--增加eureka-server的依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>

    </dependencies>

    <!--依赖管理,用于管理spring-cloud的依赖-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <!--使用该插件打包-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

eureka配置信息

根据个人需求,配置端口号和hostname,如果hostname写域名,在系统文件hosts 里做好相关配置。本项目直接写ip,不需要配置。
eureka配置快速入门
application.yml

server:
  port: 7001

eureka:
  instance:                   #定义Eureka实例
    hostname: 127.0.0.1 #Eureka实例所在的主机名
    #eureka默认情况下,把自己当做客户端来注册自己,所以我们要禁用它
    client:
    register-with-eureka: false #表示是否将自己注册到Eureka Server上,默认为true
    fetch-registry: false       #表示是否从Eureka Server上获取注册信息,默认为true

serviceUrl:
        defaultZone: http://127.0.0.1:7001/eureka/

启动类

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

/**
 *
 * *@description:启动类
 */

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

}

启动服务,访问http://127.0.0.1:7001/
成功显示页面如下:
eureka配置快速入门

在其他需要注册到eureka的服务里的配置:

1.导入spring-cloud-starter-eureka-server依赖
2.配置文件添加

application.properties

eureka.client.serviceUrl.defaultZone=http://127.0.0.1:7001/eureka

3.启动类添加注解
@EnableEurekaClient

成功启动后,服务便注册到eureka里

相关文章: