登录
首页 >  文章 >  java教程

微服务部分创建服务注册表应用程序

时间:2025-01-23 08:24:59 458浏览 收藏

golang学习网今天将给大家带来《微服务部分创建服务注册表应用程序》,感兴趣的朋友请继续看下去吧!以下内容将会涉及到等等知识点,如果你是正在学习文章或者已经是大佬级别了,都非常欢迎也希望大家都能给我建议评论哈~希望能帮助到大家!

构建微服务应用的第一步是搭建服务注册中心,它本身也是一个特殊的微服务,负责维护所有其他微服务的注册信息。

整个过程分为六个步骤:

步骤一:创建服务注册中心

使用 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
@EnableEurekaServer
public class ServiceRegistryApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceRegistryApplication.class, args);
    }

}

步骤三:配置服务注册中心

application.properties 文件中配置以下属性,告知Spring不将该应用注册为微服务:

spring.application.name=service-registry
server.port=8761

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

步骤四至六:注册新微服务

  1. 在新的微服务 pom.xml 中添加 spring-cloud-starter-netflix-eureka-client 依赖项。
  2. 配置新微服务的 application.properties 文件,指定 Eureka 服务器的 URL (指向步骤一至三创建的服务注册中心)。
  3. 启动服务注册中心和新微服务,访问 Eureka 服务器 URL (http://localhost:8761/) 验证注册结果。

微服务部分创建服务注册表应用程序

敬请期待微服务系列文章的后续内容!感谢阅读!

好了,本文到此结束,带大家了解了《微服务部分创建服务注册表应用程序》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多文章知识!

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>