Spring Cloud 是一個相對比較新的微服務框架,2016 才推出 1.0 的 Release 版本. 可是其更新特別快,幾乎每 1-2 個月就有一次更新,雖然 Spring Cloud 時間最短, 可是相比 Dubbo 等 RPC 框架, Spring Cloud 提供的全套的分佈式系統解決方案。
Spring Cloud 爲開發者提供了在分佈式系統(配置管理,服務發現,熔斷,路由,微代理,控制總線,一次性 Token,全局瑣,Leader 選舉,分佈式 Session,集羣狀態)中快速構建的工具,使用 Spring Cloud 的開發者能夠快速的啓動服務或構建應用、同時可以快速和雲平臺資源進行對接。java
建立方法,直接建立文件夾而後寫pom.xml
首先建立一個依賴模塊,直接建立文件夾hello-spring-cloud做爲總的工程目錄,而後再在hello-spring-cloud下建立hello-spring-cloud-dependencies做爲全部模塊的父類git
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> </parent> <groupId>com.outlook.liufei32</groupId> <artifactId>hello-spring-cloud-dependencies</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>pom</packaging> <name>hello-spring-cloud-dependencies</name> <url>https://github.com/Swagger-Ranger</url> <inceptionYear>2018-Now</inceptionYear> <properties> <!-- Environment Settings --> <java.version>1.11</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- Spring Settings --> <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <!-- Compiler 插件, 設定 JDK 版本 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <showWarnings>true</showWarnings> </configuration> </plugin> <!-- 打包 jar 文件時,配置 manifest 文件,加入 lib 包的 jar 依賴 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <addMavenDescriptor>false</addMavenDescriptor> </archive> </configuration> <executions> <execution> <configuration> <archive> <manifest> <!-- Add directory entries --> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries> <addClasspath>true</addClasspath> </manifest> </archive> </configuration> </execution> </executions> </plugin> <!-- resource --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> </plugin> <!-- install --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-install-plugin</artifactId> </plugin> <!-- clean --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-clean-plugin</artifactId> </plugin> <!-- ant --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> </plugin> <!-- dependency --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> </plugin> </plugins> <pluginManagement> <plugins> <!-- Java Document Generate --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <executions> <execution> <phase>prepare-package</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <!-- YUI Compressor (CSS/JS壓縮) --> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>yuicompressor-maven-plugin</artifactId> <version>1.5.1</version> <executions> <execution> <phase>prepare-package</phase> <goals> <goal>compress</goal> </goals> </execution> </executions> <configuration> <encoding>UTF-8</encoding> <jswarn>false</jswarn> <nosuffix>true</nosuffix> <linebreakpos>30000</linebreakpos> <force>true</force> <includes> <include>**/*.js</include> <include>**/*.css</include> </includes> <excludes> <exclude>**/*.min.js</exclude> <exclude>**/*.min.css</exclude> </excludes> </configuration> </plugin> </plugins> </pluginManagement> <!-- 資源文件配置 --> <resources> <resource> <directory>src/main/java</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> </build> <repositories> <repository> <id>aliyun-repos</id> <name>Aliyun Repository</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>sonatype-repos</id> <name>Sonatype Repository</name> <url>https://oss.sonatype.org/content/groups/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>sonatype-repos-s</id> <name>Sonatype Repository</name> <url>https://oss.sonatype.org/content/repositories/snapshots</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>aliyun-repos</id> <name>Aliyun Repository</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project>
配置pom.xmlgithub
<?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> <parent> <groupId>com.outlook.liufei32</groupId> <artifactId>hello-spring-cloud-dependencies</artifactId> <version>1.0.0-SNAPSHOT</version> <relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath> </parent> <artifactId>hello-spring-cloud-eureka</artifactId> <packaging>jar</packaging> <name>hello-spring-cloud-eureka</name> <url>https://github.com/Swagger-Ranger</url> <inceptionYear>2018-Now</inceptionYear> <dependencies> <!-- Spring Boot Begin --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Spring Boot End --> <!-- Spring Cloud Begin --> <dependency> <!--<groupId>org.springframework.cloud</groupId>--> <!--<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>--> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.4.0.RELEASE</version> </dependency> <!-- Spring Cloud End --> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!--指定啓動的入口類,由於微服務是jar包運行,jar的運行方式:java -jar 入口類名--> <mainClass>com.outlook.liufei32.hello.spring.cloud.eureka.EurekaApplication</mainClass> </configuration> </plugin> </plugins> </build> </project>
建立application入口,即jar的啓動類EurekaApplication注意命令格式爲模塊名+application;和配置文件application.yml配置服務註冊
web
application.ymlspring
spring: application: name: hello-spring-cloud-eureka server: port: 8761 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
EurekaApplication.javaapache
package com.outlook.liufei32.hello.spring.cloud.eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /******************************************************************************* * @Copyright (C), 2018-2019,github:Swagger-Ranger * @FileName: EurekaApplication * @Author: liufei32@outlook.com * @Date: 2019/4/2 11:01 * @Description: eureka入口類 * @Aha-eureka: *******************************************************************************/ @SpringBootApplication @EnableEurekaServer//開啓eureka服務端,全部的服務提供者都要用這個註解註冊到eureka服務端,而服務消費者則是@EnableDiscoveryClient註解 public class EurekaApplication { public static void main( String[] args ) { SpringApplication.run(EurekaApplication.class); } }
1.在IDE配置中設置多個實例並行
2.再修改配置裏的端口
3.啓動服務的啓動類,就能看到多個實例並行
瀏覽器
注意maven依賴,經常版本不對應會致使服務註冊沒法啓動,包括JDK的版本,我使用Java11沒法啓動,但使用Java8則正常
啓動成功則在瀏覽器中訪問配置的註冊中心IP+端口就能訪問
app
建立服務提供者,直接在總包下新建hello-spring-cloud-service-服務模塊名,而後相同的步驟,配置pom.xml導入maven依賴,配置啓動類Application,配置配置文件application.yml負載均衡
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> <parent> <groupId>com.outlook.liufei32</groupId> <artifactId>hello-spring-cloud-dependencies</artifactId> <version>1.0.0-SNAPSHOT</version> <relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath> </parent> <artifactId>hello-spring-cloud-service-admin</artifactId> <packaging>jar</packaging> <name>hello-spring-cloud-service-admin</name> <url>https://github.com/Swagger-Ranger</url> <inceptionYear>2019-Now</inceptionYear> <dependencies> <!-- Spring Boot Begin --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Spring Boot End --> <!-- Spring Cloud Begin --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!-- Spring Cloud End --> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.outlook.liufei32.hello.spring.cloud.service.admin.ServiceAdminApplication</mainClass> </configuration> </plugin> </plugins> </build> </project>
application.yml
spring: application: name: hello-spring-cloud-service-admin server: port: 8762 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ #註冊到註冊端
ServiceAdminApplication.java
package com.outlook.liufei32.hello.spring.cloud.service.admin; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /******************************************************************************* * @Copyright (C), 2018-2019,github:Swagger-Ranger * @FileName: ServiceAdminApplication * @Author: liufei32@outlook.com * @Date: 2019/4/2 15:25 * @Description: admin服務的入口類 * @Aha-eureka: *******************************************************************************/ @SpringBootApplication @EnableEurekaClient public class ServiceAdminApplication { public static void main( String[] args ) { SpringApplication.run(ServiceAdminApplication.class); } }
完成後能夠新建一個controller測試返回,而後在瀏覽器中測試http://localhost:8762/hi?message=wagger-ranger
註冊成功後在服務端的web頁面上就能看到註冊的服務,
當 Client 向 Server 註冊時,它會提供一些元數據,例如主機和端口,URL,主頁等。Eureka Server 從每一個 Client 實例接收心跳消息。 若是心跳超時,則一般將該實例從註冊 Server 中刪除。