Java-Spring-Eureka的简单使用

首先,你需要一个springboot工程。

包依赖:

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

开启eureka,添加@EnableEurekaServer

@EnableEurekaServer
@SpringBootApplication
public class DcpEurekaApplication {

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

}

编写配置文件

server:
  port: 1200 #服务注册中心端口号
eureka:
  instance:
    hostname: 127.0.0.1 #服务注册中心IP地址
  client:
    registerWithEureka: false #是否向服务注册中心注册自己
    fetchRegistry: false #是否检索服务
    serviceUrl: #服务注册中心的配置内容,指定服务注册中心的位置
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

启动它,就可以进入到eureka的控制中心查看了。

接下来,把我们的服务注册到eureka上。

包依赖:

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

并同样在服务打上@EnableEurekaClient

编写bootstrap.yml文件。

spring : 
  application : 
    name : ly-bd-data-collection-svc #注册到Eureka的名字
eureka:
   instance:
     prefer-ip-address: true #设置微服务调用地址为IP优先(否则为false,调用服务名优先)
     # 设为否就可以通过eureka.instance.ip-address指定IP地址
     lease-renewal-interval-in-seconds: 30 # 心跳时间,即服务续约间隔时间(缺省为30s)
     lease-expiration-duration-in-seconds: 90 # 发呆时间,即服务续约到期时间(缺省为90s)
   client:
     service-url:
       defaultZone: ${defaultZone:http://${discovery:discovery}:1200/eureka/}
     register-with-eureka: false
     fetch-registry: false

另外,如果使用docker容器进行微服务部署,必须为各个容器搭建网桥通信,不然是发布不上去的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值