飞道的博客

【进击全栈 1】SpringCloud项目起步、nacos、gateway、Redis、mybatis组件搭建

272人阅读  评论(0)

一、前言

一直以来都有一个想法,那就是成为一个全栈工程师,一个架构师,5月1日,终于迈出了第一步,买了一台适合开发的笔记本,小新Pro16酷睿版(锐龙版没有秒杀到),i5-1300H,16G内存,512固态,MX450显卡,在6000这个价位也算是高配了,好了,开始编码吧,这次没有借口了,万事俱备只欠东风,挑战高薪,挑战全栈,我来了。

天才在左,疯子在右!

二、前期准备

本项目暂定项目名GooReeyProject,SpringBoot + Vue构建,具体项目内容未定。

基本架构nacos、gateway、Linux、Redis、rabbitMQ、MySQL、docker、Vue。

1、安装MySQL5.7

2、安装nacos

我安装的是window版的nacos和MySQL,安装nacos时需要注意,要讲配置文件中的集群版改为单机版,才能启动!

3、安装Redis

三、创建父工程

我觉得主要是pom文件


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0 </modelVersion>
  6. <groupId>com.guor </groupId>
  7. <artifactId>GooReeyProject </artifactId>
  8. <version>1.0-SNAPSHOT </version>
  9. <modules>
  10. <module>01common </module>
  11. <module>02gateway </module>
  12. </modules>
  13. <properties>
  14. <system.version>1.0.0 </system.version>
  15. <system.ip>127.0.0.1 </system.ip>
  16. <system.sport>808 </system.sport>
  17. <system.mode>http </system.mode>
  18. <java.version>1.8 </java.version>
  19. <spring-cloud.version>Greenwich.SR1 </spring-cloud.version>
  20. <skipTests>true </skipTests>
  21. <nacos.version>0.2.2.RELEASE </nacos.version>
  22. </properties>
  23. <packaging>pom </packaging>
  24. <name>GooReeyProject </name>
  25. <description>This is parent project </description>
  26. <!-- 本项目的父模块使用spring-boot框架 -->
  27. <parent>
  28. <groupId>org.springframework.boot </groupId>
  29. <artifactId>spring-boot-starter-parent </artifactId>
  30. <version>2.1.4.RELEASE </version>
  31. </parent>
  32. <dependencies>
  33. <dependency>
  34. <groupId>org.springframework.boot </groupId>
  35. <artifactId>spring-boot-starter-test </artifactId>
  36. <scope>test </scope>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.projectlombok </groupId>
  40. <artifactId>lombok </artifactId>
  41. <scope>provided </scope>
  42. </dependency>
  43. <dependency>
  44. <groupId>commons-lang </groupId>
  45. <artifactId>commons-lang </artifactId>
  46. <version>2.6 </version>
  47. </dependency>
  48. <dependency>
  49. <groupId>com.github.pagehelper </groupId>
  50. <artifactId>pagehelper-spring-boot-starter </artifactId>
  51. <version>1.2.5 </version>
  52. </dependency>
  53. <dependency>
  54. <groupId>org.springframework.cloud </groupId>
  55. <artifactId>spring-cloud-starter-alibaba-nacos-discovery </artifactId>
  56. </dependency>
  57. <dependency>
  58. <groupId>com.alibaba </groupId>
  59. <artifactId>druid </artifactId>
  60. <version>1.1.23 </version>
  61. </dependency>
  62. <dependency>
  63. <groupId>org.springframework.cloud </groupId>
  64. <artifactId>spring-cloud-starter-alibaba-nacos-config </artifactId>
  65. <version>0.2.1.RELEASE </version>
  66. </dependency>
  67. </dependencies>
  68. <dependencyManagement>
  69. <dependencies>
  70. <dependency>
  71. <groupId>org.springframework.cloud </groupId>
  72. <artifactId>spring-cloud-dependencies </artifactId>
  73. <version>${spring-cloud.version} </version>
  74. <type>pom </type>
  75. <scope>import </scope>
  76. </dependency>
  77. <dependency>
  78. <groupId>org.springframework.cloud </groupId>
  79. <artifactId>spring-cloud-alibaba-dependencies </artifactId>
  80. <version>${nacos.version} </version>
  81. <type>pom </type>
  82. <scope>import </scope>
  83. </dependency>
  84. </dependencies>
  85. </dependencyManagement>
  86. <build>
  87. <resources>
  88. <resource>
  89. <directory>src/main/resources </directory>
  90. <includes>
  91. <include>**/*.* </include>
  92. </includes>
  93. </resource>
  94. <resource>
  95. <directory>src/main/resources </directory>
  96. <includes>
  97. <include>**/*.yml </include>
  98. </includes>
  99. <filtering>true </filtering>
  100. </resource>
  101. <resource>
  102. <directory>src/main/java </directory>
  103. <includes>
  104. <include>**/*.xml </include>
  105. </includes>
  106. </resource>
  107. </resources>
  108. </build>
  109. </project>

 四、创建gateway子工程

1、pom文件


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>GooReeyProject </artifactId>
  7. <groupId>com.guor </groupId>
  8. <version>1.0-SNAPSHOT </version>
  9. </parent>
  10. <modelVersion>4.0.0 </modelVersion>
  11. <artifactId>02gateway </artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.mybatis.spring.boot </groupId>
  15. <artifactId>mybatis-spring-boot-starter </artifactId>
  16. <version>2.0.1 </version>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.cloud </groupId>
  20. <artifactId>spring-cloud-starter-gateway </artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.cloud </groupId>
  24. <artifactId>spring-cloud-starter-netflix-hystrix </artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.cloud </groupId>
  28. <artifactId>spring-cloud-starter-alibaba-nacos-config </artifactId>
  29. </dependency>
  30. </dependencies>
  31. <dependencyManagement>
  32. <dependencies>
  33. <dependency>
  34. <groupId>com.alibaba.cloud </groupId>
  35. <artifactId>spring-cloud-starter-alibaba-nacos-discovery </artifactId>
  36. <exclusions>
  37. <exclusion>
  38. <groupId>org.springframework.cloud </groupId>
  39. <artifactId>spring-cloud-starter-netflix-ribbon </artifactId>
  40. </exclusion>
  41. </exclusions>
  42. </dependency>
  43. </dependencies>
  44. </dependencyManagement>
  45. <build>
  46. <finalName>gateway-${system.version} </finalName>
  47. </build>
  48. </project>

2、配置文件


  
  1. spring:
  2. application:
  3. name: gateway
  4. cloud:
  5. nacos:
  6. discovery:
  7. server-addr: 127.0. 0.1: 8848
  8. config:
  9. server-addr: 127.0. 0.1: 8848
  10. file-extension: yml
  11. ext-config:
  12. - data-id: datasource-share-config.yml
  13. group: SHARE_GROUP
  14. refresh: true
  15. - data-id: log-share-config.yml
  16. group: SHARE_GROUP
  17. refresh: true

(1)gateway.yml 


  
  1. server:
  2. port: 8080
  3. spring:
  4. application:
  5. name: gateway
  6. version: 1.0. 0
  7. cloud:
  8. gateway:
  9. discovery:
  10. locator:
  11. enabled: true
  12. lowerCaseServiceId: true
  13. filters:
  14. - StripPrefix= 1
  15. routes:
  16. - id: management
  17. uri: lb:management # 服务端 service_id
  18. predicates:
  19. - Path=/management /**
  20. filters:
  21. - name: Hystrix
  22. args:
  23. name: fallbackcmd
  24. fallbackUri: forward:/defaultFallback
  25. - id: demo
  26. uri: lb://demo
  27. predicates:
  28. - Path=/demo/**
  29. hystrix:
  30. command:
  31. default:
  32. execution:
  33. isolation:
  34. strategy: SEMAPHORE
  35. thread:
  36. timeoutInMilliseconds: 1500
  37. shareSecurityContext: true
  38. config:
  39. scheduleThreadPool: 5
  40. restTemplateTimeout: 3000
  41. setting:
  42. loginAccessPath: /permission
  43. login:
  44. tokenCheckFrequency: 600000

(2)datasource-share-config.yml


  
  1. spring:
  2. datasource:
  3. url: jdbc:mysql: //localhost:3306/blue?serverTimezone=UTC
  4. driverClassName: com.mysql.cj.jdbc.Driver
  5. username: root
  6. password: root
  7. mybatis:
  8. typeAliasesPackage: com.guor.*.bean.**
  9. mapperLocations: classpath*:**/com/guor /**/dao/mapping/*Mapper.xml
  10. configuration:
  11. map-underscore-to-camel-case: true

(3)log-share-config.yml


  
  1. logging:
  2. path: logs
  3. level:
  4. root: info
  5. com.panasonic.mes: debug
  6. com.panasonic.mes.editor: debug
  7. com.alibaba.nacos.client.naming: warn
  8. file:
  9. max-size: 20MB
  10. max-history: 30
  11. pattern:
  12. file: "%d{${LOG_DATEFORMAT_PATTERN:yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:%5p} ${PID:- } --- [%15.15t] %-40.40logger{39} [%5.5line] : %m%n${LOG_EXCEPTION_CONVERSION_WORD:%wEx}"
  13. console: "%d{${LOG_DATEFORMAT_PATTERN:yyyy-MM-dd HH:mm:ss.SSS}} %clr(${LOG_LEVEL_PATTERN:%5p}) %clr(${PID:- }){magenta} --- %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr([%5.5line]){cyan} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:%wEx}"

3、启动类


  
  1. package com.guor;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
  5. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6. import org.springframework.cloud.context.config.annotation.RefreshScope;
  7. import org.springframework.scheduling.annotation.EnableScheduling;
  8. @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
  9. @EnableDiscoveryClient
  10. @EnableScheduling
  11. @RefreshScope
  12. public class GatewayApplication {
  13. public static void main(String[] args) {
  14. SpringApplication.run(GatewayApplication.class, args);
  15. System.out.println("hello world");
  16. }
  17. }

五、创建management管理模块

1、pom文件


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>GooReeyProject </artifactId>
  7. <groupId>com.guor </groupId>
  8. <version>1.0-SNAPSHOT </version>
  9. </parent>
  10. <modelVersion>4.0.0 </modelVersion>
  11. <artifactId>03management </artifactId>
  12. <properties>
  13. <maven.compiler.source>8 </maven.compiler.source>
  14. <maven.compiler.target>8 </maven.compiler.target>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>org.springframework.boot </groupId>
  19. <artifactId>spring-boot-starter-web </artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.mybatis.spring.boot </groupId>
  23. <artifactId>mybatis-spring-boot-starter </artifactId>
  24. <version>2.0.1 </version>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.cloud </groupId>
  28. <artifactId>spring-cloud-starter-alibaba-nacos-config </artifactId>
  29. </dependency>
  30. <!--MySQL依赖 -->
  31. <dependency>
  32. <groupId>mysql </groupId>
  33. <artifactId>mysql-connector-java </artifactId>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.boot </groupId>
  37. <artifactId>spring-boot-starter-jdbc </artifactId>
  38. </dependency>
  39. </dependencies>
  40. <build>
  41. <finalName>management-${system.version} </finalName>
  42. </build>
  43. </project>

2、配置文件


  
  1. spring:
  2. application:
  3. name: management
  4. cloud:
  5. nacos:
  6. discovery:
  7. server-addr: 127.0.0.1:8848
  8. config:
  9. server-addr: 127.0.0.1:8848
  10. file-extension: yml
  11. ext-config:
  12. - data-id: datasource-share-config.yml
  13. group: SHARE_GROUP
  14. refresh: true
  15. - data-id: log-share-config.yml
  16. group: SHARE_GROUP
  17. refresh: true

  
  1. server:
  2. port: 8081
  3. spring:
  4. application:
  5. name: management
  6. version: 1.0.0
  7. mvc:
  8. static-path-pattern: /management/**
  9. resources:
  10. static-locations:
  11. - file:../../web/management
  12. - file:../../web/common

3、启动类


  
  1. package com.guor;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6. import org.springframework.cloud.context.config.annotation.RefreshScope;
  7. @EnableDiscoveryClient
  8. @SpringBootApplication(scanBasePackages = "com.guor")
  9. @MapperScan("com.guor.management.dao")
  10. @RefreshScope
  11. public class ManagementApplication {
  12. public static void main(String[] args) {
  13. SpringApplication.run(ManagementApplication.class, args);
  14. }
  15. }

六、整合mybatis

1、user表设计

数据库选择的是最常用的MySQL


  
  1. CREATE TABLE `user` (
  2. `user_id` int( 10) unsigned NOT NULL AUTO_INCREMENT,
  3. `username` varchar( 100) NOT NULL,
  4. `password` varchar( 40) NOT NULL,
  5. `age` int( 11) DEFAULT NULL,
  6. `sex` int( 11) DEFAULT NULL,
  7. `telephone` varchar( 100) DEFAULT NULL,
  8. `address` varchar( 100) DEFAULT NULL,
  9. `create_date` date DEFAULT NULL,
  10. `update_date` date DEFAULT NULL,
  11. `deleted` int( 11) DEFAULT NULL,
  12. `version` int( 11) DEFAULT NULL,
  13. PRIMARY KEY ( `user_id`)
  14. ) ENGINE= InnoDB AUTO_INCREMENT= 3 DEFAULT CHARSET=utf8;

2、UserController


  
  1. package com.guor.management.controller;
  2. import com.guor.management.bean.User;
  3. import com.guor.management.service.UserService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.PutMapping;
  7. import org.springframework.web.bind.annotation.RequestBody;
  8. import org.springframework.web.bind.annotation.RestController;
  9. import java.util.List;
  10. @RestController
  11. public class UserController {
  12. @Autowired
  13. private UserService userService;
  14. @GetMapping("/getUserList")
  15. public List<User> getUserList(){
  16. return userService.getUserList();
  17. }
  18. @PutMapping("/insertUser")
  19. public void insertUser(@RequestBody User user){
  20. userService.insertUser(user);
  21. }
  22. }

3、UserService


  
  1. package com.guor.management.service;
  2. import com.guor.management.bean.User;
  3. import java.util.List;
  4. public interface UserService {
  5. List<User> getUserList();
  6. void insertUser(User user);
  7. }

  
  1. package com.guor.management.service.impl;
  2. import com.guor.management.bean.User;
  3. import com.guor.management.dao.UserMapper;
  4. import com.guor.management.service.UserService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import java.util.List;
  8. @Service
  9. public class UserServiceImpl implements UserService {
  10. @Autowired
  11. private UserMapper userMapper;
  12. @Override
  13. public List<User> getUserList() {
  14. return userMapper.getUserList();
  15. }
  16. @Override
  17. public void insertUser(User user) {
  18. userMapper.insertUser(user);
  19. }
  20. }

4、UserMapper


  
  1. package com.guor.management.dao;
  2. import com.guor.management.bean.User;
  3. import java.util.List;
  4. public interface UserMapper {
  5. public List<User> getUserList();
  6. public void insertUser(User user);
  7. }

  
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="com.guor.management.dao.UserMapper">
  4. <select id="getUserList" resultType="java.util.LinkedHashMap">
  5. select * from user;
  6. </select>
  7. <insert id="insertUser" parameterType="com.guor.management.bean.User">
  8. INSERT INTO gooreey.`user` (username, password) VALUES (#{username}, #{password});
  9. </insert>
  10. </mapper>

5、User


  
  1. package com.guor.management.bean;
  2. import com.guor.base.bean.BaseBean;
  3. import lombok.Data;
  4. @Data
  5. public class User extends BaseBean {
  6. private Integer userId;
  7. private String username;
  8. private String password;
  9. private Integer age;
  10. private Integer sex;
  11. private String telephone;
  12. private String address;
  13. }

  
  1. package com.guor.base.bean;
  2. import lombok.Data;
  3. import java.util.Date;
  4. @Data
  5. public class BaseBean {
  6. private Date createDate;
  7. private Date updateDate;
  8. private Integer deleted;
  9. private Integer version;
  10. }

6、postman接口测试

 

 

往期精彩内容:

Java知识体系总结(2021版)

Java多线程基础知识总结

【全栈最全Java框架总结】SSH、SSM、Springboot

超详细的springBoot学习笔记

常见数据结构与算法整理总结

Java设计模式:23种设计模式全面解析

Java面试题总结(附答案)

 


转载:https://blog.csdn.net/guorui_java/article/details/116465118
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场