SAAS-HRM-day2

  • 1. hrm仓库搭建
    • 1.1 在GitHub创建仓库
    • 1.2 项目初始化
    • 1.3 把项目导入idea,完成版本控制
  • 2. 配置中心搭建
    • 2.1 github配置库
    • 2.2 配置中心服务端
      • 2.2.1 步骤分析
      • 2.2.2 步骤实现
    • 2.3 配置中心客户端-zuul改造
      • 2.3.1 步骤分析
      • 2.3.2 步骤实现
    • 2.4 配置中心客户端-系统管理中心改造
      • 2.4.1 步骤分析
      • 2.4.2 步骤实现
  • 3. 课程中心后台搭建
    • 3.1 步骤分析
    • 3.2 步骤实现
      • 3.2.1 hrm_course_interface配置
      • 3.2.2 hrm_course_service配置
    • 3.3 Swagger配置
      • 3.3.1 本项目swagger配置
      • 3.3.2 网关swagger配置
  • 4. 后台管理前端搭建-课程类型表的crud
    • 4.1 准备工作
    • 4.2 修改前端
      • 4.2.1 步骤分析
      • 4.2.2 步骤实现
  • 5. 坑点注意

1. hrm仓库搭建

1.1 在GitHub创建仓库

1.2 项目初始化

  1. 把项目仓库地址克隆到本地
  2. 拷贝项目原型到本地的克隆文件夹里,添加,提交并推送
  3. 拉取仓库忽略target和idea,添加,提交并推送

1.3 把项目导入idea,完成版本控制

删除原来的项目,从github克隆下来项目

2. 配置中心搭建

2.1 github配置库

在github创建配置仓库 https://github.com/94only/hrm_config.git

2.2 配置中心服务端

2.2.1 步骤分析

  1. 创建服务端项目
  2. 导入jar
  3. application.yml配置
  4. 入口类
  5. 测试

2.2.2 步骤实现

  1. 创建服务端项目

    在二级子模块hrm_support_parent下创建三级子模块hrm_config_server

  2. 导包

        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
            org.springframework.cloud
            spring-cloud-config-server
        
  1. application.yml配置
server:
  port: 8848 # 端口
spring:
  application:
    name: config-server # 起个名字
  cloud:
    config:
      server:
        git:
          uri: https://github.com/94only/hrm_config.git # 配置仓库的地址
          username: [email protected] # github用户名
          password: rootapy06942  # GitHub密码
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka # 注册到eureka的地址
  instance:
    prefer-ip-address: true # 显示真实ip
  1. 入口类
package cn.wangningbo.hrm;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigServer8848Application {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServer8848Application.class, args);
    }
}
  1. 测试

    先启动EurekaServer7001Application,再启动ConfigServer8848Application。然后浏览器访问地址http://localhost:7001/,看到CONFIG-SERVER注册进去了即可算作成功

2.3 配置中心客户端-zuul改造

Eureka,configserver不需要配置,其他的都是客户端

2.3.1 步骤分析

这个时候它的配置应该是从GitHub的配置中心获取

  1. github远程仓库准备好配置zuul的配置文件
  2. 导包
  3. 做配置
  4. 测试

2.3.2 步骤实现

  1. github远程仓库准备好配置zuul的配置文件(注意:eureka的配置不能放上去)
server:
  port: 9527
spring:
  application:
    name: zuul-gateway
zuul:
  routes:
    sysmanage.serviceId: hrm-sysmanage # 服务名
    sysmanage.path: /sysmanage/** # 把myUser打头的所有请求都转发给user-provider
  ignored-services: "*" # 所有服务都不允许以服务名来访问
  prefix: "/services" # 加一个统一前缀
  retryable: true # 是否重试
ribbon:
  ConnectTimeout: 250 # 连接超时时间(ms)
  ReadTimeout: 2000 # 通信超时时间(ms)
  OkToRetryOnAllOperations: true # 是否对所有操作重试
  MaxAutoRetriesNextServer: 2 # 同一服务不同实例的重试次数
  MaxAutoRetries: 1 # 同一实例的重试次数
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMillisecond: 3000 # 熔断超时时长:3000ms
  1. 导包
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
  1. 做配置application.yml
spring:
  profiles:
    active: dev
  cloud:
    config:
#      uri: http://localhost:8848 # 配置configserver单个服务器的时候
      discovery:
        enabled: true
        service-id: config-server # 多个服务器的时候,根据名字从配置中心获取多个服务
      label: master # 分支名字
      name: application-zuul  # 文件名字
      profile: ${spring.profiles.active} # 那个环境
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
  instance:
    # instance-id: gateway-9527.com
    prefer-ip-address: true
  1. 测试

    先启动,在启动,最后启动,如果都没报错,并且注册到了注册中心,端口是指定的9527,即可算作成功!

2.4 配置中心客户端-系统管理中心改造

2.4.1 步骤分析

  1. github远程仓库准备好配置sysmanage的配置文件

application-sysmanage-dev.yml

server:
  port: 9001
spring:
  application:
    name: hrm-sysmanage
  datasource:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/hrm_sysmanage
      username: root
      password: apy06942
mybatis-plus:
  mapper-locations: classpath:cn/wangningbo/hrm/mapper/*Mapper.xml
  type-aliases-package: cn.wangningbo.hrm.domain,cn.wangningbo.hrm.query
  1. 导包
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
  1. 做配置

  1. 测试

2.4.2 步骤实现

3. 课程中心后台搭建

3.1 步骤分析

  1. 创建二级子模块hrm_course_parent

  2. 在二级子模块hrm_course_parent下创建两个三级子模块hrm_course_interface和hrm_course_service

  3. hrm_course_interface配置

    3.1 导入jar
    3.2 表设计
    3.3 代码生成

  4. hrm_course_service配置

    4.1 导入jar
    4.2 配置文件
    4.3 入口类
    4.4 启动测试eureka服务里面有没有
    4.5 配置到配置中心
    4.6 入口类
    4.7 测试
    4.8 配置到网关,实现网关访问
    4.9 postman测试

3.2 步骤实现

  1. 创建二级子模块hrm_course_parent
  2. 在二级子模块hrm_course_parent下创建两个三级子模块hrm_course_interface和hrm_course_service

3.2.1 hrm_course_interface配置

  1. 导入jar
        
            cn.wangningbo.hrm
            hrm_basic_util
            1.0-SNAPSHOT
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            com.baomidou
            mybatis-plus
            2.2.0
        
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
  1. 表设计

  2. 代码生成

    在hrm_course_service配置完以后

3.2.2 hrm_course_service配置

  1. 导入jar
        
        
            cn.wangningbo.hrm
            hrm_course_interface
            1.0-SNAPSHOT
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
            com.baomidou
            mybatis-plus-boot-starter
            2.2.0
        
        
        
            mysql
            mysql-connector-java
        
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
        
            io.springfox
            springfox-swagger2
            2.9.2
        
        
        
            io.springfox
            springfox-swagger-ui
            2.9.2
        
  1. 配置文件
server:
  port: 9002
spring:
  application:
    name: hrm-course
  datasource:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/hrm_course
      username: root
      password: apy06942
mybatis-plus:
  mapper-locations: classpath:cn/wangningbo/hrm/mapper/*Mapper.xml
  type-aliases-package: cn.wangningbo.hrm.domain,cn.wangningbo.hrm.query
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
  instance:
    prefer-ip-address: true
  1. 入口类
  2. 启动测试eureka服务里面有没有
  3. 配置到配置中心
  4. 入口类
package cn.wangningbo.hrm;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
@MapperScan("cn.wangningbo.hrm.mapper")
public class Course9002Application {
    public static void main(String[] args) {
        SpringApplication.run(Course9002Application.class, args);
    }
}
  1. 测试

    访问,查看是否可以拿到数据

  2. 配置到网关,实现网关访问

在网关的配置文件里再zuul里面的routes配置一下

sysmanage.serviceId: hrm-course # 服务名
sysmanage.path: /course/** # 把myUser打头的所有请求都转发给user-provider
  1. postman测试

3.3 Swagger配置

3.3.1 本项目swagger配置

  1. 在service模块里面导入jar
        
        
            io.springfox
            springfox-swagger2
            2.9.2
        
        
        
            io.springfox
            springfox-swagger-ui
            2.9.2
        
  1. 准备swagger所需类
package cn.wangningbo.hrm.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2 {
 
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //对外暴露服务的包,以controller的方式暴露,所以就是controller的包.
                .apis(RequestHandlerSelectors.basePackage("cn.wangningbo.hrm.web.controller"))
                .paths(PathSelectors.any())
                .build();
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("课程中心api")
                .description("课程中心接口文档说明")
                .contact(new Contact("wangningbo", "", "[email protected]"))
                .version("1.0")
                .build();
    }
}

3.3.2 网关swagger配置

在网关的配置那里新加一行代码

resources.add(swaggerResource("课程中心", "/services/course/v2/api-docs", "2.0"));

4. 后台管理前端搭建-课程类型表的crud

4.1 准备工作

准备一个elementui的前端项目,注释掉登录拦截和修改端口,修改项目名字

4.2 修改前端

4.2.1 步骤分析

  1. 写一个coursetype.vue文件,实现增删改查
  2. 配置路由
  3. 配置axios通过网关访问
  4. 配置跨域请求通过
  5. 测试

4.2.2 步骤实现

  1. 写一个coursetype.vue文件,实现增删改查
  2. 配置路由
  3. 配置axios通过网关访问
//配置axios的全局基本路径
axios.defaults.baseURL = 'http://localhost:9527/services/'
  1. 配置跨域请求通过

    在网关中配置跨域请求通过

package cn.wangningbo.hrm.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

//跨域处理
@Configuration
public class GlobalCorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        //1.添加CORS配置信息
        CorsConfiguration config = new CorsConfiguration();
        //1) 允许的域,不要写*,否则cookie就无法使用了
        config.addAllowedOrigin("http://127.0.0.1:6001");
        config.addAllowedOrigin("http://localhost:6001");
        //2) 是否发送Cookie信息
        config.setAllowCredentials(true);
        //3) 允许的请求方式
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");
        // 4)允许的头信息
        config.addAllowedHeader("*");
        //2.添加映射路径,我们拦截一切请求
        UrlBasedCorsConfigurationSource configSource = new
                UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", config);
        //3.返回新的CorsFilter.
        return new CorsFilter(configSource);
    }
}
  1. 测试

5. 坑点注意

  1. github的配置文件编码
  2. idea的编码
  3. 分页那里没拿到总数,要使用分页插件。
//Spring boot方式:配置分页插件
@EnableTransactionManagement
@Configuration
@MapperScan(" cn.wangningbo.hrm.mapper")
public class MybatisPlusConfig {

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

你可能感兴趣的:(SAAS-HRM-day2)