spring-cloud eureka注册发现

 idea新建一个eureka server服务

application.yml 配置:

spring:
  application:
    name: eureka-server

server:
  port: 7000

eureka:
  instance:
    hostname: localhost
  server:
    response-cache-update-interval-ms: 3000
    enable-self-preservation: false #设为false,关闭自我保护主要
    eviction-interval-timer-in-ms: 3000 #清理间隔(单位毫秒,默认是60*1000)
  client:
    #表示是否将自己注册到Eureka Server
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://localhost:${server.port}/eureka/

通过@EnableEurekaServer 开启

package com.example.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);
    }

}

启动服务,访问 localhost:7000,可以看到eureka界面如下,这时候没有服务注册在eureka:

新建一个eureka-discovery服务,注册在eureka server中

pom.xml中主要需要引入:

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

application.yml中配置如下:
server:
  port: 7001

spring:
  application:
    name: eureka-discovery

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7000/eureka/
  instance:
    lease-expiration-duration-in-seconds: 2
    #服务刷新时间配置,每隔这个时间会主动心跳一次
    #默认30s
    lease-renewal-interval-in-seconds: 1
    #将ip注册到eureka server上而不是机器主机名
    prefer-ip-address: true
    #ip-address: 127.0.0.1
    #InstanceId默认是${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}},
    #也就是:主机名:应用名:应用端口
    #通过instance-id 自定义ip+端口号
    instance-id: ${spring.cloud.client.ipaddress}:${server.port}

启动类EurekaDiscoveryApplication中配置@EnableEurekaClient

package com.example.eurekadiscovery;

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

@SpringBootApplication
@EnableEurekaClient
public class EurekaDiscoveryApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaDiscoveryApplication.class, args);
    }

}

启动eureka-discovery服务

再访问http://localhost:7000/,可以发现eureka-discovery已经注册在eureka server:

 

gitHub地址:https://github.com/gexiaoshan518/spring-cloud.git

 

欢迎扫码交流:

 



posted @ 2019-01-17 14:34  宅山仔  阅读(278)  评论(0编辑  收藏  举报