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

本文详细介绍了如何一步步搭建一个基于Springboot和Springcloud的父子微服务框架,包括创建空壳Maven项目作为父级,添加Eureka作为注册中心,以及构建Gateway作为网关。通过配置父级和子模块的pom.xml文件,以及相应的启动注解和应用配置,最终形成一个简单的微服务架构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

首先创建空壳maven项目

File->New->Project

 

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

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

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

父级项目右击New->Module

 

 

 

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

搭建gateway(网关)

父级项目右击New->Module

 

完成创建后项目结构

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

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
    </parent>

查看springboot对应springcloud版本

Spring Cloud

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR12</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

添加子模块

    <modules>
        <module>eureka</module>
        <module>gateway</module>
    </modules>

添加后完整pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
    </parent>

    <groupId>com.own</groupId>
    <artifactId>myself</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>eureka</module>
        <module>gateway</module>
    </modules>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR12</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

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

    <parent>
        <groupId>com.own</groupId>
        <artifactId>myself</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

删除无用依赖

eureka完整pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.own</groupId>
        <artifactId>myself</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.own</groupId>
    <artifactId>eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

gateway完整pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.own</groupId>
        <artifactId>myself</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.own</groupId>
    <artifactId>gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gateway</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

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

 在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 使用微服务的名称转发对应的服务

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值