当前位置:   article > 正文

eureka配置快速入门_初学 配置euraka

初学 配置euraka

创建一个maven项目

在这里插入图片描述

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

eureka配置信息

根据个人需求,配置端口号和hostname,如果hostname写域名,在系统文件hosts 里做好相关配置。本项目直接写ip,不需要配置。
文件路径
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/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

启动类

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);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

启动服务,访问http://127.0.0.1:7001/
成功显示页面如下:
在这里插入图片描述

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

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

application.properties

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

3.启动类添加注解
@EnableEurekaClient

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

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读