Spring Cloud Eureka學習筆記
阿新 • • 發佈:2018-12-14
用於服務的註冊於發現
由兩個元件組成
- Eureka Server 註冊中心
- Eureka Client 服務註冊
Eureka Server(服務註冊中心)
先建立一個springboot專案,新增依賴的時候選擇如下
pom檔案如下
<?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.cloud</groupId> <artifactId>eureka</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>eureka</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.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>Finchley.SR1</spring-cloud.version> </properties> <dependencies> <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-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>
新增@EnableEurekaServer註解,啟動一個服務註冊中心提供給其他應用進行對話
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
除此之外還需要新增配置檔案
eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ #由於註冊中心會將自己作為客戶端嘗試註冊自己,關閉客戶端註冊行為 register-with-eureka: false #eureka的自我保護(只在開發環境關閉) server: enable-self-preservation: false spring: application: name: eureka server: port: 8761
執行後,訪問http://localhost:8761/可以看到如下頁面:
Eureka Client 服務提供方
同樣,先建立一個springboot專案,新增依賴的時候選擇
pom檔案如下,注意新增tomcat的依賴,預設是沒有新增的
<?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.cloud</groupId> <artifactId>client</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>client</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.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>Finchley.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </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>
同樣新增@EnableDiscoveryClient註解
@SpringBootApplication
@EnableDiscoveryClient
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}
新增配置檔案
eureka:
client:
service-url:
#指定註冊中心的地址
defaultZone: http://localhost:8761/eureka/
# instance:
# hostname: clientname
spring:
application:
name: client
執行專案,在之前的Eureka頁面可以看到,服務被註冊進來了
Eureka Server 高可用
雙節點註冊中心
兩個Eureka註冊中心互相註冊,client在兩個註冊中心都進行註冊
配置檔案如下:
Eureka1:
eureka:
client:
service-url:
defaultZone: http://localhost:8762/eureka/
#不讓eureka出現在註冊中心
register-with-eureka: false
#eureka的自我保護(只在開發環境關閉)
server:
enable-self-preservation: false
spring:
application:
name: eureka
Eureka2:
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
#不讓eureka出現在註冊中心
register-with-eureka: false
#eureka的自我保護(只在開發環境關閉)
server:
enable-self-preservation: false
spring:
application:
name: eureka
client:
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
# instance:
# hostname: clientname
spring:
application:
name: client
啟動Eureka1,Eureka2,再啟動client一段時間之後,client的服務會註冊到兩個註冊中心
三節點註冊中心
同上,三個Eureka兩兩註冊,client同時註冊三個
Eureka1:
eureka:
client:
service-url:
defaultZone: http://localhost:8762/eureka/, http://localhost:8763/eureka/
#不讓eureka出現在註冊中心
register-with-eureka: false
#eureka的自我保護(只在開發環境關閉)
server:
enable-self-preservation: false
spring:
application:
name: eureka
Eureka2:
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/, http://localhost:8763/eureka/
#不讓eureka出現在註冊中心
register-with-eureka: false
#eureka的自我保護(只在開發環境關閉)
server:
enable-self-preservation: false
spring:
application:
name: eureka
Eureka3:
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/, http://localhost:8762/eureka/
#不讓eureka出現在註冊中心
register-with-eureka: false
#eureka的自我保護(只在開發環境關閉)
server:
enable-self-preservation: false
spring:
application:
name: eureka
client:
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
# instance:
# hostname: clientname
spring:
application:
name: client
服務發現的兩種方式
- 客戶端發現
- Eureka
- 服務端發現
- nginx
- zookeeper
- kubernetes