boot与cloud版本
springboot:提供了快速开发微服务的能力
springcloud提供了微服务治理的能力(服务注册与发现、服务降级、限流、熔断、网关、负载均衡、配置中心...),为微服务开发提供了全家桶服务
springboot的版本查看地址:Spring Boot
springcloud的版本查看地址:Spring Cloud
详细版本对应信息查看:https://start.spring.io/actuator/info
SpringCloud-alibaba
Springcloud与springcloud-alibaba关系
◆ 我们通常说的SpringCloud,泛指Spring Cloud Netflix,也是springcloud第一代
◆ SpringCloud Alibaba是SpringCloud的子项目,是阿里巴巴结合自身微服务实践,
◆ SpringCloud Alibaba符合SpringCloud标准,依赖于springcloud
确定版本
确定方式:通过查看springcloud alibaba 官网确定
https://github.com/alibaba/spring-cloud-alibaba/wiki/版本说明
https://github.com/alibaba/spring-cloud-alibaba/wiki/
这里使用:
springcloud-alibaba: 2.2.5.RELEASE
springcloud: Hoxton.SR8
springboot: 2.3.2.RELEASE
创建父工程
父工程锁定springboot、springcloud、springcloud-alibaba版本
下图为示例项目模块 父项目用maven项目就可以
下图是父项目的pom.xml cloud1的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>
<!-- 自己的gva坐标-->
<groupId>com.qianfeng</groupId>
<artifactId>cloud1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- 描述哪一些子模块-->
<modules>
<module>goods</module>
<module>orders</module>
<module>commons</module>
</modules>
<!-- 表示自己父项目springboot版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!--依赖管理 描述了springcloud 和springcloudAlibaba的版本-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
服务提供者
项目的goods older相当于是一个nacos客户端
goods的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.qianfeng</groupId>
<artifactId>cloud1</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.qianfeng</groupId>
<artifactId>goods</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>goods</name>
<description>goods</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- web的场景依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 端点监控的场景依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- nacos场景依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
下图是goods的配置文件 这是一个springboot项目
spring:
application:
name: goods
cloud:
nacos:
discovery:
server-addr: localhost:8848
username: nacos
password: nacos
server:
port: 9001
下图是goods一个controller controller的包要写在goods下面 访问时访问配置的9001
@RestController
@RequestMapping("/goods")
public class GoodsController {
@RequestMapping("/getOne")
public Goods getOne(Integer gid){
Goods goods = new Goods(gid, "小浣熊", 1);
return goods;
}
}
order的pom.xml跟goods的xml一样 导依赖描述父项目 只是配置文件端口改为9002
commons是一个管理实体类的项目模块 pom文件如下 是一个maven项目
<?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>cloud1</artifactId>
<groupId>com.qianfeng</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>commons</artifactId>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
例如order项目想引入goods实体类 导入commons的gva坐标就可以在order项目里使用实体类