1.SpringCloud电商示例
1.1.示例架构
以常见的电商业务为例
cloud-demo:
- order-service # 订单服务
- user-service # 用户服务
- product-service # 商品服务
— cloud-common # 不是应用服务,主要用于存放一些公共的内容,如pojo、baseservice这种、还有utils
cloud-demo模块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>com.acx</groupId>
<artifactId>cloud-demo</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>
<modules>
<module>cloud-common</module>
<module>order-service</module>
<module>product-service</module>
<module>user-service</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
cloud-common模块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>cloud-demo</artifactId>
<groupId>com.acx</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.acx</groupId>
<artifactId>cloud-common</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<lombok.version>1.18.20</lombok.version>
<druid.version>1.1.10</druid.version>
<mybatis.plus.version>3.2.0</mybatis.plus.version>
<redis.version>1.4.1.RELEASE</redis.version>
<fastjson.version>1.2.15</fastjson.version>
</properties>
<dependencies>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!--fastjson:序列化工具-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!--mybatisplus:mysql持久层操作-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis.plus.version}</version>
</dependency>
<!--jdbc:mysql连接驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--druid:mysql数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>${redis.version}</version>
</dependency>
</dependencies>
</project>
user-service模块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>cloud-demo</artifactId>
<groupId>com.acx</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.acx</groupId>
<artifactId>user-service</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.acx</groupId>
<artifactId>cloud-common</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
user-service配置文件application.yml:
server:
port: 8083
spring:
application:
name: user-service
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/mc_mall?useSSL=false&useUnicode=true&characterEncoding=utf-8
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 50 #初始化时建立物理连接的个数
minIdle: 30 #最小连接池数量
maxActive: 200 #最大连接池数量
maxWait: 60000 #获取连接时最大等待时间
timeBetweenEvictionRunsMillis: 60000 #Destory线程检测连接的间隔时间
minEvictableIdleTimeMillis: 300000 #连接保持空闲而不被驱逐的最长时间
redis:
host: localhost
port: 6379
password: 123456
product-service模块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>cloud-demo</artifactId>
<groupId>com.acx</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.acx</groupId>
<artifactId>product-service</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.acx</groupId>
<artifactId>cloud-common</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
product-service配置文件application.yml:
server:
port: 8082
spring:
application:
name: product-service
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/mc_mall?useSSL=false&useUnicode=true&characterEncoding=utf-8
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 50 #初始化时建立物理连接的个数
minIdle: 30 #最小连接池数量
maxActive: 200 #最大连接池数量
maxWait: 60000 #获取连接时最大等待时间
timeBetweenEvictionRunsMillis: 60000 #Destory线程检测连接的间隔时间
minEvictableIdleTimeMillis: 300000 #连接保持空闲而不被驱逐的最长时间
redis:
host: localhost
port: 6379
password: 123456
order-service模块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>cloud-demo</artifactId>
<groupId>com.acx</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.acx</groupId>
<artifactId>order-service</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.acx</groupId>
<artifactId>cloud-common</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
order-service配置文件application.yml:
server:
port: 8081
spring:
application:
name: order-service
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/mc_mall?useSSL=false&useUnicode=true&characterEncoding=utf-8
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 50 #初始化时建立物理连接的个数
minIdle: 30 #最小连接池数量
maxActive: 200 #最大连接池数量
maxWait: 60000 #获取连接时最大等待时间
timeBetweenEvictionRunsMillis: 60000 #Destory线程检测连接的间隔时间
minEvictableIdleTimeMillis: 300000 #连接保持空闲而不被驱逐的最长时间
redis:
host: localhost
port: 6379
password: 123456
1.2.Spring Cloud与Spring Boot
关系:Spring Boot是Spring Cloud中必不可少的一部分,Springboot是微服务的基础实现技术。
Spring Boot和Spring Cloud版本对应表:也可自行去官网查看
Release Train | Boot Version |
---|---|
2021.0.x aka Jubilee | 2.6.x |
2020.0.x aka Ilford | 2.4.x, 2.5.x (Starting with 2020.0.3) |
Hoxton | 2.2.x, 2.3.x (Starting with SR5) |
Greenwich | 2.1.x |
Finchley | 2.0.x |
Edgware | 1.5.x |
Dalston | 1.5.x |
2.Eureka注册中心
2.1.Eureka Server
- Eureka2.X版本已经停止更新维护了,但是Eureka1.X版本还是一直在进行更新维护的
步骤一:新建eureka-service服务,并引入Eureka Server的pom依赖
<?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>cloud-demo</artifactId>
<groupId>com.acx</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.acx</groupId>
<artifactId>eureka-service</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
步骤二:eureka-service服务启动类上面加上@EnableEurekaServer标签开启eureka-server服务
package com.acx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
步骤三:编写eureka配置application.yml
server:
port: 10081
spring:
application:
name: eureka-service
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10081/eureka/
register-with-eureka: false #是否在注册中心注册自己
fetch-registry: false #是否检索拉取服务列表
2.2.Eureka Client
步骤一:以order-service服务为例pom文件引入eureka-client依赖,后面依次配置product和user服务即可
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
步骤二:编写eureka相关的配置文件
eureka:
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://127.0.0.1:10081/eureka/
Eureka控制台查看服务注册详情
- 游览器输入localhost:100081直接访问euraka控制台,发现order、product、user三个服务注册成功
2.3.Eureka保活机制
- 每一个eureka-client客户端服务都会定时的想eureka-server注册中心发送一个心跳包进行保活,服务端会通过心跳包来实现对客户端活跃状态进行监控。