Hoxton.SR10
版本:Hoxton.SR10
版本的SpringCloud所支持的 2.3.8.RELEASE
:SpringBoot2.3.8.RELEASE
所推荐的 JDK8和Maven3.5.4(3.3+)SpringCloud-Hoxton.SR10
和 SpringBoot2.3.8.RELEASE
官方PDF文档首先我们先来搭建一个聚合项目也就是一个空到只有 pom.xml
文件的maven项目,pom.xml文件内容
<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.0modelVersion>
<groupId>code.xiaohhgroupId>
<artifactId>cloud-alibaba-demoartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>pompackaging>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<spring.boot.version>2.3.8.RELEASEspring.boot.version>
<spring.cloud.version>Hoxton.SR10spring.cloud.version>
<spring.cloud.alibaba.version>2.2.5.RELEASEspring.cloud.alibaba.version>
<mybatisplus.version>3.4.2mybatisplus.version>
<lombok.version>1.18.18lombok.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${spring.boot.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring.cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>${spring.cloud.alibaba.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>${mybatisplus.version}version>
<exclusions>
<exclusion>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generatorartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>${lombok.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
project>
然后建立一个 cloud-alibaba-common 模块作为一个统一类的管理:
然后接下来的步骤和创建maven项目一样,其中pom.xml文件:
<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>cloud-alibaba-demoartifactId>
<groupId>code.xiaohhgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloud-alibaba-commonartifactId>
<packaging>jarpackaging>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
dependency>
<dependency>
<groupId>commons-langgroupId>
<artifactId>commons-langartifactId>
<version>2.6version>
dependency>
dependencies>
project>
其中的代码都是一些公用的工具类,因为不想让文档过于臃肿,待会会提交到git仓库
创建 cloud-alibaba-payment
支付模块,pom.xml文件:
<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>cloud-alibaba-demoartifactId>
<groupId>code.xiaohhgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloud-alibaba-paymentartifactId>
<packaging>jarpackaging>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>code.xiaohhgroupId>
<artifactId>cloud-alibaba-commonartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<executions>
<execution>
<goals>
<goal>repackagegoal>
goals>
execution>
executions>
plugin>
plugins>
build>
project>
bootstrap.yml:
spring:
cloud:
nacos:
discovery: # 注册中心
server-addr: 192.168.1.230:8848 #请更换成自己的nacos地址
config: # 配置中心
server-addr: 192.168.1.230:8848 #请更换成自己的nacos地址
file-extension: yml # 配置中心文件的后缀
application.yml:
server:
port: 2020 # 服务器端口号
spring:
application:
name: cloud-alibaba-payment # 应用名,也就是注册到注册中心的名字
datasource: # 数据库配置
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.1.230:3306/cloud_payment?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: root
profiles:
active: dev # 当前处于开发环境
mybatis-plus: # mybatis plus 配置
mapper-locations: classpath:mapper/payment/*.xml # mapper 文件配置
type-aliases-package: code.xiaohh.cloudalibaba.common.entities # entities 的别名都在哪个包下
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 设置打印SQL语句
PaymentApplication.java:
package code.xiaohh.cloudalibaba.payment;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
*
* 支付模块启动类
*
*
* @author XiaoHH
* @version 1.0
* @date 2021-04-10 星期六 14:53:51
* @file PaymentApplication.java
*/
@EnableDiscoveryClient // 启用注册中心
@EnableFeignClients(basePackages = "code.xiaohh.cloudalibaba.common.feigns") // 启用远程调用
@SpringBootApplication
public class PaymentApplication {
public static void main(String[] args) {
// 启动支付模块
SpringApplication.run(PaymentApplication.class, args);
System.out.println("^V^ 支付模块启动成功 ^V^\n" +
"/-\\ /-\\ /-\\ /-\\ /-\\ /-\\\n" +
" X i a o H H\n" +
"\\-/ \\-/ \\-/ \\-/ \\-/ \\-/");
}
}
payment数据库建库建表语句:
DROP DATABASE IF EXISTS `cloud_payment`;
CREATE DATABASE `cloud_payment`;
USE `cloud_payment`;
CREATE TABLE `payment` (
`payment_id` BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '支付id',
`serial` VARCHAR(50) NOT NULL COMMENT '支付流水号'
) COMMENT '支付表';
接下来的代码我们用 renren-generator
的代码生成器生成以下,其实也就是SpringBoot的SSM项目的代码,可以查看我的文章SpringBoot搭建SSM项目,点击这里下载代码生成器,代码生成器步骤请自己去看人家的官方微信,首先添加在数据库当中添加一条数据看看效果:
添加成功,SQL语句也成功发送:
数据库当中:
同时我们生成订单模块的微服务,代码基本上相同,不同的地方:
application.yml数据库连接的url配置和应用名端口号(仅仅只列出了不同的地方):
server:
port: 2030 # 服务器端口号
spring:
application:
name: cloud-alibaba-order # 应用名,也就是注册到注册中心的名字
datasource:
url: jdbc:mysql://192.168.1.230:3306/cloud_order?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
order数据库建库建表语句:
DROP DATABASE IF EXISTS `cloud_order`;
CREATE DATABASE `cloud_order`;
USE `cloud_order`;
CREATE TABLE `order` (
`order_id` BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '订单id',
`order_number` VARCHAR(50) NOT NULL COMMENT '订单号'
) COMMENT '订单表';
同样用代码生成器生成代码,然后我们来到nacos服务列表,可以看到nacos服务注册是成功的
然后我们来试试注册中心,注册中心配置读取的规则如下:
${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
# 应用名-当前环境.配置中心文件的后缀
那么根据 bootstrap.yml
和 application.yml
的配置则:
cloud-alibaba-order
模块的配置名为 cloud-alibaba-order-dev.yml
cloud-alibaba-payment
模块的配置名为 cloud-alibaba-payment-dev.yml
现在去nacos配置中心添加配置:
订单服务配置:
支付模块配置:
查看配置号的配置
重启之后可以发现两个服务分别在 8080 和 8090 启动成功
根据SpringCloud的架构图我们还需要一个服务网关来路由将请求推送到指定服务:
我们再建立一个 cloud-alibaba-gateway
来作为服务网关
pom.xml:
<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>cloud-alibaba-demoartifactId>
<groupId>code.xiaohhgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloud-alibaba-gatewayartifactId>
<packaging>jarpackaging>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-gatewayartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<executions>
<execution>
<goals>
<goal>repackagegoal>
goals>
execution>
executions>
plugin>
plugins>
build>
project>
bootstrap.yml:
spring:
cloud:
nacos:
discovery: # 注册中心
server-addr: 192.168.1.230:8848 #请更换成自己的nacos地址
config: # 配置中心
server-addr: 192.168.1.230:8848 #请更换成自己的nacos地址
file-extension: yml # 配置中心文件的后缀
application.yml:
server:
port: 80 # 服务器端口号
spring:
application:
name: cloud-alibaba-gateway # 应用名,也就是注册到注册中心的名字
profiles:
active: dev # 当前处于开发环境
cloud:
gateway: # 网关配置
routes: # 路由配置
# 订单服务的路由
- id: cloud-alibaba-order-route # 单个路由的id
uri: lb://cloud-alibaba-order # 路由条件满足后跳转到这个服务
predicates:
- Path=/api/order/** # 断言,当请求url满足这个路径要求时路由
filters:
- RewritePath=/api/(?>.*),/$\{segment} # 路径重写,确保不会404
#支付服务的路由
- id: cloud-alibaba-payment-route # 单个路由的id
uri: lb://cloud-alibaba-payment # 路由条件满足后跳转到这个服务
predicates:
- Path=/api/payment/** # 断言,当请求url满足这个路径要求时路由
filters:
- RewritePath=/api/(?>.*),/$\{segment} # 路径重写,确保不会404
GatewayApplication.java:
package code.xiaohh.cloudalibaba.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
*
* 网关模块启动类
*
*
* @author XiaoHH
* @version 1.0
* @date 2021-04-10 星期六 16:42:08
* @file GatewayApplication.java
*/
@EnableDiscoveryClient
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
// 启动网关模块
SpringApplication.run(GatewayApplication.class, args);
System.out.println("^V^ 网关模块启动成功 ^V^\n" +
"/-\\ /-\\ /-\\ /-\\ /-\\ /-\\\n" +
" X i a o H H\n" +
"\\-/ \\-/ \\-/ \\-/ \\-/ \\-/");
}
}
好的,网关配置好了,现在我们来测试以下:
根据路径重写规则发送获取订单id为1的请求:
http://127.0.0.1/api/order/order/info/1
可以看到请求的是网关的80端口,并且转发到订单服务了,订单服务打印SQL语句:
再来测试支付模块:
http://127.0.0.1/api/payment/payment/info/1
同样请求成功:
支付同样的也打印了SQL语句:
微服务框架的搭建就告一段落了,其他接口自行测试哦!代码已经提交git仓库