說明,這裏使用的開發工具是 eclipse, 創建兩個 maven 項目,一個做 server,一個做 client
1. 創建 server
1.1 修改 pom.xml 文件
基礎配置:設置 utf-8 編碼,設置 jdk 版本號
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
首先加入<parent> 依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
這時會報一個 build error, 加入 spring-milestone 就可以解決
<repositories>
<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>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
加入 Eureka Server 依賴
<!--eureka server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
此時會報如下錯誤
Project build error: 'dependencies.dependency.version' for org.springframework.cloud:spring-cloud-starter-eureka-server:jar is missing.
加入下面的配置即可
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
注意:這裏的版本如何選的不合適會報錯
1.2 添加配置文件 application.properties(英文有待提高)
#registration center port
server.port=9998
spring.application.name=eureka-server
#the hostname of this registration center instance
eureka.instance.hostname:localhost
#specify the location of the service registry
eureka.client.service-url.defaultZone=http://localhost:9998/eureka/
#whether to register yourself with the service registry
eureka.client.register-with-eureka=false
#whether to retrieve the service
eureka.client.fetch-registry=false
1.3 創建啓動類
package com.ahav;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args){
SpringApplication.run(EurekaServerApplication.class, args);
}
}
啓動成功:
1.4 瀏覽器打開
http://localhost:9998
更簡潔的 pom.xml (修改了版本)
<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.ahav</groupId>
<artifactId>boxEurekaServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eurekaserver</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.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>
</properties>
<dependencies>
<!--eureka server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</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>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
#使用這個更簡潔的版本在加入安全驗證的時候配置文件時需要在前面再加上一級 spring , 如下
spring.security.user.name=admin
spring.security.user.password=123456
這是與標題3中使用的區別
客戶端修改之後仍然會找不到註冊中心,因爲 springcloud 2.0 以上的 security 默認啓用 csrf 檢測。
解決辦法是在 Eureka Server 端添加一個配置類關閉 csrf 檢測功能。
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
super.configure(http);
}
}
2. 創建client
2.1 設置基礎配置信息:編碼、jdk版本等,加入<parent>依賴
2.2 加入 spring-milestones 解決 2.1 引起的錯誤
2.3 加入 Eureka client 的依賴
<!--eureka client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
加入上面的依賴後悔報錯,解決方法是加入下面內容:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.4 添加配置文件 application.properties
server.port=9997
spring.application.name=user-service
eureka.client.register-with-eureka=true
eureka.client.service-url.defaultZone = http://localhost:9998/eureka/
#Heartbeat check
eureka.instance.lease-renewal-interval-in-seconds=30
eureka.instance.lease-expiration-duration-in-seconds=90
eureka.client.register-with-eureka 默認爲true,表示在服務啓動時服務提供者會通過 REST 請求將自己註冊到 Eureka Server (服務註冊中心)上;設置爲 false 則不會註冊到 Eureka Server。
下面兩句是心跳檢測機制,這裏使用的是默認值;
eureka.instance.lease-renewal-interval-in-seconds 表示 client 給 server 發送心跳的頻率。如果Server 沒有收到 client 的心跳,當前微服務會被剔除,就接收不到流量了。如果當前微服務實現了HealthCheckCallback,並決定讓自己unavailable的話,該服務也不會接收到流量。
eureka.instance.lease-expiration-duration-in-seconds 表示 Eureka Server 從上一次檢測到心跳之後至檢測到下一次心跳的超時時間,如果90秒沒有檢測到下一次心跳,Eureka Server 會移除這個微服務。這個值如果太大可能將流量轉發過去的時候這個微服務已經被剔除了;如果太小可能因爲網絡不穩定等因素而誤被移除掉。至少要大於上 eureka.instance.lease-renewal-interval-in-seconds 的值。
2.5 創建啓動類
package com.ahav;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class UserApplication {
public static void main(String[] args){
SpringApplication.run(UserApplication.class, args);
}
}
啓動結果:
2.6 瀏覽器查看 Eureka Server 可以看到微服務註冊成功
到這裏,簡單的註冊與發現的最基本的框架就搭好了。
2.3步驟中改爲如下版本必須主動引入 web 依賴,否則 Spring 一些常用註解引用不了
<!--eureka client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3. 安全驗證
到目前爲止,服務註冊中心的頁面是可以公開訪問的,可以加入安全驗證使得訪問更安全
3.1 首先在 Eureka Server 的 pom.xml 文件加入安全驗證的依賴
<!-- 安全驗證 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
3.2 然後在 application.properties 文件中加入登錄需要的用戶名、密碼
security.user.name=admin
security.user.password=123456
3.3 重新啓動 Eureka Server 再打開瀏覽器,需要登錄成功才能打開服務註冊中心頁面。
3.4 此時可以正常訪問服務註冊中心,但是啓動服務提供者的程序會報錯
這是因爲服務提供者沒有通過安全檢測。
3.5 修改Eureka Client的 application.properties
server.port=9997
spring.application.name=user-service
eureka.client.register-with-eureka=true
eureka.client.service-url.defaultZone=http://${security.user.name}:${security.user.password}@localhost:9998/eureka/
#Heartbeat check
eureka.instance.lease-renewal-interval-in-seconds=30
eureka.instance.lease-expiration-duration-in-seconds=90
security.user.name=admin
security.user.password=123456
${security.user.name}:${security.user.password}@localhost:9998/eureka/
#Heartbeat check
eureka.instance.lease-renewal-interval-in-seconds=30
eureka.instance.lease-expiration-duration-in-seconds=90
security.user.name=admin
security.user.password=123456
4. application.properties 一些其他配置信息。
5. Eureka 高可用