搭建springboot+springcloud父子分布式项目

一步步跟着完成后就可以搭建一个简单父子微服务框架

首先创建空壳maven项目

File->New->Project

搭建springboot+springcloud父子分布式项目_第1张图片

 搭建springboot+springcloud父子分布式项目_第2张图片

搭建springboot+springcloud父子分布式项目_第3张图片

删除多余文件只保留pom.xml

搭建springboot+springcloud父子分布式项目_第4张图片

现在就有了一个空壳maven项目,用作于父级项目,后续在其基础上创建springcloud项目

创建eureka(注册中心)项目选择相应服务

父级项目右击New->Module

 搭建springboot+springcloud父子分布式项目_第5张图片

 搭建springboot+springcloud父子分布式项目_第6张图片

 搭建springboot+springcloud父子分布式项目_第7张图片

勾选注册服务后点击next->finish完成创建

搭建springboot+springcloud父子分布式项目_第8张图片

搭建gateway(网关)

父级项目右击New->Module

搭建springboot+springcloud父子分布式项目_第9张图片

搭建springboot+springcloud父子分布式项目_第10张图片

 搭建springboot+springcloud父子分布式项目_第11张图片

完成创建后项目结构

搭建springboot+springcloud父子分布式项目_第12张图片

接下来完成父子项目的构建

在父级pom.xml中添加springboot依赖

    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.12.RELEASE
    

查看springboot对应springcloud版本

Spring Cloud

搭建springboot+springcloud父子分布式项目_第13张图片

添加版本控制以便父级统一管理

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.SR12
                pom
                import
            
        
    

添加子模块

    
        eureka
        gateway
    

添加后完整pom.xml文件



    4.0.0

    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.12.RELEASE
    

    com.own
    myself
    1.0-SNAPSHOT
    pom

    
        eureka
        gateway
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.SR12
                pom
                import
            
        
    

子项目pom.xml更改父级指向

    
        com.own
        myself
        1.0-SNAPSHOT
         
    

删除无用依赖

搭建springboot+springcloud父子分布式项目_第14张图片

eureka完整pom.xml



    4.0.0
    
        com.own
        myself
        1.0-SNAPSHOT
         
    

    com.own
    eureka
    0.0.1-SNAPSHOT
    eureka
    Demo project for Spring Boot

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


gateway完整pom.xml



    4.0.0
    
        com.own
        myself
        1.0-SNAPSHOT
         
    

    com.own
    gateway
    0.0.1-SNAPSHOT
    gateway
    Demo project for Spring Boot

    
        
            org.springframework.cloud
            spring-cloud-starter-gateway
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


到这里我们就完成了一个简单微服务框架的基础,接下来配置启动

 在EurekaApplication添加类注解

@EnableEurekaServer

配置application.yml文件

server:
  port: 9800
spring:
  application:
    name: eureka
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:${server.port}/eureka/
    register-with-eureka: false #是否将自己注册到Eureka服务中,本身就是服务,所以无需注册
    fetch-registry: false # 是否从Eureka 中获取注册信息
#    instance:
#        prefer-ip-address: true #将自己的ip注册到eureka服务中

 在GatewayApplication添加类注解

@EnableEurekaClient

配置application.yml文件

server:
  port: 9801
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:9800/eureka/
  instance:
    prefer-ip-address: true
  server:
    enable-self-preservation: false
spring:
  application:
    name: gateway
  cloud:
    gateway:
      globalcors:
        cors-configurations:
          '[/**]': #匹配所有请求
            allow-credentials: true
            allowedHeaders: "*"
            allowedOrigins: "*" #跨域处理 允许所有的域
            allowedMethods: # 支持的方法
              - GET
              - POST
              - PUT
              - DELETE
    discovery:
      locator:
        enabled: true
        lower-case-service-id: true #使用小写service-id 使用微服务的名称转发对应的服务

配置好后就可以完成我们的启动了!

搭建springboot+springcloud父子分布式项目_第15张图片

你可能感兴趣的:(搭建微服务项目,java,eureka,idea)