首先,你需要一个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容器进行微服务部署,必须为各个容器搭建网桥通信,不然是发布不上去的。