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

2018-07-24 07:53:44来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

开发工具:STS

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

前言:

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

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

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

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

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

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

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

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

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

一、建立父工程

1.项目结构

 

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

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4     <modelVersion>4.0.0</modelVersion>
  5 
  6     <groupId>com.xm</groupId>
  7     <artifactId>SpringCloudDemo</artifactId>
  8     <version>0.0.1-SNAPSHOT</version>
  9     <packaging>pom</packaging>
 10 
 11     <name>SpringCloudDemo</name>
 12     <description>This is a Web about springcloud</description>
 13     
 14     
 15 
 16     <properties>
 17         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 18         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 19         <!-- Jdk版本控制 -->
 20         <maven.compiler.source>1.8</maven.compiler.source>
 21         <maven.compiler.target>1.8</maven.compiler.target>
 22         <java.version>1.8</java.version>
 23     </properties>
 24 <dependencyManagement>
 25     <dependencies>
 26         <!-- spring-cloud版本控制 -->
 27         <dependency>
 28             <groupId>org.springframework.cloud</groupId>
 29             <artifactId>spring-cloud-dependencies</artifactId>    
 30             <version>Dalston.SR1</version>
 31             <type>pom</type>
 32             <scope>import</scope>
 33         </dependency>
 34         <!-- spring-boot版本控制 -->
 35         <dependency>
 36             <groupId>org.springframework.boot</groupId>
 37             <artifactId>spring-boot-dependencies</artifactId>
 38             <version>1.5.14.RELEASE</version>
 39             <type>pom</type>
 40             <scope>import</scope>
 41         </dependency>
 42         <!-- <dependency>
 43             <groupId>org.springframework.boot</groupId>
 44             <artifactId>spring-boot-starter-web</artifactId>
 45             <version>1.5.14.RELEASE</version>
 46         </dependency> -->
 47         
 48         <dependency>
 49             <groupId>org.projectlombok</groupId>
 50             <artifactId>lombok</artifactId>
 51             <version>1.18.0</version>
 52             <scope>provided</scope>
 53         </dependency>
 54         <!-- 添加代码生成器的依赖 -->
 55         <dependency>
 56             <groupId>org.apache.velocity</groupId>
 57             <artifactId>velocity-engine-core</artifactId>
 58             <version>2.0</version>
 59         </dependency>
 60         <!--  添加MP依赖-->
 61         <dependency>
 62           <groupId>com.baomidou</groupId>
 63           <artifactId>mybatis-plus-boot-starter</artifactId>
 64           <version>2.3</version>
 65         </dependency>
 66         
 67         <dependency>
 68             <groupId>com.alibaba</groupId>
 69             <artifactId>druid</artifactId>
 70             <version>1.1.9</version>
 71         </dependency>
 72 
 73         <dependency>
 74             <groupId>mysql</groupId>
 75             <artifactId>mysql-connector-java</artifactId>
 76             <version>5.1.46</version>
 77             <scope>runtime</scope>
 78         </dependency>
 79         <!-- 修改后立即生效,热部署 -->
 80         <!-- <dependency>
 81             <groupId>org.springframework</groupId>
 82             <artifactId>springloaded</artifactId>
 83             <version>1.5.14.RELEASE</version>
 84         </dependency>
 85         <dependency>
 86             <groupId>org.springframework.boot</groupId>
 87             <artifactId>spring-boot-devtools</artifactId>
 88             <version>1.5.14.RELEASE</version>
 89         </dependency>
 90         <dependency>
 91             <groupId>org.springframework.boot</groupId>
 92             <artifactId>spring-boot-starter-test</artifactId>
 93             <version>1.5.14.RELEASE</version>
 94             <scope>test</scope> 
 95         </dependency>-->
 96     </dependencies>
 97 </dependencyManagement>
 98 
 99     <modules>
100         <module>Api</module>
101         <module>User</module>
102         <module>UI</module>
103     </modules>
104 </project>

 3.dependencyManagement与dependencies的区别

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

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

二、建立Api子工程

1.项目结构:

2.添加依赖

 1 <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">
 2   <modelVersion>4.0.0</modelVersion>
 3   <parent>
 4     <groupId>com.xm</groupId>
 5     <artifactId>SpringCloudDemo</artifactId>
 6     <version>0.0.1-SNAPSHOT</version>
 7   </parent>
 8   <artifactId>Api</artifactId>
 9   <dependencies>
10       <dependency>
11           <groupId>org.projectlombok</groupId>
12           <artifactId>lombok</artifactId>
13       </dependency>
14       <dependency>
15           <groupId>com.baomidou</groupId>
16           <artifactId>mybatis-plus-boot-starter</artifactId>
17       </dependency>
18   </dependencies>
19 </project>

 

3.添加公共实体

 1 package com.xm.springcloud.pojo;
 2 
 3 import com.baomidou.mybatisplus.enums.IdType;
 4 import java.util.Date;
 5 import com.baomidou.mybatisplus.annotations.TableId;
 6 import com.baomidou.mybatisplus.annotations.TableField;
 7 import com.baomidou.mybatisplus.annotations.TableId;
 8 import com.baomidou.mybatisplus.enums.IdType;
 9 import com.baomidou.mybatisplus.activerecord.Model;
10 import java.io.Serializable;
11 
12 import com.baomidou.mybatisplus.annotations.Version;
13 
14 import lombok.Data;
15 import lombok.EqualsAndHashCode;
16 import lombok.experimental.Accessors;
17 
18 /**
19  * <p>
20  * 
21  * </p>
22  *
23  * @author xm
24  * @since 2018-07-23
25  */
26 @Data
27 @EqualsAndHashCode(callSuper = false)
28 @Accessors(chain = true)
29 public class User extends Model<User> {
30 
31     private static final long serialVersionUID = 1L;
32 
33     /**
34      * 主键id
35      */
36     @TableId(value = "id", type = IdType.AUTO)
37     private Integer id;
38     /**
39      * 账号
40      */
41     @TableField("userName")
42     private String userName;
43     /**
44      * 密码
45      */
46     private String password;
47     /**
48      * 昵称
49      */
50     @TableField("nickName")
51     private String nickName;
52     /**
53      * 性别:0:男,1:女
54      */
55     private Boolean sex;
56     /**
57      * 生日
58      */
59     private Date birth;
60 
61 
62     @Override
63     protected Serializable pkVal() {
64         return this.id;
65     }
66 
67 }

 4.说明:

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

三、建立服务模块

1.项目结构

 

2.添加依赖

 1 <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">
 2   <modelVersion>4.0.0</modelVersion>
 3   <parent>
 4     <groupId>com.xm</groupId>
 5     <artifactId>SpringCloudDemo</artifactId>
 6     <version>0.0.1-SNAPSHOT</version>
 7   </parent>
 8   <artifactId>User</artifactId>
 9   <dependencies>
10       <dependency>
11           <groupId>org.springframework.boot</groupId>
12           <artifactId>spring-boot-starter-web</artifactId>
13       </dependency>
14       <!-- <dependency>
15           <groupId>org.springframework.boot</groupId>
16           <artifactId>spring-boot-devtools</artifactId>
17       </dependency> -->
18       <dependency>
19           <groupId>org.springframework.boot</groupId>
20           <artifactId>spring-boot-starter-test</artifactId>
21       </dependency>
22       <dependency>
23           <groupId>com.baomidou</groupId>
24           <artifactId>mybatis-plus-boot-starter</artifactId>
25       </dependency>
26       <dependency>
27           <groupId>org.apache.velocity</groupId>
28           <artifactId>velocity-engine-core</artifactId>
29       </dependency>
30       <dependency>
31           <groupId>org.projectlombok</groupId>
32           <artifactId>lombok</artifactId>
33       </dependency>
34       <dependency>
35           <groupId>com.alibaba</groupId>
36           <artifactId>druid</artifactId>
37       </dependency>
38       <dependency>
39           <groupId>mysql</groupId>
40           <artifactId>mysql-connector-java</artifactId>
41       </dependency>
42       <!-- <dependency>
43           <groupId>org.springframework</groupId>
44           <artifactId>springloaded</artifactId>
45       </dependency> -->
46       <dependency>
47           <groupId>com.xm</groupId>
48           <artifactId>Api</artifactId>
49           <version>0.0.1-SNAPSHOT</version>
50       </dependency>
51   </dependencies>
52   <build>
53         <plugins>
54             <plugin>
55                 <groupId>org.springframework.boot</groupId>
56                 <artifactId>spring-boot-maven-plugin</artifactId>
57             </plugin>
58         </plugins>
59         
60         <resources>
61           <resource>
62               <directory>src/main/java</directory>
63               <filtering>false</filtering>
64               <includes>
65                   <include>**/mapper/*.xml</include>
66               </includes>
67           </resource>
68       </resources>
69     </build>
70 </project>

 

3.配置文件

 1 server:
 2   port: 8001
 3 
 4 #配置日志级别
 5 logging:
 6   level:
 7     com: 
 8      xm: 
 9       springcloud: 
10        dao: trace
11       
12 spring: 
13    application:
14       name:  user
15    datasource:
16     type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
17     driver-class-name:  com.mysql.jdbc.Driver              # mysql驱动包
18     url: jdbc:mysql://10.1.51.31:3306/timehotel?useSSL=true         # 数据库名称
19     username: root
20     password: cube1501
21     dbcp2:
22       min-idle: 5                                           # 数据库连接池的最小维持连接数
23       initial-size: 5                                       # 初始化连接数
24       max-total: 5                                          # 最大连接数
25       max-wait-millis: 200                                  # 等待连接获取的最大超时时间
26       
27 mybatis-plus:
28   mapper-locations: classpath:/com/springcloud/dao/mapper/*Mapper.xml
29   type-aliases-package: com.xm.springcloud.pojo
30   configuration:
31     map-underscore-to-camel-case: true
32     cache-enabled: false

 

4.代码结构

5.说明:

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

四、建立UI模块

1.项目结构

 

2.添加依赖

 1 <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">
 2   <modelVersion>4.0.0</modelVersion>
 3   <parent>
 4     <groupId>com.xm</groupId>
 5     <artifactId>SpringCloudDemo</artifactId>
 6     <version>0.0.1-SNAPSHOT</version>
 7   </parent>
 8   <artifactId>UI</artifactId>
 9   <dependencies>
10       <dependency>
11           <groupId>org.springframework.boot</groupId>
12           <artifactId>spring-boot-starter-web</artifactId>
13       </dependency>
14       <dependency>
15           <groupId>com.xm</groupId>
16           <artifactId>Api</artifactId>
17           <version>0.0.1-SNAPSHOT</version>
18       </dependency>
19   </dependencies>
20 </project>

 

3.配置文件

1 server:
2   port: 80

 

4.配置注入RestTemplate

 1 package com.xm.springcloud.config;
 2 
 3 import org.springframework.context.annotation.Bean;
 4 import org.springframework.context.annotation.Configuration;
 5 import org.springframework.web.client.RestTemplate;
 6 
 7 @Configuration
 8 public class MyConfig {
 9     @Bean
10     public RestTemplate getRestTemplate() {
11         return new RestTemplate();
12     }
13 
14 }

 

5.转发请求服务

 1 package com.xm.springcloud.controller;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.web.bind.annotation.GetMapping;
 5 import org.springframework.web.bind.annotation.PathVariable;
 6 import org.springframework.web.bind.annotation.RestController;
 7 import org.springframework.web.client.RestTemplate;
 8 
 9 import com.xm.springcloud.pojo.User;
10 
11 @RestController
12 public class UserController {
13     
14     private static final String url = "http://127.0.0.1:8001";
15     
16     @Autowired
17     private RestTemplate restTemplate;
18     
19     @GetMapping("/user/{id}")
20     public User getById(@PathVariable("id")Long id) {
21         
22         return restTemplate.getForObject(url+"/user/"+id, User.class);
23     }
24 
25 }

6.说明

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

五、运行结果

1.测试User模块

 

2.测试UI模块


                                              2018-07-24

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Java知识点梳理——抽象类和接口

下一篇:Spring Cloud学习路线