Dubbo+Zookeeper(RPC协议,主要用于大型分布式项目:当当)
SpringCloud+Eureka(官方)(闭源)(Http协议,中小型开发)
SpringCloud+Eureka+RestTemplate+Feign+Hystric+Zull+GateWay+配置中心+分布式事务+Bus总线
SpringCoud+Nacos(阿里)(Http协议,中小型开发)
SpringCloud+Nacos+RestTemplate+Feign+Sentinel+GateWay+Nacos自带的配置中心+分布式事务+链路跟踪
古老的技术
Spring Cloud是2014年,由Martin Fowler与James Lewis提出开发的。他们在开发的时候,为了提升开发效率,减少配置文件的编写,于是选择了Spring Boot作为微服务程序开发脚手架工具
但是它们在开发时,有版本的要求,版本对应关系,可从官网查看
Spring Cloud | Spring Boot |
---|---|
Angel版本 | 兼容Spring Boot 1.2.x |
Brixton版本 | 兼容Spring Boot 1.3.x,也兼容Spring Boot 1.4.x |
Camden版本 | 兼容Spring Boot 1.4.x,也兼容Spring Boot 1.5.x |
Dalston版本、Edgware版本 | 兼容Spring Boot 1.5.x,不兼容Spring Boot 2.0.x |
Finchley版本 | 兼容Spring Boot 2.0.x,不兼容Spring Boot 1.5.x |
Greenwich版本 | 兼容Spring Boot 2.1.x |
Spring Boot | Spring Cloud |
---|---|
1.5.2.RELEASE | Dalston.RC1 |
1.5.9.RELEASE | Edgware.RELEASE |
2.0.2.RELEASE | Finchley.BUILD-SNAPSHOT |
2.0.3.RELEASE | Finchley.RELEASE |
2.1.0.RELEASE | Greenwich.SR1 |
2.3.0.RELEASE | Hoxton.SR4 |
maven
mysql
mybatis
spring cloud alibaba
工程名 | 描述 | 端口 |
---|---|---|
star-cloud | 父工程 | - |
star-common | 公共模块,放置实体类和工具类 | - |
star-product | 商品微服务 | 808x |
star-order | 订单微服务 | 809x |
作为父工程,是不需要编写源代码的,所以注解把父工程中的src目录删除掉,然后编辑父工程的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>
<groupId>top.ygang</groupId>
<artifactId>star-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- 依赖版本锁定 -->
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
<spring-cloud-alibaba.version>2.1.1.RELEASE</spring-cloud-alibaba.version>
<spring-boot.version>2.1.3.RELEASE</spring-boot.version>
<lombok.version>1.18.16</lombok.version>
<mysql.version>5.1.38</mysql.version>
<mybatis.version>1.3.2</mybatis.version>
</properties>
<!-- 版本仲裁中心,管理子模块中使用的依赖版本 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 安装Spring Boot Maven插件,可以简化Maven和Spring Boot之间的交互 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1、在该模块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">
<parent>
<artifactId>star-cloud</artifactId>
<groupId>top.ygang</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>star-common</artifactId>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
</project>
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">
<parent>
<artifactId>star-cloud</artifactId>
<groupId>top.ygang</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>star-product</artifactId>
<dependencies>
<dependency>
<groupId>top.ygang</groupId>
<artifactId>star-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>
由于引入的公共模块已经引入了lombok和mysql依赖,所以不需要再引入
编写代码
和商品微服务一样的步骤
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
@PostMapping("/insert/{pid}/{number}")
public ResultVO insert(@PathVariable Integer pid,@PathVariable Integer number){
try {
ResultVO resultVO = restTemplate.getForObject("http://localhost:8080/product/findByPid/" + pid, ResultVO.class);
//此处返回的是一个LinkedHashMap
LinkedHashMap map = (LinkedHashMap)resultVO.getE();
//使用hutool工具,将map中的值封装进对象
Product product = BeanUtil.fillBeanWithMap(map,new Product(),true);
//将商品中的id和名称封装进订单对象
Orders orders = new Orders();
BeanUtil.copyProperties(product,orders,true);
orders.setNumber(number);
orders.setPrice(product.getPrice() * number);
orders.setUsername("lucy");
//插入数据库
ordersService.insert(orders);
return ResultVO.success("新增订单成功");
}catch (Exception e){
e.printStackTrace();
return ResultVO.fail("新增订单异常");
}
}