SpringCloud(一):学习搭建分布式项目(入门)

SpringCloud(一):学习搭建分布式项目(入门)

一、技术选型

Spring Cloud Hoxton.SR8
Spring Boot 2.3.0.RELEASE

二、模块设计

springcloud父工程(Project)下初次带着3个子模块(Module) ------ pom
shop-common【封装的整体entity / 接口 / 公共配置等】 ------ jar
shop-user-807x【服务提供者】 ------- war
shop-product-808x【服务提供者】 ------- war
shop-order-809x【服务消费者】 ------- war
注:端口号分配是为后面集群预留。

在这里插入图片描述

三、开始搭建分布式项目

1.先创建一个父工程

在这里插入图片描述在这里插入图片描述

2.在父工程里面引入依赖

<?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>

    <groupId>com.nine.winfred</groupId>
    <artifactId>SpringCloud-Demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>shop-user</module>
        <module>shop-common</module>
        <module>shop-product</module>
        <module>shop-order</module>
    </modules>

    <properties>
        <java.version>1.9</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.source>1.9</maven.compiler.source>
        <maven.compiler.target>1.9</maven.compiler.target>
        <springboot.version>2.3.0.RELEASE</springboot.version>
        <springcloud.version>Hoxton.SR8</springcloud.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- spring-cloud-dependencies -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${springcloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- spring-boot-dependencies -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${springboot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

3.在父工程里面新建子工程模块

在这里插入图片描述
在这里插入图片描述

4.在子模块里面引入依赖

在这里插入图片描述

<?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">
    <parent>
        <artifactId>SpringCloud-Demo</artifactId>
        <groupId>com.nine.winfred</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>shop-user</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!--公共模块-->
        <dependency>
            <groupId>com.nine.winfred</groupId>
            <artifactId>shop-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--springboot-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

注:子工程建立好之后父工程的pom.xml 里面会自动生成如下代码(表示该工程下的子工程)

在这里插入图片描述

5.依次把所需要的子工程都建立好,结构如下

在这里插入图片描述

6.在java下面新建一个包,包下面新建一个主启动类,在resources下面新建application.yml (这个名字不能随意修改,否则没用,前面图标变成绿色的小叶子则成功)

在这里插入图片描述

7.主启动类代码:

package com.nine.winfred;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

8.application.yml 代码(yml里面根据之前的需求填写对应的端口号):

server:
  port: 8071

9.在主启动类所在的包下面新建一个controller包先用于测试

在这里插入图片描述

10.再新建一个用于测试的控制器

package com.nine.winfred.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class TestUser {

    @ResponseBody
    @RequestMapping("/test")
    public String test(){
        return "测试";
    }
}

11.回到主启动类,右击选择Run启动

在这里插入图片描述

12.执行成功

在这里插入图片描述

13.打开浏览器输入 http://localhost:8071/user/test
端口号为yml里面设置的8071,后面加上控制器设置的请求路径,页面打印控制器返回的字符串就代表成功了。

在这里插入图片描述

13.项目的最终结构

在这里插入图片描述

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值