掘金日新计划 · 8 月更文挑战第19天--搭建Eureka Client客户端注册

87 阅读1分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第19天,点击查看活动详情

上一个小节,我们学会了配置Eureka Server的配置,并且成功启动了Eureka,那么今天我们来学习一下如何把客户端服务注册到Eureka Server中

1.首先看一下我们之前的项目结构

image.png
今天我们要学习的是把good-list和good-price注册到eureka-server中,接下来跟着我的步伐,一起来实践一下吧

2.我们先来配置一下good-list的注册到eureka-server中

image.png 找到good-list的pom.xml文件,新增以下配置

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

然后在rescource下的application.proterties配置Eureka的默认地址 image.png 这个是eureka server注册中心的地址,这个说明客户端向这个地址注册

eureka.client.service-url.defaultZone = http://localhost:8000/eureka/

3.接下来我们分别启动Eureka Server和good-list项目

image.png 启动之后,我们访问 http://localhost:8000/ 发现good-list已经注册上去了 image.png

3.同样的操作,我们把good-price也注册上去

在good-price的pom.xml文件配置eureka客户端

image.png

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

然后在rescource下的application.proterties配置Eureka的默认地址 image.png

4.配置好之后,我们启动good-price服务

image.png 这时候三个服务都启动起来了

5.测试

访问:http://localhost:8000/ image.png 我们可以看到good-list和good-price都已经注册上来了

总结: pom.xml 引入 eureka-client依赖
模块中 application.properties 配置eureka server地址
重启服务即可