第一步:采用idea创建父项目:
第二步:输入相应的名称
第三步:点击finish 完成后,因为这个只是父项目,所以可以把src目录删除掉
第四步:修改maven的相关地址及配置
第五步:创建eureka服务module
第六步:点击finish,等待maven去下载相应的jar包,jar依赖下载完后添加@EnableEurekaServer的注解信息
package com.example.eurekaserverone;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerOneApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerOneApplication.class, args);
}
}
第七步:添加application.properties的相关配置信息:
## server
server.port=8081
##eureka
#指定环境
eureka.environment=work
# 设置是否将自己作为客户端注册到注册中心(缺省true)
# 这里为不需要(查看@EnableEurekaServer注解的源码,会发现它间接用到了@EnableDiscoveryClient)
eureka.client.register-with-eureka=false
# 设置是否从注册中心获取注册信息(缺省true)
# 因为这是一个单点的EurekaServer,不需要同步其它EurekaServer节点的数据,故设为false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
#是否开启自我保护模式,默认为true。
eureka.server.enable-self-preservation=true
#续期时间,即扫描失效服务的间隔时间(缺省为60*1000ms)
eureka.server.eviction-interval-timer-in-ms=10000
第八步:启动服务,访问地址http://localhost:8081/
看到该界面则表示注册中心服务已经成功
但这时候并没有服务注册进来,所有服务instance处是没记录的
第九步:新建client的module,然后注册相应服务
第十步:找到com.example.eurekaclientone.EurekaClientOneApplication类,添加@EnableDiscoveryClient注解以实现Eureka中的DiscoveryClient实现。
package com.example.eurekaclientone;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class EurekaClientOneApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientOneApplication.class, args);
}
}
修改配置文件application.properties
##eureka
eureka.client.serviceUrl.defaultZone=http://localhost:8081/eureka/
eureka.instance.instance-id=${spring.application.name}:${server.port}
# 设置微服务调用地址为IP优先(缺省为false)
eureka.instance.prefer-ip-address=true
# 心跳时间,即服务续约间隔时间(缺省为30s)
eureka.instance.lease-renewal-interval-in-seconds=30
# 发呆时间,即服务续约到期时间(缺省为90s)
eureka.instance.lease-expiration-duration-in-seconds=90
第十一步:启动client,当然首先要保证server节点已经启动。
刷新下http://localhost:8081/
然后发现了instances里面有记录,则表示注册进来已经成功。
gitee的地址:
git@gitee.com:going100/eurekademo.git