1.Spring Cloud初相识--------简单项目搭建

开发工具:STS

代码下载链接:GitHub管理项目

前言:

Springcloud 算是当前比较火的技术,一套微服务架构的技术。

我个人对微服务的理解为:

服务可以代表service,微服务就是小的service,或者说逻辑上不可再分的功能单元。

比如一个电商管理平台,包括:用户管理、商品管理、订单管理、店铺管理、库存管理。。。。。。

我们可以把这些模块抽取成单独的进程单元,方便我们进行对某一功能的集群。

比如,我们的商品管理的业务压力较大,我们就可以集群多个商品管理单元。

微服务架构在我的个人理解下,就是整体分布式、局部集群式架构。把资源更加合理的分配,

也更加适合系统的弹性伸缩。

下面我们来搭建一个简单的微服务框架。

一、建立父工程

1.项目结构

图片描述

2.添加依赖,控制版本一致

 <?xml version="1.0" encoding="UTF-8"?>
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
 
     <groupId>com.xm</groupId>
     <artifactId>SpringCloudDemo</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     <packaging>pom</packaging>
 
     <name>SpringCloudDemo</name>
     <description>This is a Web about springcloud</description>
     
     
 
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
         <!-- Jdk版本控制 -->
         <maven.compiler.source>1.8</maven.compiler.source>
         <maven.compiler.target>1.8</maven.compiler.target>
         <java.version>1.8</java.version>
     </properties>
 <dependencyManagement>
     <dependencies>
         <!-- spring-cloud版本控制 -->
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-dependencies</artifactId>    
             <version>Dalston.SR1</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>
         <!-- spring-boot版本控制 -->
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-dependencies</artifactId>
             <version>1.5.14.RELEASE</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>
         <!-- <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
             <version>1.5.14.RELEASE</version>
         </dependency> -->
         
         <dependency>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>
             <version>1.18.0</version>
             <scope>provided</scope>
         </dependency>
         <!-- 添加代码生成器的依赖 -->
         <dependency>
             <groupId>org.apache.velocity</groupId>
             <artifactId>velocity-engine-core</artifactId>
             <version>2.0</version>
         </dependency>
         <!--  添加MP依赖-->
         <dependency>
           <groupId>com.baomidou</groupId>
           <artifactId>mybatis-plus-boot-starter</artifactId>
           <version>2.3</version>
         </dependency>
         
         <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>druid</artifactId>
             <version>1.1.9</version>
         </dependency>
 
         <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
             <version>5.1.46</version>
             <scope>runtime</scope>
         </dependency>
         <!-- 修改后立即生效,热部署 -->
         <!-- <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>springloaded</artifactId>
             <version>1.5.14.RELEASE</version>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-devtools</artifactId>
             <version>1.5.14.RELEASE</version>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <version>1.5.14.RELEASE</version>
             <scope>test</scope> 
         </dependency>-->
     </dependencies>
 </dependencyManagement>
 
     <modules>
         <module>Api</module>
         <module>User</module>
         <module>UI</module>
     </modules>
 </project>

3.dependencyManagement与dependencies的区别

dependencyManagement:只控制版本,子类需要添加指定依赖,但不需要指定版本

dependencies:子类将直接继承,无需再pom文件中配置

二、建立Api子工程

1.项目结构:

图片描述

2.添加依赖

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
     <groupId>com.xm</groupId>
     <artifactId>SpringCloudDemo</artifactId>
     <version>0.0.1-SNAPSHOT</version>
   </parent>
   <artifactId>Api</artifactId>
   <dependencies>
       <dependency>
           <groupId>org.projectlombok</groupId>
           <artifactId>lombok</artifactId>
       </dependency>
       <dependency>
           <groupId>com.baomidou</groupId>
           <artifactId>mybatis-plus-boot-starter</artifactId>
       </dependency>
   </dependencies>
 </project>

3.添加公共实体

 package com.xm.springcloud.pojo;
 
 import com.baomidou.mybatisplus.enums.IdType;
 import java.util.Date;
 import com.baomidou.mybatisplus.annotations.TableId;
 import com.baomidou.mybatisplus.annotations.TableField;
 import com.baomidou.mybatisplus.annotations.TableId;
 import com.baomidou.mybatisplus.enums.IdType;
 import com.baomidou.mybatisplus.activerecord.Model;
 import java.io.Serializable;
 
 import com.baomidou.mybatisplus.annotations.Version;
 
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.experimental.Accessors;
 
 /**
  * <p>
  * 
  * </p>
  *
  * @author xm
  * @since 2018-07-23
  */
 @Data
 @EqualsAndHashCode(callSuper = false)
 @Accessors(chain = true)
 public class User extends Model<User> {
 
     private static final long serialVersionUID = 1L;
 
     /**
      * 主键id
      */
     @TableId(value = "id", type = IdType.AUTO)
     private Integer id;
     /**
      * 账号
      */
     @TableField("userName")
     private String userName;
     /**
      * 密码
      */
     private String password;
     /**
      * 昵称
      */
     @TableField("nickName")
     private String nickName;
     /**
      * 性别:0:男,1:女
      */
     private Boolean sex;
     /**
      * 生日
      */
     private Date birth;
 
 
     @Override
     protected Serializable pkVal() {
         return this.id;
     }
 
 }

4.说明:

Api里面放的是模块通用的类,其他模块需要引用其依赖使用。

三、建立服务模块

1.项目结构

图片描述

2.添加依赖

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
     <groupId>com.xm</groupId>
     <artifactId>SpringCloudDemo</artifactId>
     <version>0.0.1-SNAPSHOT</version>
   </parent>
   <artifactId>User</artifactId>
   <dependencies>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <!-- <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-devtools</artifactId>
       </dependency> -->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-test</artifactId>
       </dependency>
       <dependency>
           <groupId>com.baomidou</groupId>
           <artifactId>mybatis-plus-boot-starter</artifactId>
       </dependency>
       <dependency>
           <groupId>org.apache.velocity</groupId>
           <artifactId>velocity-engine-core</artifactId>
       </dependency>
       <dependency>
           <groupId>org.projectlombok</groupId>
           <artifactId>lombok</artifactId>
       </dependency>
       <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>druid</artifactId>
       </dependency>
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
       </dependency>
       <!-- <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>springloaded</artifactId>
       </dependency> -->
       <dependency>
           <groupId>com.xm</groupId>
           <artifactId>Api</artifactId>
           <version>0.0.1-SNAPSHOT</version>
       </dependency>
   </dependencies>
   <build>
         <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
             </plugin>
         </plugins>
         
         <resources>
           <resource>
               <directory>src/main/java</directory>
               <filtering>false</filtering>
               <includes>
                   <include>**/mapper/*.xml</include>
               </includes>
           </resource>
       </resources>
     </build>
 </project>

3.配置文件

 server:
   port: 8001
 
 #配置日志级别
 logging:
   level:
     com: 
      xm: 
       springcloud: 
        dao: trace
       
 spring: 
    application:
       name:  user
    datasource:
     type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
     driver-class-name:  com.mysql.jdbc.Driver              # mysql驱动包
     url: jdbc:mysql://10.1.51.31:3306/timehotel?useSSL=true         # 数据库名称
     username: root
     password: cube1501
     dbcp2:
       min-idle: 5                                           # 数据库连接池的最小维持连接数
       initial-size: 5                                       # 初始化连接数
       max-total: 5                                          # 最大连接数
       max-wait-millis: 200                                  # 等待连接获取的最大超时时间
       
 mybatis-plus:
   mapper-locations: classpath:/com/springcloud/dao/mapper/*Mapper.xml
   type-aliases-package: com.xm.springcloud.pojo
   configuration:
     map-underscore-to-camel-case: true
     cache-enabled: false

4.代码结构

图片描述

5.说明:

因为mybatisplus和热部署有冲突,在这里就取消了热部署

四、建立UI模块

1.项目结构

图片描述

2.添加依赖

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
     <groupId>com.xm</groupId>
     <artifactId>SpringCloudDemo</artifactId>
     <version>0.0.1-SNAPSHOT</version>
   </parent>
   <artifactId>UI</artifactId>
   <dependencies>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <dependency>
           <groupId>com.xm</groupId>
           <artifactId>Api</artifactId>
           <version>0.0.1-SNAPSHOT</version>
       </dependency>
   </dependencies>
 </project>

3.配置文件

 server:
   port: 80

4.配置注入RestTemplate

 package com.xm.springcloud.config;
 
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.web.client.RestTemplate;
 
 @Configuration
 public class MyConfig {
     @Bean
     public RestTemplate getRestTemplate() {
         return new RestTemplate();
     }
 
 }

5.转发请求服务

 package com.xm.springcloud.controller;
 
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.client.RestTemplate;
 
 import com.xm.springcloud.pojo.User;
 
 @RestController
 public class UserController {
     
     private static final String url = "http://127.0.0.1:8001";
     
     @Autowired
     private RestTemplate restTemplate;
     
     @GetMapping("/user/{id}")
     public User getById(@PathVariable("id")Long id) {
         
         return restTemplate.getForObject(url+"/user/"+id, User.class);
     }
 
 }

6.说明

80端口在浏览器中会自动缺省

五、运行结果

1.测试User模块

图片描述

2.测试UI模块

图片描述

                                              2018-07-24