一.服務發現的方式:java
1.客戶端發現:Eureka、ZooKeeper(存在缺陷)緣由:http://blog.csdn.net/whereismatrix/article/details/53305045mysql
2.服務端發現:consul+nginxnginx
描述:Eureka是Netflix開發的服務發現框架,自己是一個基於REST的服務,主要用於定位運行在AWS域中的中間層服務,以達到負載均衡和中間層服務故障轉移的目的。SpringCloud將它集成在其子項目spring-cloud-netflix中,一實現SpringCloud的服務發現功能web
二.服務註冊的功能spring
1.服務註冊表:服務註冊是一個記錄當前可用的微服務實例的網絡信息數據庫,是服務發現機制的主要核心,服務註冊表查詢api、管理api,使用查詢api得到可用服務的實例,使用管理api實現註冊、註銷sql
2.服務註冊:節點啓動後,會在Eureka Server中進行註冊,這樣EurekaServer中的服務註冊表中將會存儲全部可用服務節點的信息,服務節點的信息能夠在界面中直觀的看到。數據庫
三.實現一個Eurka Serve【microservice-discovery-eureka】apache
1.添加Eureka的maven依賴api
<?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.example</groupId> <artifactId>microservice-discovery-eureka</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>microservice-discovery-eureka</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Dalston.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </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-security</artifactId> </dependency>--> </dependencies> <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> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.啓動類:使用@EnableEurekaServer註解tomcat
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer//註冊成一個服務註冊中心,給其餘微服務應用進行提供會話 @SpringBootApplication public class MicroserviceDiscoveryEurekaApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceDiscoveryEurekaApplication.class, args); } }
3.application.properties的配置項
security.basic.enabled=true security.user.name=user security.user.password=test123456 server.port=8761 eureka.instance.hostname=localhost eureka.client.registerWithEureka=false eureka.client.fetchRegistry=false #添加身份驗證
eureka.client.serviceUrl.defaultZone=http://${security.user.name}:${security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/ #默認
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/ spring.application.name=cloud-eureka-server
4.啓動【microservice-discovery-eureka】
四.實現一個【microservice-provider-user】
1.添加Eureka的maven依賴
<?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.example</groupId> <artifactId>microservice-provider-user</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>microservice-provider-user</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Dalston.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</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-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <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> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.啓動類:使用@EnableEurekaClient註解
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableAutoConfiguration @EnableEurekaClient//註冊一個服務註冊應用 public class MicroserviceProviderUserApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceProviderUserApplication.class, args); } }
3.application.properties的配置項
server.port=9090
#服務註冊表中描述應用的名稱
spring.application.name=microservice-provider-user security.user.name=user security.user.password=test123456 #eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka-server-1.4.6/eureka/#添加身份驗證
eureka.client.serviceUrl.defaultZone=http://${security.user.name}:${security.user.password}@localhost:8761/eureka/ eureka.instance.appname=microservice.client.user #eureka.client.registerWithEureka=true #eureka.client.fetchRegistry=true
4.啓動【microservice-provider-user】
描述:先啓動服務發現,再啓動服務註冊類,刷新服務發現管理頁面
五.對Eureka服務的身份驗證(Authenticating with the Eureka Server)
1.修改應用maven依賴(服務註冊、服務發現)
<!-- 身份驗證--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
2.添加身份驗證
服務發現application.properties的配置項
security.basic.enabled=true security.user.name=user security.user.password=test123456 server.port=8761 eureka.instance.hostname=localhost eureka.client.registerWithEureka=false eureka.client.fetchRegistry=false eureka.client.serviceUrl.defaultZone=http://${security.user.name}:${security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/ spring.application.name=cloud-eureka-server
3.從新啓動,訪問服務發現管理頁面
4.服務註冊應用也須要用到身份驗證,不然註冊不了
服務註冊application.properties的配置項
server.port=9090 spring.application.name=microservice-provider-user security.user.name=user security.user.password=test123456 eureka.client.serviceUrl.defaultZone=http://${security.user.name}:${security.user.password}@localhost:8761/eureka/ eureka.instance.appname=microservice.client.user #eureka.client.registerWithEureka=true #eureka.client.fetchRegistry=true