spring cloud eureka简单demo

1. eureka是啥?

是分布式系统的核心,服务发现机制的实现,有eureka-server跟eureka-client两部分。

2. 创建eureka-server项目

maven 项目依赖导入

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <dependencyManagement>
        <dependencies>
            <!--spring cloud 版本指定-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.BUILD-SNAPSHOT</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>   
    </dependencyManagement>  
    <!-- eureka-server jar-->
    <dependencies>
        <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
    <!--jar包仓库指定-->
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/libs-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>      

application.yml 配置

server:
  #应用端口
  port: 8761
eureka:
  client:
    #是否注册自身 默认true 注册 false 不注册
    register-with-eureka: false
    #是否检查服务清单 默认true 检查 false不检查
    fetch-registry: false
  instance:
    #主机域名
    hostname: localhost
  server:
    #是否开启自我保护 默认true 开启  false 不开启
    enable-self-preservation: false
    #服务失效扫描时间 单位毫秒
    eviction-interval-timer-in-ms: 5000

spring:
  application:
    #应用名称
    name: eureka-server

logging:
  #日志文件存储地址
  path: logs

Application.java

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

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

3. 创建 eureka-client 项目

maven 项目依赖导入

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <dependencyManagement>
        <dependencies>
            <!--spring cloud 版本指定-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.BUILD-SNAPSHOT</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>   
    </dependencyManagement>  
     <!-- eureka-client jar-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <!--jar包仓库指定-->
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/libs-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>      

application.yml 配置

spring:
  application:
    #应用名称
    name: eureka-client
server:
  #端口号
  port: 8081

eureka:
  client:
    service-url:
        #eureka-server 地址
        defaultZone: http://localhost:8761/eureka/

  instance:
    #服务标识
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    #服务续约到期时间 单位秒
    lease-expiration-duration-in-seconds: 10
    #服务续约间隔时间 单位秒
    lease-renewal-interval-in-seconds: 5

management:
  health:
    defaults:
      #开启健康检查
      enabled: true

Application.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

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

4. 测试

按顺序启动,eureka-server,eureka-client,访问http://localhost:8761
效果图如下:
参考界面

5. github 简单用例地址

https://github.com/wangdie0623/eureka-demo/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值