微服务旅程(二)单节点eureka 服务注册中心

第一步:采用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值