以常见的电商业务为例
cloud-demo:
- order-service # 订单服务
- user-service # 用户服务
- product-service # 商品服务
— cloud-common # 不是应用服务,主要用于存放一些公共的内容,如pojo、baseservice这种、还有utils
cloud-demo模块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>com.acxgroupId>
<artifactId>cloud-demoartifactId>
<packaging>pompackaging>
<version>1.0.0version>
<modules>
<module>cloud-commonmodule>
<module>order-servicemodule>
<module>product-servicemodule>
<module>user-servicemodule>
modules>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.4.RELEASEversion>
parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Greenwich.SR1version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
project>
cloud-common模块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-demoartifactId>
<groupId>com.acxgroupId>
<version>1.0.0version>
parent>
<modelVersion>4.0.0modelVersion>
<groupId>com.acxgroupId>
<artifactId>cloud-commonartifactId>
<version>1.0.0version>
<packaging>jarpackaging>
<properties>
<lombok.version>1.18.20lombok.version>
<druid.version>1.1.10druid.version>
<mybatis.plus.version>3.2.0mybatis.plus.version>
<redis.version>1.4.1.RELEASEredis.version>
<fastjson.version>1.2.15fastjson.version>
properties>
<dependencies>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>${lombok.version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>${fastjson.version}version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>${mybatis.plus.version}version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>${druid.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-redisartifactId>
<version>${redis.version}version>
dependency>
dependencies>
project>
user-service模块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-demoartifactId>
<groupId>com.acxgroupId>
<version>1.0.0version>
parent>
<modelVersion>4.0.0modelVersion>
<groupId>com.acxgroupId>
<artifactId>user-serviceartifactId>
<version>1.0.0version>
<packaging>jarpackaging>
<dependencies>
<dependency>
<groupId>com.acxgroupId>
<artifactId>cloud-commonartifactId>
<version>1.0.0version>
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:
<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-demoartifactId>
<groupId>com.acxgroupId>
<version>1.0.0version>
parent>
<modelVersion>4.0.0modelVersion>
<groupId>com.acxgroupId>
<artifactId>product-serviceartifactId>
<version>1.0.0version>
<packaging>jarpackaging>
<dependencies>
<dependency>
<groupId>com.acxgroupId>
<artifactId>cloud-commonartifactId>
<version>1.0.0version>
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:
<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-demoartifactId>
<groupId>com.acxgroupId>
<version>1.0.0version>
parent>
<modelVersion>4.0.0modelVersion>
<groupId>com.acxgroupId>
<artifactId>order-serviceartifactId>
<version>1.0.0version>
<packaging>jarpackaging>
<dependencies>
<dependency>
<groupId>com.acxgroupId>
<artifactId>cloud-commonartifactId>
<version>1.0.0version>
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
关系: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 |
步骤一:新建eureka-service服务,并引入Eureka Server的pom依赖
<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-demoartifactId>
<groupId>com.acxgroupId>
<version>1.0.0version>
parent>
<modelVersion>4.0.0modelVersion>
<groupId>com.acxgroupId>
<artifactId>eureka-serviceartifactId>
<version>1.0.0version>
<packaging>jarpackaging>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
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 #是否检索拉取服务列表
步骤一:以order-service服务为例pom文件引入eureka-client依赖,后面依次配置product和user服务即可
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
步骤二:编写eureka相关的配置文件
eureka:
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://127.0.0.1:10081/eureka/
