系列目錄:java
Spring Cloud的服務發現有2種實現方式,一個是Euraka服務,一個是基於Consul的服務。本文先講講Eureka的服務發現實現。web
Spring Cloud Eureka 模塊提供的功能是被動式的服務發現。 spring cloud eureka 分爲兩部分:spring
** 什麼是服務發現?** 服務發現就像聊天室一個,每一個用戶來的時候去服務器上註冊,這樣他的好友們就能看到你,你同時也將獲取好友的上線列表. 在微服務中,服務就至關於聊天室的用戶,而服務註冊中心就像聊天室服務器同樣,目前服務發現的解決方案有Eureka , Consul ,Etcd , Zookeeper ,SmartStack ,等等。 本文就來說講Eureka,如圖所示,Eureka Client經過HTTP(或者TCP,UDP)去Eureka Server註冊和獲取服務列表,爲了高可用通常會有多個Eureka Server組成集羣。Eureka會移除那些心跳檢查未到達的服務。apache
<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.zrx</groupId> <artifactId>minaret-eureka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>minaret-eureka-server</name> <description>Eureka註冊中心</description> <!-- org.springframework.booot 父類 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- 屬性配置文件 --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <spring.boot.admin.server>1.5.0</spring.boot.admin.server> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 服務發現 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>${spring.boot.admin.server}</version> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
啓動應用Application.javasegmentfault
@SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) throws Exception { SpringApplication.run(EurekaApplication.class, args); } }
配置application.yml服務器
server: port: 8761 spring: application: name: eureka-service eureka: client: registerWithEureka: false # 禁止 註冊中心,註冊本身做爲服務,默認是能夠的 fetchRegistry: false # 禁止 註冊中心,註冊本身做爲服務,默認是能夠的,本地是單點so,設置爲false server: waitTimeInMsWhenSyncEmpty: 0 evictionIntervalTimerInMs: 4000 instance: hostname: peer1 #對應的hostname metadataMap: instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}}
創建Eureka Client 端,例如user-microservice , 這是用戶服務。app
<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.zrx</groupId> <artifactId>minaret-sys-provider-user</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>minaret-sys-provider-user</name> <description>minaret-sys-provider-user</description> <!-- org.springframework.booot 父類 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <properties> <spring.boot.admin.server>1.5.0</spring.boot.admin.server> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Euraka --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- end --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.16</version> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>${spring.boot.admin.server}</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Application.classdom
@EnableDiscoveryClient @SpringBootApplication public class SysUserApplication { public static void main(String[] args) { new SpringApplicationBuilder(SysUserApplication.class).web(true).run(args); } }
server: port: 3333 spring: application.name: user-microservice-service # z指定註冊中心的位置 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/