千家信息网

Spring cloud中怎么配置项目结构实现Eureka服务

发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,本篇内容主要讲解"Spring cloud中怎么配置项目结构实现Eureka服务",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Spring cloud中怎
千家信息网最后更新 2025年02月02日Spring cloud中怎么配置项目结构实现Eureka服务

本篇内容主要讲解"Spring cloud中怎么配置项目结构实现Eureka服务",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Spring cloud中怎么配置项目结构实现Eureka服务"吧!

父项目管理

首先,我们在创建投放系统之前,先看一下我们的工程结构:

mscx-ad-sponsor就是我们的广告投放系统。如上结构,我们需要首先创建一个Parent Project mscx-ad

来编写父项目的pom,来管理我们的统一依赖信息。

    4.0.0    pom            mscx-ad-discovery        mscx-ad-zuul        mscx-ad-gateway        mscx-ad-discovery-nacos        mscx-ad-common        mscx-ad-db        mscx-ad-sponsor        mscx-ad-search        mscx-ad-feign-sdk                org.springframework.boot        spring-boot-starter-parent        2.1.5.RELEASE                 com.sxzhongf    mscx-ad    1.0-SNAPSHOT    分布式广告系统    基于Spring Cloud Alibaba 实现的分布式广告系统            1.8        Greenwich.SR2                            org.projectlombok            lombok                            org.springframework.boot            spring-boot-starter-test            test                                   org.springframework.boot            spring-boot-starter-actuator                                                                    org.springframework.cloud                spring-cloud-dependencies                ${spring-cloud.version}                pom                import                                                              spring-milestones            Spring Milestones            https://repo.spring.io/milestone                            false                                                alibaba-milestones            ali Milestones            http://maven.aliyun.com/nexus/content/groups/public/                            false                                                                                org.springframework.boot                spring-boot-maven-plugin                        

服务发现

Eureka
  • Eureka Server (提供服务的注册和发现)

  • Eureka Client

    • Service provider (服务提供方,将自身注册到server上,从而让Eureka Server保存provider的元数据,让其他的服务消费者可以找到当前服务)

    • Service Consumer(服务消费方,从Eureka Server上获取注册的服务列表,从而消费服务)

创建project mscx-ad-discovery, 然后使用SpringBoot项目的三部曲(加依赖,加注解,改配置)

编写POM,重点关注依赖spring-cloud-starter-eureka-server
            mscx-ad        com.sxzhongf        1.0-SNAPSHOT        ../pom.xml        4.0.0    jar    com.sxzhongf    mscx-ad-discovery    1.0-SNAPSHOT    服务发现组件    先使用eureka实现,后续会使用nacos替换掉                        org.springframework.cloud                        spring-cloud-starter-eureka-server            1.2.7.RELEASE                                                    org.springframework.boot                spring-boot-maven-plugin                        
添加注解(@EnableEurekaServer)
@SpringBootApplication@EnableEurekaServerpublic class DiscoveryApplication {    public static void main(String[] args) {        SpringApplication.run(DiscoveryApplication.class, args);    }}
改配置

单点

spring:  application:    name: ad-discovery-serverserver:  port: 8888eureka:  instance:    hostname: localhost #单机版  client:    fetch-registry: false #是否从eureka server获取注册信息    register-with-eureka: false #注册自己到eureka    service-url:      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

集群

测试的时候,需要修改hosts文件

zhangpandeMacBook-Pro:etc zhangpan$ cat hosts##...##127.0.0.1       localhost127.0.0.1       server1127.0.0.1       server2127.0.0.1       server3::1             localhost

然后修改application.yml

spring:  application:    name: ad-discovery  profiles: server1server:  port: 7777eureka:  instance:    hostname: server1    prefer-ip-address: false  client:    service-url:      defaultZone: http://server2:8888/eureka/,http://server3:9999/eureka/---spring:  application:    name: ad-discovery  profiles: server2server:  port: 8888eureka:  instance:    hostname: server2    prefer-ip-address: false  client:    service-url:      defaultZone: http://server1:7777/eureka/,http://server3:9999/eureka/---spring:  application:    name: ad-discovery  profiles: server3server:  port: 9999eureka:  instance:    hostname: server3    prefer-ip-address: false  client:    service-url:      defaultZone: http://server2:8888/eureka/,http://server1:7777/eureka/

启动集群测试:

  • 配置启动profile / java -jar mscx-ad-discovery.jar --spring.profiles.active=server1

  • 效果展示

到此,相信大家对"Spring cloud中怎么配置项目结构实现Eureka服务"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0