项目使用技术:mybatis,redis,springboot,springMVC,Nacos,Feign,GateWay,rabbitmq
xxxxxxxxxx
861
2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5 <groupId>com.os467</groupId>
6 <artifactId>demo</artifactId>
7 <version>0.0.1-SNAPSHOT</version>
8 <name>demo</name>
9 <description>Demo project for Spring Cloud</description>
10
11 <!--指定为pom打包方式-->
12 <packaging>pom</packaging>
13
14 <properties>
15 <!--指定jdk版本-->
16 <java.version>1.8</java.version>
17 <!--指定编码格式-->
18 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
20 <!--SpringBoot版本-->
21 <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
22 <!--SpringCloud版本-->
23 <spring-cloud.version>Hoxton.SR9</spring-cloud.version>
24 <!--Mysql版本-->
25 <mysql.version>8.0.22</mysql.version>
26 <!--Mybatis版本-->
27 <mybatis.version>2.1.1</mybatis.version>
28 </properties>
29
30 <parent>
31 <groupId>org.springframework.boot</groupId>
32 <artifactId>spring-boot-starter-parent</artifactId>
33 <version>2.3.9.RELEASE</version>
34 <relativePath/>
35 </parent>
36
37 <modules>
38 <module>order-service</module>
39 </modules>
40
41 <dependencyManagement>
42 <dependencies>
43 <!--nacos管理依赖-->
44 <dependency>
45 <groupId>com.alibaba.cloud</groupId>
46 <artifactId>spring-cloud-alibaba-dependencies</artifactId>
47 <version>2.2.6.RELEASE</version>
48 <type>pom</type>
49 <scope>import</scope>
50 </dependency>
51
52 <!-- springCloud -->
53 <dependency>
54 <groupId>org.springframework.cloud</groupId>
55 <artifactId>spring-cloud-dependencies</artifactId>
56 <version>${spring-cloud.version}</version>
57 <type>pom</type>
58 <scope>import</scope>
59 </dependency>
60
61 <!-- mysql驱动 -->
62 <dependency>
63 <groupId>mysql</groupId>
64 <artifactId>mysql-connector-java</artifactId>
65 <version>${mysql.version}</version>
66 </dependency>
67
68 <!--mybatis-->
69 <dependency>
70 <groupId>org.mybatis.spring.boot</groupId>
71 <artifactId>mybatis-spring-boot-starter</artifactId>
72 <version>${mybatis.version}</version>
73 </dependency>
74
75 </dependencies>
76 </dependencyManagement>
77
78 <!--lombok依赖-->
79 <dependencies>
80 <dependency>
81 <groupId>org.projectlombok</groupId>
82 <artifactId>lombok</artifactId>
83 </dependency>
84 </dependencies>
85
86</project>
xxxxxxxxxx
611
2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5 <artifactId>order-service</artifactId>
6
7 <parent>
8 <groupId>com.os467</groupId>
9 <artifactId>demo</artifactId>
10 <version>0.0.1-SNAPSHOT</version>
11 </parent>
12
13 <dependencies>
14 <!--feign客户端依赖-->
15 <dependency>
16 <groupId>org.springframework.cloud</groupId>
17 <artifactId>spring-cloud-starter-openfeign</artifactId>
18 </dependency>
19
20 <!--httpClient的依赖-->
21 <dependency>
22 <groupId>io.github.openfeign</groupId>
23 <artifactId>feign-httpclient</artifactId>
24 </dependency>
25
26 <!-- nacos客户端依赖 -->
27 <dependency>
28 <groupId>com.alibaba.cloud</groupId>
29 <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
30 </dependency>
31
32 <!--SpringWeb-->
33 <dependency>
34 <groupId>org.springframework.boot</groupId>
35 <artifactId>spring-boot-starter-web</artifactId>
36 </dependency>
37
38 <!--mysql-->
39 <dependency>
40 <groupId>mysql</groupId>
41 <artifactId>mysql-connector-java</artifactId>
42 </dependency>
43
44 <!--mybatis-->
45 <dependency>
46 <groupId>org.mybatis.spring.boot</groupId>
47 <artifactId>mybatis-spring-boot-starter</artifactId>
48 </dependency>
49
50 </dependencies>
51
52 <build>
53 <plugins>
54 <plugin>
55 <groupId>org.springframework.boot</groupId>
56 <artifactId>spring-boot-maven-plugin</artifactId>
57 </plugin>
58 </plugins>
59 </build>
60
61</project>
创建工程选择springboot,清除父工程src,pom.xml内容
引入约束
xxxxxxxxxx
91
2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5 <groupId>com.os467</groupId>
6 <artifactId>demo</artifactId>
7 <version>0.0.1-SNAPSHOT</version>
8 <name>demo</name>
9 <description>Demo project for Spring Cloud</description>
指定父工程打包方式
xxxxxxxxxx
21<!--指定为pom打包方式-->
2<packaging>pom</packaging>
引入版本参数
xxxxxxxxxx
151<properties>
2 <!--指定jdk版本-->
3 <java.version>1.8</java.version>
4 <!--指定编码格式-->
5 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
6 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
7 <!--SpringBoot版本-->
8 <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
9 <!--SpringCloud版本-->
10 <spring-cloud.version>Hoxton.SR9</spring-cloud.version>
11 <!--Mysql版本-->
12 <mysql.version>8.0.22</mysql.version>
13 <!--Mybatis版本-->
14 <mybatis.version>2.1.1</mybatis.version>
15</properties>
继承一个springboot父工程
xxxxxxxxxx
61<parent>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-starter-parent</artifactId>
4 <version>2.3.9.RELEASE</version>
5 <relativePath/>
6</parent>
<dependencyManagement>
标签
在dependencyManagement元素中声明所依赖的jar包的版本号等信息,那么所有子项目再次引入此依赖jar包时则无需显式的列出版本号
xxxxxxxxxx
361<dependencyManagement>
2 <dependencies>
3 <!--nacos管理依赖-->
4 <dependency>
5 <groupId>com.alibaba.cloud</groupId>
6 <artifactId>spring-cloud-alibaba-dependencies</artifactId>
7 <version>2.2.6.RELEASE</version>
8 <type>pom</type>
9 <scope>import</scope>
10 </dependency>
11
12 <!-- springCloud -->
13 <dependency>
14 <groupId>org.springframework.cloud</groupId>
15 <artifactId>spring-cloud-dependencies</artifactId>
16 <version>${spring-cloud.version}</version>
17 <type>pom</type>
18 <scope>import</scope>
19 </dependency>
20
21 <!-- mysql驱动 -->
22 <dependency>
23 <groupId>mysql</groupId>
24 <artifactId>mysql-connector-java</artifactId>
25 <version>${mysql.version}</version>
26 </dependency>
27
28 <!--mybatis-->
29 <dependency>
30 <groupId>org.mybatis.spring.boot</groupId>
31 <artifactId>mybatis-spring-boot-starter</artifactId>
32 <version>${mybatis.version}</version>
33 </dependency>
34
35 </dependencies>
36</dependencyManagement>
添加lombok组件依赖
xxxxxxxxxx
71<!--lombok依赖-->
2<dependencies>
3 <dependency>
4 <groupId>org.projectlombok</groupId>
5 <artifactId>lombok</artifactId>
6 </dependency>
7</dependencies>
在父工程pom文件中添加模块
xxxxxxxxxx
31<modules>
2 <module>order-service</module>
3</modules>
选择父工程添加模块,创建maven工程,引入约束
xxxxxxxxxx
31
2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
继承父工程<parent>标签
引入父pom里面的依赖时无须指定版本
xxxxxxxxxx
51<parent>
2 <groupId>com.os467</groupId>
3 <artifactId>demo</artifactId>
4 <version>0.0.1-SNAPSHOT</version>
5</parent>
声明当前工程名称
xxxxxxxxxx
31<modelVersion>4.0.0</modelVersion>
2
3<artifactId>order-service</artifactId>
添加所需依赖
xxxxxxxxxx
381<dependencies>
2 <!--feign客户端依赖-->
3 <dependency>
4 <groupId>org.springframework.cloud</groupId>
5 <artifactId>spring-cloud-starter-openfeign</artifactId>
6 </dependency>
7
8 <!--httpClient的依赖-->
9 <dependency>
10 <groupId>io.github.openfeign</groupId>
11 <artifactId>feign-httpclient</artifactId>
12 </dependency>
13
14 <!-- nacos客户端依赖 -->
15 <dependency>
16 <groupId>com.alibaba.cloud</groupId>
17 <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
18 </dependency>
19
20 <!--SpringWeb-->
21 <dependency>
22 <groupId>org.springframework.boot</groupId>
23 <artifactId>spring-boot-starter-web</artifactId>
24 </dependency>
25
26 <!--mysql-->
27 <dependency>
28 <groupId>mysql</groupId>
29 <artifactId>mysql-connector-java</artifactId>
30 </dependency>
31
32 <!--mybatis-->
33 <dependency>
34 <groupId>org.mybatis.spring.boot</groupId>
35 <artifactId>mybatis-spring-boot-starter</artifactId>
36 </dependency>
37
38</dependencies>
添加spring boot提供的maven打包插件
<finalName>
标签 打包成jar包的名字
xxxxxxxxxx
91<build>
2 <finalName>application</finalName>
3 <plugins>
4 <plugin>
5 <groupId>org.springframework.boot</groupId>
6 <artifactId>spring-boot-maven-plugin</artifactId>
7 </plugin>
8 </plugins>
9</build>
创建application.yaml文件
xxxxxxxxxx
41# 应用名称
2spring
3 application
4 name orderservice
创建启动类
业务相关的类需要写在和启动类同级的包下
xxxxxxxxxx
131package com.os467.orderservice;
2
3import org.springframework.boot.SpringApplication;
4import org.springframework.boot.autoconfigure.SpringBootApplication;
5
6
7public class OrderServiceApplication {
8
9 public static void main(String[] args) {
10 SpringApplication.run(OrderServiceApplication.class, args);
11 }
12
13}
为微服务搭建包结构
在父pom文件中添加依赖,让所有子模块继承
xxxxxxxxxx
51<!--freemarker模板引擎-->
2<dependency>
3 <groupId>org.springframework.boot</groupId>
4 <artifactId>spring-boot-starter-freemarker</artifactId>
5</dependency>
在子模块启动类配置文件中配置视图后缀和模板文件位置
xxxxxxxxxx
101# 应用名称
2spring
3 application
4 name orderservice
5 #freemarker配置
6 freemarker
7 #配置模板文件位置
8 template-loader-path classpath /templates/
9 #视图后缀
10 suffix .ftl
xxxxxxxxxx
161<html>
2<head>
3
4</head>
5
6<body>
7
8<#--通过freemarker来遍历集合中的对象-->
9<#list orderList as order >
10
11 ${order ! "null"}
12
13</#list>
14
15</body>
16</html>
配置mysql数据源信息,用来连接数据库
xxxxxxxxxx
231# 应用名称
2spring
3 application
4 name orderservice
5 #freemarker配置
6 freemarker
7 #配置模板文件位置
8 template-loader-path classpath /templates/
9 #视图后缀
10 suffix .ftl
11
12 #数据源信息
13 datasource
14 username root
15 password root
16 driver-class-name com.mysql.cj.jdbc.Driver
17 url jdbc mysql //localhost 3306/cloud_user?serverTimezone=GMT&characterEncoding=utf-8
18
19mybatis
20 #映射文件位置
21 mapper-locations classpath com/os467/mapper/*.xml
22 #类型别名,为当前包下的类起别名
23 type-aliases-package com.os467.orderservice
引入mapper文件约束
xxxxxxxxxx
41
2
3
4
创建Feign-api子模块
添加openfeign依赖坐标
xxxxxxxxxx
61<dependencies>
2 <dependency>
3 <groupId>org.springframework.cloud</groupId>
4 <artifactId>spring-cloud-starter-openfeign</artifactId>
5 </dependency>
6</dependencies>
配置feign信息
xxxxxxxxxx
51feign
2 httpclient
3 enabled true #支持httpclient开关
4 max-connections200
5 max-connections-per-route50
创建feign客户端接口
xxxxxxxxxx
141package com.os467.feign.client;
2
3import com.os467.feign.pojo.User;
4import org.springframework.cloud.openfeign.FeignClient;
5import org.springframework.web.bind.annotation.GetMapping;
6import org.springframework.web.bind.annotation.PathVariable;
7
8"userservice") (
9public interface UserClient {
10
11 "/user/{id}") (
12 User findUserById( ("id") Long id);
13
14}
在服务调用端的启动类添加注解
指定在当前服务开启的Feign客户端
xxxxxxxxxx
181package com.os467.orderservice;
2
3import com.os467.feign.client.UserClient;
4import org.mybatis.spring.annotation.MapperScan;
5import org.springframework.boot.SpringApplication;
6import org.springframework.boot.autoconfigure.SpringBootApplication;
7import org.springframework.cloud.openfeign.EnableFeignClients;
8
9
10clients = {UserClient.class}) (
11"com.os467.orderservice.mapper") (
12public class OrderServiceApplication {
13
14 public static void main(String[] args) {
15 SpringApplication.run(OrderServiceApplication.class, args);
16 }
17
18}
Feign远程调用服务
xxxxxxxxxx
451package com.os467.orderservice.service.impl;
2
3import com.os467.feign.client.UserClient;
4import com.os467.feign.pojo.User;
5import com.os467.orderservice.mapper.OrderMapper;
6import com.os467.orderservice.pojo.Order;
7import com.os467.orderservice.service.OrderService;
8import org.springframework.beans.factory.annotation.Autowired;
9import org.springframework.stereotype.Service;
10
11import java.util.List;
12
13
14public class OrderServiceImpl implements OrderService {
15
16
17 private UserClient userClient;
18
19
20 private OrderMapper orderMapper;
21
22 /**
23 * 获取订单列表
24 * @return
25 */
26
27 public List<Order> getOrderList() {
28
29 //获取到订单列表,调用
30 List<Order> orderList = orderMapper.getOrderList();
31
32 //聚合用户信息
33 for (Order order : orderList) {
34
35 User user = userClient.findUserById(order.getUserId());
36
37 order.setUser(user);
38
39 }
40
41 return orderList;
42 }
43
44
45}
配置Nacos注册中心
xxxxxxxxxx
171# 应用名称
2spring
3 cloud
4 nacos
5 #nacos服务器地址
6 server-addr localhost8848
7 #暴露本服务
8 discovery
9 #集群名称
10 cluster-name ZJ
11 #是否为临时实例
12 ephemeraltrue
13
14#负载均衡规则,使用nacos注册中心的负载均衡规则
15userservice
16 ribbon
17 NFLoadBalancerRuleClassName com.alibaba.cloud.nacos.ribbon.NacosRule