[Spring cloud 一步步实现广告系统] 2. 配置项目结构 & 实现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
@EnableEurekaServer
public class DiscoveryApplication {
    public static void main(String[] args) {
        SpringApplication.run(DiscoveryApplication.class, args);
    }
}
改配置
单点
spring:
  application:
    name: ad-discovery-server
server:
  port: 8888
eureka:
  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    localhost
127.0.0.1    server1
127.0.0.1    server2
127.0.0.1    server3
::1             localhost

然后修改application.yml

spring:
  application:
    name: ad-discovery
  profiles: server1
server:
  port: 7777
eureka:
  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: server2
server:
  port: 8888
eureka:
  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: server3
server:
  port: 9999
eureka:
  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
  • 效果展示

做一个好人。

你可能感兴趣的