构建微服务应用的第一步是搭建服务注册中心,它本身也是一个特殊的微服务,负责维护所有其他微服务的注册信息。
整个过程分为六个步骤:
步骤一:创建服务注册中心
使用 spring-cloud-starter-netflix-eureka-server 依赖项构建服务注册中心微服务应用。 pom.xml 文件如下:
4.0.0 org.springframework.boot spring-boot-starter-parent 3.4.1 com.sky service-registry 1.0 service-registry registry for job portal application 21 2024.0.0 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
登录后复制
步骤二:启用Eureka Server
在主应用程序类中添加 @EnableEurekaServer 注解:
package com.sky.service_registry;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServerpublic class ServiceRegistryApplication { public static void main(String[] args) { SpringApplication.run(ServiceRegistryApplication.class, args); }}
登录后复制
步骤三:配置服务注册中心
在 application.properties 文件中配置以下属性,告知Spring不将该应用注册为微服务:
spring.application.name=service-registryserver.port=8761eureka.instance.hostname=localhosteureka.client.register-with-eureka=falseeureka.client.fetch-registry=false
登录后复制
步骤四至六:注册新微服务
在新的微服务 pom.xml 中添加 spring-cloud-starter-netflix-eureka-client 依赖项。配置新微服务的 application.properties 文件,指定 Eureka 服务器的 URL (指向步骤一至三创建的服务注册中心)。启动服务注册中心和新微服务,访问 Eureka 服务器 URL (http://localhost:8761/) 验证注册结果。
敬请期待微服务系列文章的后续内容!感谢阅读!
以上就是微服务部分创建服务注册表应用程序的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2607369.html