这里使用IDEA构建一个springcloud聚合工程demo
,服务注册与发现组件使用eureka,然后使用ribbon实现客户端负载均衡 在创建工程之前,我是先把eureka服务端给创建好了
1、创建父工程
File–>new–>project,选择spring Initializr
SDK选择自己本地的JDK,可以通过右边的NEW按钮进行选择导入
填写父工程的一些信息,Type选择Maven Project ; 打包方式选择jar; Java Version根据自己本地的java版本进行选择
这里选择需引入的外部依赖Jar包,这里父工程的作用类似于文件夹,来进行版本控制,所以这里不需要引入外部jar包。外部jar包的引入会在子工程的一个公共工程中进行引入
选择该工程存放的位置
创建成功,父工程目录结构如下
.gitignore 用git做版本控制时 用这个文件控制那些文件或文件夹 不被提交(不用git的话可删除 没影响)
HELP.md md是一种文档格式 这个就是你项目的帮助文档(可删除 没影响)
mvnw linux上处理mevan版本兼容问题的脚本(可删除 没影响)
mvnw.cmd windows 上处理mevan版本兼容问题的脚本(可删除 没影响)
spring-boot.iml 是IDEA特有的文件每个导入IDEA的项目都会生成一个项目同名的 .iml文件 用于保存你对这个项目的配置 (删了程序重新导入后还会生成 但由于配置丢失可能会造成程序异常)
这里可以把父工程无用的src目录删除2、在父工程下创建公共工程 common
在父工程名上右键–>new–>Module
这里也不进行jar包的引入,后面会在common工程的pom.xml文件中统一引入jar包依赖。点击next
点击finish,创建成功
common工程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.gmf</groupId>
<artifactId>gmf-father</artifactId>
<version>0.0.1</version>
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<groupId>com.gmf</groupId>
<artifactId>common</artifactId>
<version>${common.version}</version>
<name>common</name>
<description>公共工程</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--spring boot依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 测试类 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- 微服务组件-eureka client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>${spring.cloud.netflix.version}</version>
</dependency>
<!-- 微服务组件-ribbon -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>${spring.cloud.netflix.version}</version>
</dependency>
<!-- 微服务组件-feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>${spring.cloud.netflix.version}</version>
</dependency>
<!-- 微服务组件-hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>${spring.cloud.netflix.version}</version>
</dependency>
<!-- 微服务组件-网关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
<version>${spring.cloud.netflix.version}</version>
</dependency>
<!--数据库-mysql-驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--SqlServer数据库驱动 -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<!-- mybatis-plus;这里使用的持久层框架是mybatis-plus,是对mybatis的一种封装和拓展,增删改查等操作使用对应的方法即可使用 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-annotation</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.3.2</version>
</dependency>
<!-- 工具类-lombok;用在实体类bean上,这里的主要作用是生成bean中的set和get方法;这里需要先在idea里安装lombok插件; -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- 热启动 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
</build>
</project>
这里需要将common的application配置文档改为application-common,方便其他工程引用该配置文件
common工程的application.properties文件内容先设置如下:
#eureka-config
eureka.client.service-url.defaultZone=http://127.0.0.1:10001/eureka
eureka.client.healthcheck.enabled=true
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true
eureka.client.registry-fetch-interval-seconds=10
eureka.instance.lease-renewal-interval-in-seconds=15
eureka.instance.lease-expiration-duration-in-seconds=30
eureka.instance.hostname=${spring.cloud.client.ip-address}
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
eureka.instance.prefer-ip-address=true
#DB-config-mssql
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://127.0.0.1:1433;SelectMethod=cursor;databaseName=TestDB
#spring.datasource.username=eomc
spring.datasource.username=songlp
spring.datasource.password=Aa1qazxsw2!@
#mybatis-plus
mybatis-plus.mapper-locations=classpath:mapping/*.xml
#实体类映射包
mybatis-plus.type-aliases-package=com.gmf.briup.**.entity
3、按照创建common工程的过程 创建order-service01、order-service02工程,来模拟后台服务端集群。再创建一个goods-serbice工程,一个front前端工程,页面都放在front工程下
oreder-service01工程的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.gmf</groupId>
<artifactId>gmf-father</artifactId>
<version>0.0.1</version>
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<groupId>com.gmf</groupId>
<artifactId>order-service01</artifactId>
<version>${order-service.version}</version>
<name>order-service01</name>
<description>后端订单服务工程01</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 依赖common工程 -->
<dependency>
<groupId>com.gmf</groupId>
<artifactId>common</artifactId>
<version>${common.version}</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>false</addResources>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
</project>
oreder-service01工程的applicationproperties文件如下
server.port=8091
spring.application.name=order-service
#引用common工程的application配置
spring.profiles.include=common
#可以注册到eureka服务注册中心
eureka.client.enabled=true
order-service02的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.gmf</groupId>
<artifactId>gmf-father</artifactId>
<version>0.0.1</version>
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<groupId>com.gmf</groupId>
<artifactId>order-service02</artifactId>
<version>${order-service.version}</version>
<name>order-service02</name>
<description>后端订单服务工程02</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 依赖common工程 -->
<dependency>
<groupId>com.gmf</groupId>
<artifactId>common</artifactId>
<version>${common.version}</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>false</addResources>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
</project>
order-service02的application.properties配置文件内容:
server.port=8092
#和order-service01工程的微服务实例名一样,都是order-service,只是端口不一样。
spring.application.name=order-service
#引用common工程的application配置
spring.profiles.include=common
eureka.client.enabled=true
goods-service工程的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.gmf</groupId>
<artifactId>gmf-father</artifactId>
<version>0.0.1</version>
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<groupId>com.gmf</groupId>
<artifactId>goods-service</artifactId>
<version>${goods-service.version}</version>
<name>goods-service</name>
<description>后端商品服务工程</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 依赖common工程 -->
<dependency>
<groupId>com.gmf</groupId>
<artifactId>common</artifactId>
<version>${common.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-client</artifactId>
<version>2.2.4.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>false</addResources>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
</project>
goods-service工程的application配置文件内容如下:
server.port=8093
spring.application.name=goods-service
#引用common工程的application配置
spring.profiles.include=common
eureka.client.enabled=true
front前端工程的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.gmf</groupId>
<artifactId>gmf-father</artifactId>
<version>0.0.1</version>
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<groupId>com.gmf</groupId>
<artifactId>front</artifactId>
<version>${front.version}</version>
<name>front</name>
<description>前端工程</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 依赖common工程 -->
<dependency>
<groupId>com.gmf</groupId>
<artifactId>common</artifactId>
<version>${common.version}</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>false</addResources>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
</project>
front工程的application配置文件内容如下:
server.port=8090
spring.application.name=front
#引用common工程的application配置
spring.profiles.include=common
eureka.client.enabled=true
#thymeleaf页面的配置信息
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=false
各个子工程的pom.xml文件和application文件都基本配置完成,最后配置父工程的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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gmf</groupId>
<artifactId>gmf-father</artifactId>
<version>0.0.1</version>
<name>gmf-father</name>
<description>父工程</description>
<packaging>pom</packaging>
<properties>
<!--版本控制-->
<java.version>1.8</java.version>
<spring.cloud.netflix.version>2.2.4.RELEASE</spring.cloud.netflix.version>
<common.version>0.0.1</common.version>
<front.version>0.0.1</front.version>
<order-service.version>0.0.1</order-service.version>
<goods-service.version>0.0.1</goods-service.version>
</properties>
<!--子工程-->
<modules>
<module>common</module>
<module>front</module>
<module>order-service01</module>
<module>order-service02</module>
<module>goods-service</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>
<delimiter>@</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
<resources>
<!--可以新建webapp目录,将前端页面资源放到该目录下-->
<resource>
<directory>src/main/webapp</directory>
<includes>
<include>**/*</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.yml</include>
<include>**/*.yaml</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>local</id>
<properties>
<spring.active>local</spring.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>dev</id>
<properties>
<spring.active>dev</spring.active>
</properties>
</profile>
</profiles>
</project>
各个子工程的启动类上都要添加注解 @EnableEurekaClient;
这里值得注意的是,我们有时并不需要在启动类上标明 @EnableEurekaClien 注解,该微服务也可以注册到eureka服务注册中心,这是因为EurekaClient的注册,其实是和两个配置项有关的,一个是eureka.client.enabled,另一个是spring.cloud.service-registry.auto-registration.enabled,只不过这两个配置默认都是true。所以只要我们不在配置文件里将这两个配置项显示地设置为false,即使不在启动类上标 @EnableEurekaClien 注解,该微服务也可以进行服务注册
4、到这里为止,整个springcloud的架构就基本构建好了
依次启动几个子工程,在浏览器中输入127.0.0.1:10001(或eurekaserver01:10001)或127.0.0.1:10002 进入eureka注册中心查看微服务注册启动成功与否。启动成功的话如下图:
5、在上面搭建的微服务聚合工程中,我们可以做一些简单的springcloud组件应用,包括负载均衡(Ribbon),服务网关(Zuul),服务熔断和降级(Hystrix)等,在后面的博客中,我们会继续完善这些组件的应用