赞
踩
在说微服务之前先简单的说一下Spring Cloud框架,看官网介绍Spring Cloud.
Spring Cloud Overview |
---|
Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems (e.g. configuration management, service discovery, circuit breakers, intelligent routing, micro-proxy, control bus, one-time tokens, global locks, leadership election, distributed sessions, cluster state). Coordination of distributed systems leads to boiler plate patterns, and using Spring Cloud developers can quickly stand up services and applications that implement those patterns. They will work well in any distributed environment, including the developer’s own laptop, bare metal data centres, and managed platforms such as Cloud Foundry. |
简单翻译一下就是说:
Spring Cloud 为开发者提供了快速构建分布式系统中一些常见模式的工具(例如配置管理、服务发现、断路器、智能路由、微代理、控制总线、一次性令牌、全局锁、领导选举、分布式会话、集群状态)。使用 Spring Cloud 开发人员可以快速建立实现这些模式的服务和应用程序。
我的项目就是基于Spring Boot,然后使用Spring Cloud搭建的微服务项目,下面说一下目录结构。
首先new一个Project
选择Maven,选择quickstart,然后起项目名,就完成了。
项目创建完成后,将没用的文件夹删除,只留下pom.xml文件即可。
pom.xml文件
<?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.aiun</groupId> <artifactId>BackendManageSystem</artifactId> <version>1.0-SNAPSHOT</version> <!--项目打包类型--> <packaging>pom</packaging> <!--模块管理,实现pom项目之间的聚合关系, 聚合关系下对父项目使用mvn命令,对其它子项目产生同样的效果--> <modules> <module>backend-common</module> <module>backend-order</module> <module>backend-generator</module> <module>backend-server8761</module> <module>backend-server8762</module> <module>backend-product</module> <module>backend-user</module> <module>backend-service-api</module> <module>backend-shipping</module> <module>backend-config-server</module> </modules> <!--继承spring-boot-starter-parent依赖--> <!--使用继承方式,实现复用,符合继承的都可以被使用--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> </parent> <!--集中定义依赖组件版本号,但不引入,在子工程中用到申明的依赖时, 可以不加依赖的版本号,这样可以统一管理工程中用到的版本号--> <properties> <!-- JDK 版本定义 --> <java.version>1.8</java.version> <!-- Spring Cloud 版本定义--> <spring-cloud.version>Hoxton.SR11</spring-cloud.version> <!-- MyBatis 依赖 --> <mybatis.version>1.3.2</mybatis.version> <!--pagehelper 分页依赖--> <pagehelper.version>1.3.0</pagehelper.version> <!--mysql 数据库依赖--> <mysql.version>8.0.18</mysql.version> <!--druid 连接池依赖--> <druid.version>1.1.10</druid.version> <!--lombok 连接池依赖--> <lombok.version>1.18.12</lombok.version> <!-- feign 缓存--> <feign.version>2.1.0.RELEASE</feign.version> <!-- google.guava 依赖--> <guava.version>18.0</guava.version> <!-- swagger 依赖--> <swagger.version>2.7.0</swagger.version> </properties> <!--项目依赖管理,父项目只是声明依赖,子项目需要写明需要的依赖(可以省略版本信息)--> <dependencyManagement> <dependencies> <!--Spring Cloud 依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- lombok 依赖 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> <scope>provided</scope> </dependency> <!--工具类--> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>${guava.version}</version> </dependency> <!-- mybatis 依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.version}</version> </dependency> <!-- pagehelper 分页依赖 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>${pagehelper.version}</version> </dependency> <!-- mysql 数据库依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- druid 连接池依赖 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <!--feign 依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>${feign.version}</version> </dependency> <!-- swagger 依赖--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${swagger.version}</version> </dependency> </dependencies> </dependencyManagement> </project>
这里以用户子系统模块为例,首先看pom.xml文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.aiun</groupId> <artifactId>BackendManageSystem</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.aiun</groupId> <artifactId>backend-user</artifactId> <version>0.0.1-SNAPSHOT</version> <name>backend-user</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--backend common 依赖--> <dependency> <groupId>com.aiun</groupId> <artifactId>backend-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--config client 依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency> <!--spring boot test 依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--spring boot web 依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- mybatis 依赖--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!--mysql 依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--druid 连接池依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <!-- lombok 依赖 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!-- swagger 依赖--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <releases> <enabled>false</enabled> </releases> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
先引入父Maven的依赖
<parent>
<groupId>com.aiun</groupId>
<artifactId>BackendManageSystem</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
因为父工程里面已经指定依赖的版本,所以子项目引用的时候不要写版本号,方便管理,后面如果需要更换版本号,直接在父工程里面修改就可以了。
BackendManageSystem是Maven父工程
backend-common用来存放所有系统使用的常量和工具类
backend-config-server是配置服务器
backend-generator是MyBatis逆向工程
backend-order是订单系统
backend-product是产品系统
backend-server8761是注册中心
backend-server8762是注册中心
backend-service-api是对外提供的Feign接口
backend-shipping是地址系统
backend-user是用户系统
注:项目已上传GitHub:https://github.com/AiunCode/BackendManageSystem.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。