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.项目的最终结构