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

一、前言

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

天才在左,疯子在右!

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

二、前期准备

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

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

1、安装MySQL5.7

2、安装nacos

【进击全栈 1】SpringCloud项目起步、nacos、gateway、Redis、mybatis组件搭建_第2张图片

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

3、安装Redis

【进击全栈 1】SpringCloud项目起步、nacos、gateway、Redis、mybatis组件搭建_第3张图片

三、创建父工程

我觉得主要是pom文件



    4.0.0

    com.guor
    GooReeyProject
    1.0-SNAPSHOT
    
        01common
        02gateway
    

    
        1.0.0
        127.0.0.1
        808
        http
        1.8
        Greenwich.SR1
        true
        0.2.2.RELEASE
    

    pom
    GooReeyProject
    This is parent project

    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.projectlombok
            lombok
            provided
        

        
            commons-lang
            commons-lang
            2.6
        

        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.2.5
        
        
            org.springframework.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
        
            com.alibaba
            druid
            1.1.23
        
        
            org.springframework.cloud
            spring-cloud-starter-alibaba-nacos-config
            0.2.1.RELEASE
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
            
                org.springframework.cloud
                spring-cloud-alibaba-dependencies
                ${nacos.version}
                pom
                import
            
        
    

    
        
            
                src/main/resources
                
                    **/*.*
                
            
            
                src/main/resources
                
                    **/*.yml
                
                true
            
            
                src/main/java
                
                    **/*.xml
                
            
        
    

 四、创建gateway子工程

1、pom文件



    
        GooReeyProject
        com.guor
        1.0-SNAPSHOT
    
    4.0.0

    02gateway

    
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.0.1
        
        
            org.springframework.cloud
            spring-cloud-starter-gateway
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix
        
        
            org.springframework.cloud
            spring-cloud-starter-alibaba-nacos-config
        
    

    
        
            
                com.alibaba.cloud
                spring-cloud-starter-alibaba-nacos-discovery
                
                    
                        org.springframework.cloud
                        spring-cloud-starter-netflix-ribbon
                    
                
            
        
    
    
        gateway-${system.version}
    

2、配置文件

spring:
  application:
    name: gateway
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yml
        ext-config:
          - data-id: datasource-share-config.yml
            group: SHARE_GROUP
            refresh: true
          - data-id: log-share-config.yml
            group: SHARE_GROUP
            refresh: true

【进击全栈 1】SpringCloud项目起步、nacos、gateway、Redis、mybatis组件搭建_第4张图片

(1)gateway.yml 

server:
  port: 8080
      
spring:
  application:
    name: gateway
    version: 1.0.0
  cloud:
    gateway:                      
      discovery:
        locator:
          enabled: true
          lowerCaseServiceId: true
          filters: 
            - StripPrefix=1     
      routes:
        - id: management
          uri: lb:management   # 服务端 service_id
          predicates:
            - Path=/management/**
          filters: 
            - name: Hystrix 
              args: 
                name: fallbackcmd
                fallbackUri: forward:/defaultFallback
        - id: demo
          uri: lb://demo
          predicates:
            - Path=/demo/**
            
hystrix:
  command:
    default:
      execution:
        isolation:
          strategy: SEMAPHORE
          thread:
            timeoutInMilliseconds: 1500
  shareSecurityContext: true
                
config: 
  scheduleThreadPool: 5
  restTemplateTimeout: 3000
 
setting: 
  loginAccessPath: /permission
 
login: 
  tokenCheckFrequency: 600000

(2)datasource-share-config.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/blue?serverTimezone=UTC
    driverClassName: com.mysql.cj.jdbc.Driver
    username: root
    password: root
mybatis:
  typeAliasesPackage: com.guor.*.bean.**
  mapperLocations: classpath*:**/com/guor/**/dao/mapping/*Mapper.xml
  configuration:
    map-underscore-to-camel-case: true

(3)log-share-config.yml

logging: 
  path: logs
  level:
    root: info
    com.panasonic.mes: debug
    com.panasonic.mes.editor: debug
    com.alibaba.nacos.client.naming: warn 
  file: 
    max-size: 20MB
    max-history: 30
  pattern: 
    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}"
    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、启动类

package com.guor;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableDiscoveryClient
@EnableScheduling
@RefreshScope
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
        System.out.println("hello world");
    }
}

【进击全栈 1】SpringCloud项目起步、nacos、gateway、Redis、mybatis组件搭建_第5张图片

五、创建management管理模块

1、pom文件



    
        GooReeyProject
        com.guor
        1.0-SNAPSHOT
    
    4.0.0

    03management

    
        8
        8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.0.1
        
        
            org.springframework.cloud
            spring-cloud-starter-alibaba-nacos-config
        
        
        
            mysql
            mysql-connector-java
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
    
    
        management-${system.version}
    

2、配置文件

spring:
  application:
    name: management
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yml
        ext-config:
          - data-id: datasource-share-config.yml
            group: SHARE_GROUP
            refresh: true
          - data-id: log-share-config.yml
            group: SHARE_GROUP
            refresh: true
server:
  port: 8081
 
spring:
  application:
    name: management
    version: 1.0.0
  mvc:
   static-path-pattern: /management/**
  resources:
    static-locations: 
    - file:../../web/management
    - file:../../web/common

3、启动类

package com.guor;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;

@EnableDiscoveryClient
@SpringBootApplication(scanBasePackages = "com.guor")
@MapperScan("com.guor.management.dao")
@RefreshScope
public class ManagementApplication {

    public static void main(String[] args) {
        SpringApplication.run(ManagementApplication.class, args);
    }

}

【进击全栈 1】SpringCloud项目起步、nacos、gateway、Redis、mybatis组件搭建_第6张图片

六、整合mybatis

1、user表设计

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

CREATE TABLE `user` (
  `user_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(100) NOT NULL,
  `password` varchar(40) NOT NULL,
  `age` int(11) DEFAULT NULL,
  `sex` int(11) DEFAULT NULL,
  `telephone` varchar(100) DEFAULT NULL,
  `address` varchar(100) DEFAULT NULL,
  `create_date` date DEFAULT NULL,
  `update_date` date DEFAULT NULL,
  `deleted` int(11) DEFAULT NULL,
  `version` int(11) DEFAULT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

2、UserController

package com.guor.management.controller;

import com.guor.management.bean.User;
import com.guor.management.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/getUserList")
    public List getUserList(){
        return userService.getUserList();
    }

    @PutMapping("/insertUser")
    public void insertUser(@RequestBody User user){
        userService.insertUser(user);
    }
}

3、UserService

package com.guor.management.service;

import com.guor.management.bean.User;

import java.util.List;

public interface UserService {
    List getUserList();
    void insertUser(User user);
}
package com.guor.management.service.impl;

import com.guor.management.bean.User;
import com.guor.management.dao.UserMapper;
import com.guor.management.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List getUserList() {
        return userMapper.getUserList();
    }

    @Override
    public void insertUser(User user) {
        userMapper.insertUser(user);
    }
}

4、UserMapper

package com.guor.management.dao;

import com.guor.management.bean.User;

import java.util.List;

public interface UserMapper {
    public List getUserList();

    public void insertUser(User user);
}



    

    
        INSERT INTO gooreey.`user` (username, password) VALUES (#{username}, #{password});
    

5、User

package com.guor.management.bean;

import com.guor.base.bean.BaseBean;
import lombok.Data;

@Data
public class User extends BaseBean {
   private Integer userId;
   private String username;
   private String password;
   private Integer age;
   private Integer sex;
   private String telephone;
   private String address;
}

package com.guor.base.bean;

import lombok.Data;

import java.util.Date;

@Data
public class BaseBean {
    private Date createDate;
    private Date updateDate;
    private Integer deleted;
    private Integer version;
}

6、postman接口测试

【进击全栈 1】SpringCloud项目起步、nacos、gateway、Redis、mybatis组件搭建_第7张图片

【进击全栈 1】SpringCloud项目起步、nacos、gateway、Redis、mybatis组件搭建_第8张图片

 

 

往期精彩内容:

Java知识体系总结(2021版)

Java多线程基础知识总结

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

超详细的springBoot学习笔记

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

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

Java面试题总结(附答案)

 

你可能感兴趣的:(Spring,Cloud)