SpringCloud项目搭建
项目使用技术:mybatis,redis,springboot,springMVC,Nacos,Feign,GateWay,rabbitmq
父工程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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.os467</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Cloud</description>
<!--指定为pom打包方式-->
<packaging>pom</packaging>
<properties>
<!--指定jdk版本-->
<java.version>1.8</java.version>
<!--指定编码格式-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!--SpringBoot版本-->
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
<!--SpringCloud版本-->
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
<!--Mysql版本-->
<mysql.version>8.0.22</mysql.version>
<!--Mybatis版本-->
<mybatis.version>2.1.1</mybatis.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.9.RELEASE</version>
<relativePath/>
</parent>
<modules>
<module>order-service</module>
</modules>
<dependencyManagement>
<dependencies>
<!--nacos管理依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- springCloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!--lombok依赖-->
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
子模块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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>order-service</artifactId>
<parent>
<groupId>com.os467</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<!--feign客户端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--httpClient的依赖-->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
<!-- nacos客户端依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--SpringWeb-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
详细步骤
创建父工程
创建工程选择springboot,清除父工程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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.os467</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Cloud</description>
指定父工程打包方式
<!--指定为pom打包方式-->
<packaging>pom</packaging>
引入版本参数
<properties>
<!--指定jdk版本-->
<java.version>1.8</java.version>
<!--指定编码格式-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!--SpringBoot版本-->
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
<!--SpringCloud版本-->
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
<!--Mysql版本-->
<mysql.version>8.0.22</mysql.version>
<!--Mybatis版本-->
<mybatis.version>2.1.1</mybatis.version>
</properties>
继承一个springboot父工程
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.9.RELEASE</version>
<relativePath/>
</parent>
<dependencyManagement>
标签
在dependencyManagement元素中声明所依赖的jar包的版本号等信息,那么所有子项目再次引入此依赖jar包时则无需显式的列出版本号
<dependencyManagement>
<dependencies>
<!--nacos管理依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- springCloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
添加lombok组件依赖
<!--lombok依赖-->
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
创建子模块(服务)
在父工程pom文件中添加模块
<modules>
<module>order-service</module>
</modules>
选择父工程添加模块,创建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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
继承父工程<parent>标签
引入父pom里面的依赖时无须指定版本
<parent>
<groupId>com.os467</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
声明当前工程名称
<modelVersion>4.0.0</modelVersion>
<artifactId>order-service</artifactId>
添加所需依赖
<dependencies>
<!--feign客户端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--httpClient的依赖-->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
<!-- nacos客户端依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--SpringWeb-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
</dependencies>
添加spring boot提供的maven打包插件
<finalName>
标签 打包成jar包的名字
<build>
<finalName>application</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
创建application.yaml文件
# 应用名称
spring:
application:
name: orderservice
创建启动类
业务相关的类需要写在和启动类同级的包下
package com.os467.orderservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}
}
为微服务搭建包结构
- Controller
- Service
- Mapper
- pojo
整合freemarker
在父pom文件中添加依赖,让所有子模块继承
<!--freemarker模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
在子模块启动类配置文件中配置视图后缀和模板文件位置
# 应用名称
spring:
application:
name: orderservice
#freemarker配置
freemarker:
#配置模板文件位置
template-loader-path: classpath:/templates/
#视图后缀
suffix: .ftl
- 在resource文件下创建templates文件,里面存放ftl文件
<html>
<head>
</head>
<body>
<#--通过freemarker来遍历集合中的对象-->
<#list orderList as order >
${order ! "null"}
</#list>
</body>
</html>
整合mybatis
配置mysql数据源信息,用来连接数据库
# 应用名称
spring:
application:
name: orderservice
#freemarker配置
freemarker:
#配置模板文件位置
template-loader-path: classpath:/templates/
#视图后缀
suffix: .ftl
#数据源信息
datasource:
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/cloud_user?serverTimezone=GMT&characterEncoding=utf-8
mybatis:
#映射文件位置
mapper-locations: classpath:com/os467/mapper/*.xml
#类型别名,为当前包下的类起别名
type-aliases-package: com.os467.orderservice
- 在resource文件下创建mapper文件夹(对应服务的包结构),用来存放写sql的xml文件
引入mapper文件约束
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
整合Feign客户端
创建Feign-api子模块
添加openfeign依赖坐标
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
配置feign信息
feign:
httpclient:
enabled: true #支持httpclient开关
max-connections: 200
max-connections-per-route: 50
创建feign客户端接口
package com.os467.feign.client;
import com.os467.feign.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient("userservice")
public interface UserClient {
@GetMapping("/user/{id}")
User findUserById(@PathVariable("id") Long id);
}
在服务调用端的启动类添加注解
指定在当前服务开启的Feign客户端
package com.os467.orderservice;
import com.os467.feign.client.UserClient;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients(clients = {UserClient.class})
@MapperScan("com.os467.orderservice.mapper")
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}
}
Feign远程调用服务
package com.os467.orderservice.service.impl;
import com.os467.feign.client.UserClient;
import com.os467.feign.pojo.User;
import com.os467.orderservice.mapper.OrderMapper;
import com.os467.orderservice.pojo.Order;
import com.os467.orderservice.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private UserClient userClient;
@Autowired
private OrderMapper orderMapper;
/**
* 获取订单列表
* @return
*/
@Override
public List<Order> getOrderList() {
//获取到订单列表,调用
List<Order> orderList = orderMapper.getOrderList();
//聚合用户信息
for (Order order : orderList) {
User user = userClient.findUserById(order.getUserId());
order.setUser(user);
}
return orderList;
}
}
配置Nacos注册中心
# 应用名称
spring:
cloud:
nacos:
#nacos服务器地址
server-addr: localhost:8848
#暴露本服务
discovery:
#集群名称
cluster-name: ZJ
#是否为临时实例
ephemeral: true
#负载均衡规则,使用nacos注册中心的负载均衡规则
userservice:
ribbon:
NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以邮件至 1300452403@qq.com