码迷,mamicode.com
首页 > 编程语言 > 详细

简单使用SpringCloud开发聚合项目

时间:2019-11-27 11:58:13      阅读:312      评论:0      收藏:0      [点我收藏+]

标签:test   server   dmi   实用小技巧   sep   font   类型   client   integer   

一、新建复合项目(无任何勾选,版本都选2.1.10)

    1、删除src
    2、在pom文件中添加 <packaging>pom</packaging>
    3、删除 <description> 下面内容
    4、将子项目依次添加到 <modules> 中
    5、所有子项目的pom文件中改成 <relativePath>../</relativePath>

二、新建子项目eureka-center(勾选 Eureka Server、版本要2.1.10)

    1、在.properties中添加
        server.port=8000
        
        #不把自己注册到其它中心去(单体模式)
        eureka.client.register-with-eureka=false
        
        #不从其它中心获取注册信息(单体模式)
        eureka.client.fetch-registry=false
        
        #服务注册地址
        eureka.client.service-url.defaultZone=http://localhost:8000/eureka
        
    2、在主函数上打上 @EnableEurekaServer 注解

三、新建子项目module-common(勾选 lombok)

    1、只留src和pom文件
    2、创建实体类,删除主函数
    3、pom文件去除无关依赖test和 <build>

四、新建子项目module-service(勾选 Spring Web、MyBatis Framework、MySQL Driver、Spring Data Redis、Eureka Discovery Client、版本要2.1.10)

    1、在.properties文件中添加
        server.port=8081

        spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
        spring.datasource.url=jdbc:mysql://localhost:3306/house?characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai
        spring.datasource.username=root
        spring.datasource.password=admin

        spring.redis.host=localhost
        spring.redis.port=6379

        #这里要注意一下包结构
        mybatis.type-aliases-package=cn.kgc.common.entity

        #这里要注意一下包结构
        logging.level.cn.kgc.server.mapper=debug

        spring.jackson.time-zone=GMT+8
        spring.jackson.date-format=yyyy-MM-dd HH:mm:ss

        #当前服务在注册中心的名称,顺便将服务注册到eureka服务注册中心去
        spring.application.name=服务名称

        #服务注册中心的地址(就是eureka-center项目中的配置)
        eureka.client.service-url.defaultZone=http://localhost:8000/eureka
        
    2、在pom文件中添加部分依赖(自选)
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.58</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
        </dependency>

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.10</version>
        </dependency>
        
    3、在pom文件的<buid>中添加
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>

            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        
    4、在主函数上打上 @EnableEurekaClient 注解
    
    5、创建config.SwaggerConfig类(名字随意),写下 (测试部分5、6、7)

        @Configuration
        public class SwaggerConfig {

            @Bean
            public Docket createRestApi() {
                return new Docket(DocumentationType.SWAGGER_2)
                        .apiInfo(apiInfo())
                        .select()
                        .apis(RequestHandlerSelectors.basePackage("cn.kgc.service.controller"))
                        .paths(PathSelectors.any())
                        .build();
            }

            private ApiInfo apiInfo() {
                return new ApiInfoBuilder()
                        .title("xxxAPI")
                        .description("提供xxx的增删改查服务。")
                        .termsOfServiceUrl("http://blog.csdn.net/saytime")
                        .version("1.0")
                        .build();
            }
        }
    
    6、@ApiOperation(value = "") 解释这个接口用途或作用

        @ApiImplicitParam(name = "aid",value = "账号主键编号",required = true,dataType ="Integer",paramType = "path")
        name:参数名称,value:什么值,required:是否是必须的(true/false),dataType:参数的数据类型,
        paramType:参数从哪取值(path路径/body参数的实体类)
        
        @ApiImplicitParams({
            @ApiImplicitParam()    ...    多个参数(这个),一个参数(上面那个)
        })
        
    7、一顿打包运行servcie,然后在浏览其中输入localhost:8081/swagger-ui.html 访问

五、创建子项目module-client(勾选 Spring Web、Apache Freemarker、Eureka Discovery Client、OpenFeign、版本要2.1.10)

    1、在pom文件中添加
        <resources>
            ...上面有(注意要将页面、样式、图片等加上)
        </resources>
    2、在.properties文件中添加
        server.port=8080

        spring.freemarker.cache=false
        spring.freemarker.charset=UTF-8
        spring.freemarker.suffix=.ftl
        spring.freemarker.content-type=text/html

        #设置时间输入输出格式
        spring.jackson.time-zone=GMT+8
        spring.jackson.date-format=yyyy-MM-dd HH:mm:ss

        #当前服务在注册中心的名称,顺便将服务注册到eureka服务注册中心去
        spring.application.name=服务名称

        #服务注册中心的地址(就是eureka-center项目中的配置)
        eureka.client.service-url.defaultZone=http://localhost:8000/eureka

六、一些实用小技巧

    1、同源策略解决方法
    //url:"/all_comments?pageNum="+page+"&houseId=${house.id}",
    url:"http://localhost:8081/comments/houses/${house.id}?pageNum="+page+"&pageSize=5",
    #在服务端controller上写(允许跨源请求):
    @CrossOrigin(value = "*", maxAge = 3600)

 

    2、实用PageHelper进行分页
  (1) mapper层返回值使用List<实体>,service层返回值使用PageInfo<实体>
  (2) mapper层方法不写分页参数,service层写(curr,size)两个分页参数
  (3) serviceImpl层,先写PageHelper.startPage(curr, size);
  (4) 使用mapper层的方法获得实体集合list
  (5) 将实体集合放入 PageInfo<实体> info = new PageInfo<>(list);
  (6) 返回info对象

    
    2、Apache Freemarker 常用技巧
    
        ?c  可以解决数字格式含有逗号
        
        !  可以解决${字段}输出为空的报错
        
        ?date  可以设置时间格式
        
        ?string("yyyy-MM-dd HH:mm:ss")  可以设置时间格式
        
        <#assign 变量=值 >  标签可以定义变量
        
        <#if 判断条件 >  标签可以进行条件选择
        
        <#list 集合 as 单个>  类似java的for循环,还可以 <#list ["苹果","香蕉","西瓜"] as 单个>
        
        字段_index  可以取得下标
        
        ${(num?? && num=="${字段}")?string("selected","")}  类似java的三木运算符,?? 代表不为空

简单使用SpringCloud开发聚合项目

标签:test   server   dmi   实用小技巧   sep   font   类型   client   integer   

原文地址:https://www.cnblogs.com/zz945/p/11941185.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!