微服务搭建

搭建微服务

1.项目结构

在这里插入图片描述

2.代码生成器

1.创建代码生成器的模块 hrm-code-generate

2.导入jar包

 <!--mybatis-plus依赖包-->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>2.2.0</version>
            </dependency>

       <!--模板引擎-->
            <dependency>
                <groupId>org.apache.velocity</groupId>
                <artifactId>velocity-engine-core</artifactId>
                <version>2.0</version>
            </dependency>
        <!--连接池-->
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
       </dependency>

3.创建代码生成器配置文件: mybatiesplus-config.properties , 只能根据自己的项目路径进行配置

#此处为本项目src所在路径(代码生成器输出路径),注意一定是当前项目所在的目录哟
#mapper,servier,controller输出目录
OutputDir=D:/java/iderproject/hrm-code/hrm-parent/hrm-course-parent/hrm-course-service-2020/src/main/java

#mapper.xml SQL映射文件目录
OutputDirXml=D:/java/iderproject/hrm-code/hrm-parent/hrm-course-parent/hrm-course-service-2020/src/main/resources

#domain,query输出的目录
OutputDirBase=D:/java/iderproject/hrm-code/hrm-parent/hrm-course-parent/hrm-course-common/src/main/java
#设置作者
author=canola
#自定义包路径
parent=cn.itsource.course

#数据库连接信息
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///hrm-course
jdbc.user=root
jdbc.pwd=123456

4.代码生成类:GenteratorCode

//代码生成的主类
public class GenteratorCode {

    //运行main方法就可以生成代码了
    public static void main(String[] args) throws InterruptedException {
        //用来获取Mybatis-Plus.properties文件的配置信息
        //不要加后缀//不要加后缀//不要加后缀//不要加后缀//不要加后缀//不要加后缀//不要加后缀//不要加后缀//不要加后缀//不要加后缀
        final ResourceBundle rb = ResourceBundle.getBundle("mybatiesplus-config-course");
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(rb.getString("OutputDir"));
        gc.setFileOverride(true);
        gc.setActiveRecord(true);// 开启 activeRecord 模式
        gc.setEnableCache(false);// XML 二级缓存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(false);// XML columList
        gc.setAuthor(rb.getString("author"));
        mpg.setGlobalConfig(gc);
        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setTypeConvert(new MySqlTypeConvert());
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername(rb.getString("jdbc.user"));
        dsc.setPassword(rb.getString("jdbc.pwd"));
        dsc.setUrl(rb.getString("jdbc.url"));
        mpg.setDataSource(dsc);
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setTablePrefix(new String[] { "t_" });// 此处可以修改为您的表前缀
        strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
        strategy.setInclude(new String[]{"t_course_type"}); // 需要生成的表 :
        mpg.setStrategy(strategy);
        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(rb.getString("parent")); //基本包 cn.itsource.system
        pc.setController("web.controller");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        pc.setEntity("domain");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);

        // 注入自定义配置,可以在 VM 中使用 cfg.abc 【可无】
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-rb");
                this.setMap(map);
            }
        };

        List<FileOutConfig> focList = new ArrayList<FileOutConfig>();


        // 调整 controller 生成目录演示
        focList.add(new FileOutConfig("/templates/controller.java.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                //controller输出完整路径
                return rb.getString("OutputDir")+ "/cn/itsource/course/web/controller/" + tableInfo.getEntityName() + "Controller.java";
            }
        });
        // 调整 query 生成目录演示
        focList.add(new FileOutConfig("/templates/query.java.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                //query输出完整路径
                return rb.getString("OutputDirBase")+ "/cn/itsource/course/query/" + tableInfo.getEntityName() + "Query.java";
            }
        });
        // 调整 domain 生成目录演示 , 你的domain到底要输出到哪儿????,你的domain怎么输出
        focList.add(new FileOutConfig("/templates/entity.java.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                //domain输出完整路径
                return rb.getString("OutputDirBase")+ "/cn/itsource/course/domain/" + tableInfo.getEntityName() + ".java";
            }
        });

        // 调整 xml 生成目录演示
        focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return rb.getString("OutputDirXml")+ "/cn/itsource/course/mapper/" + tableInfo.getEntityName() + "Mapper.xml";
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 自定义模板配置,可以 copy 源码 mybatis-plus/src/main/resources/templates 下面内容修改,
        // 放置自己项目的 src/main/resources/templates 目录下, 默认名称一下可以不配置,也可以自定义模板名称
        TemplateConfig tc = new TemplateConfig();
        tc.setService("/templates/service.java.vm");
        tc.setServiceImpl("/templates/serviceImpl.java.vm");
        tc.setEntity(null);
        tc.setMapper("/templates/mapper.java.vm");
        tc.setController(null);
        tc.setXml(null);
        // 如上任何一个模块如果设置 空 OR Null 将不生成该模块。
        mpg.setTemplate(tc);

        // 执行生成
        mpg.execute();
    }
}

5.拷贝模板:templates/controller.java.vm ; templates/query.java.vm
resources新建template目录

  • controller.java.vm
package ${package.Controller};

import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
import cn.itsource.hrm.query.${entity}Query;
import cn.itsource.hrm.util.AjaxResult;
import cn.itsource.hrm.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/${table.entityPath}")
public class ${entity}Controller {
    @Autowired
    public ${table.serviceName} ${table.entityPath}Service;

    /**
    * 保存和修改公用的
    * @param ${table.entityPath}  传递的实体
    * @return Ajaxresult转换结果
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody ${entity} ${table.entityPath}){
        try {
            if(${table.entityPath}.getId()!=null){
                ${table.entityPath}Service.updateById(${table.entityPath});
            }else{
                ${table.entityPath}Service.insert(${table.entityPath});
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败!"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            ${table.entityPath}Service.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败!"+e.getMessage());
        }
    }

    //获取用户
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public ${entity} get(@PathVariable("id")Long id)
    {
        return ${table.entityPath}Service.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<${entity}> list(){

        return ${table.entityPath}Service.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public PageList<${entity}> json(@RequestBody ${entity}Query query)
    {
        Page<${entity}> page = new Page<${entity}>(query.getPage(),query.getRows());
        page = ${table.entityPath}Service.selectPage(page);
        return new PageList<${entity}>(page.getTotal(),page.getRecords());
    }
}

  • query.java.vm
/**
 *
 * @author ${author}
 * @since ${date}
 */
public class ${table.entityName}Query extends BaseQuery{
}

6.调整 GenteratorCode 类: 配置文件名 ,表名 ,domain路径调整,xml路径调整 ,调整controller ,调整query

服务搭建

①.在码云上创建一个仓库用来存放项目

②.将仓库拉下来作为本地仓库

③.搭建项目

在这里插入图片描述

1.父工程pom

 <!--1.SpringBoot的父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>


    <!-- 3. 抽取公共的内容-->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <!--所有子模块一定要用到的公共的jar包-->
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

    <!--2.管理SpringCloud的jar包-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2.搭建注册中心—hrm-eureka-server-1010

2.1.导包
<dependencies>
        <!--注册中心服务端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!--web支持包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
2.2.配置类
@SpringBootApplication
@EnableEurekaServer//开启注册中心
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class);
    }
}
2.3.配置文件
eureka:
  instance:
    hostname: localhost
  client:
      registerWithEureka: false #禁用注册中心向自己注册
      fetchRegistry: false  #不让注册中心获取服务的注册列表
      serviceUrl:
        defaultZone: http://localhost:1010/eureka/ #注册中心的注册地址 ,其他微服务需要向这个地址注册
server:
  port: 1010

3.配置中心—hrm-config-server-1030

3.1.导包
 <!--eureka客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--webjar包支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--配置中心服务端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
3.2.配置类
@SpringBootApplication
@EnableConfigServer//开启配置中心
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class);
    }
}
3.3.配置文件
eureka:
  client:
    service-url:
      defaultZone: http://localhost:1010/eureka/ #注册中心服务端的注册地址

  instance:
     prefer-ip-address: true #使用ip进行注册
     instance-id: config-server:1030  #服务注册到注册中心的id
#端口号
server:
  port: 1030
#服务名称
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https:***  #码云上复制的链接
          username: ***  #码云注册账号
          password: *** #密码
          search-paths: myhrm-parent/configfiles #存放配置文件的目录

4.网关—hrm-gateway-zuul-1020

4.1.导包
<!--导入zuul-->
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-config</artifactId>
       </dependency>
   </dependencies>
4.2.配置类
@SpringBootApplication
@EnableZuulProxy//开启网关
public class ZuulServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZuulServerApplication.class);
    }
}
4.3.配置文件放到配置中心统一管理

将application.yml的放到管理配置文件的目录中,然后在当前服务的resources中新建bootstrap.yml文件

4.3.1.配置中心application-zuul-dev.yml文件
eureka:
  client:
    service-url:
      defaultZone: http://localhost:1010/eureka/ #注册中心服务端的注册地址

  instance:
     prefer-ip-address: true #使用ip进行注册
     instance-id: zuul-server:1020  #服务注册到注册中心的id
#端口号
server:
  port: 1020
#服务名称
spring:
  application:
    name: zuul-server
4.3.2.当前服务里的配置文件bootstrap.yml
spring:
  cloud:
    config:
      uri: http://localhost:1030  #本地配置中心的地址
      name: application-zuul   #码云上的配置文件名字
      profile: dev  #测试环境

5.课程服务—hrm-course-server-2020

5.1.用代码生成器生成domain、mapper等
5.2.集成Mybatis-plus
5.2.1.在当前服务导入依赖
 <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>2.2.0</version>
            </dependency>
<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.11</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
5.2.2.配置类

配置类开启事务,扫描接口包,分页插件
在这里插入图片描述

/**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
5.2.3.配置文件中集成DataSource,Mybatis-plus
 #连接数据库
  datasource:
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql:///hrm-course
    type: com.alibaba.druid.pool.DruidDataSource

#集成Mybatis-plus 
mybatis-plus:
  mapper-locations: classpath:cn/itsource/hrm/mapper/*Mapper.xml #mapper.xml的位置
5.2.4.通过网关访问

在这里插入图片描述

  • 发现路径中暴露了服务的服务名,所以在zuul配置文件中,配置服务的路由
zuul:
  ignored-services: "*" #禁止浏览器 使用服务名的方式去访问目标服务
  routes:
    course-server: "/course/**" # course-server这个服务使用 /course路径去访问
  • 再次用上面的路径发现已经不能访问了
    在这里插入图片描述- 使用路由访问成功
    在这里插入图片描述

6.集成swagger

6.1.导包
 <!--集成swagger-->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.9.2</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.9.2</version>
            </dependency>
6.2.swagger的配置类

修改swagger配置类,修改controller的包名,以及其他基本信息

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


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("系统服务api")
                .description("系统服务接口文档说明")
                .contact(new Contact("canala", "", "canala@itsource.cn"))
                .version("1.0")
                .build();
    }

}
6.3.访问

http://localhost:2020/swagger-ui.html
在这里插入图片描述

7.zuul整合Swagger

整合swagger之后,其他服务只要集成了swagger,就可以只访问一个地址,测试全部的服务

7.1.zuul服务中导包
  <!--集成swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
        </dependency>
7.2.准备整合swagger的配置类
  • -DocumentationConfig: resources.add要添加微服务的swagger资源
@Component
@Primary
public class DocumentationConfig implements SwaggerResourcesProvider {
    @Override
    public List<SwaggerResource> get() {
        List resources = new ArrayList<>();
        //添加所有的微服务的文档资源
        /**
         * 参数:
         * name:自定义的服务的名字
         * location:指定是微服务的路径   /前缀/服务名/v2/api-docs zuul中没有设置访问前缀可不写
         */
        resources.add(swaggerResource("课程管理", "/course/v2/api-docs", "2.0"));

        return resources;
    }

    private SwaggerResource swaggerResource(String name, String location, String version) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(name);
        swaggerResource.setLocation(location);
        swaggerResource.setSwaggerVersion(version);
        return swaggerResource;
    }
}
  • -SwaggerConfig:把termsOfServiceUrl配置成zuul的地址
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .pathMapping("/")
                .select() // 选择那些路径和api会生成document
                .apis(RequestHandlerSelectors.any())// 对所有api进行监控
                //不显示错误的接口地址
                .paths(Predicates.not(PathSelectors.regex("/error.*")))//错误路径不监控
                .paths(PathSelectors.regex("/.*"))// 对根下所有路径进行监控
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("人力资源综合平台")
                .description("人力资源综合平台接口文档说明")
                .termsOfServiceUrl("http://localhost:1020")//网关的地址
                .contact(new Contact("canola", "", "canola@itsoruce.cn"))
                .version("1.0")
                .build();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值