Spring Cloud學習筆記【一】Eureka服務注冊與發現


Spring Cloud Eureka 是 Spring Cloud Netflix 微服務套件的一部分,基於 Netflix Eureka 做了二次封裝,主要負責完成微服務架構中的服務治理功能,服務治理可以說是微服務架構中最為核心和基礎的模塊,他主要用來實現各個微服務實例的自動化注冊與發現。

  • 服務注冊:在服務治理框架中,通常都會構建一個注冊中心,每個服務單元向注冊中心登記自己提供的服務,將主機與端口號、版本號、通信協議等一些附加信息告知注冊中心,注冊中心按照服務名分類組織服務清單,服務注冊中心還需要以心跳的方式去監控清單中的服務是否可用,若不可用需要從服務清單中剔除,達到排除故障服務的效果。
  • 服務發現:由於在服務治理框架下運行,服務間的調用不再通過指定具體的實例地址來實現,而是通過向服務名發起請求調用實現。

Spring Cloud Eureka 使用 Netflix Eureka 來實現服務注冊與發現,即包括了服務端組件,也包含了客戶端組件,並且服務端和客戶端均采用 Java 編寫,所以 Eureka 主要適用與通過 Java 實現的分布式系統,或是與 JVM 兼容語言構建的系統,但是,由於 Eureka 服務端的服務治理機制提供了完備的 RESTful API,所以他也支持將非 Java 語言構建的微服務納入 Eureka 的服務治理體系中來。

一、開發工具說明

  • 開發工具:Spring Tool Suite 3.9.4.RELEASE

  • JDK版本:1.8.0_162

  • MAVEN版本:3.5.3

二、創建Eureka注冊中心

New Spring Starter Project

選擇 Eureka Server ,點擊finish項目創建完成

此時pom.xml內容為:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <groupId>com.carry.springcloud</groupId>
 7     <artifactId>eureka-server</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>eureka-server</name>
12     <description>Demo project for Spring Boot</description>
13 
14     <parent>
15         <groupId>org.springframework.boot</groupId>
16         <artifactId>spring-boot-starter-parent</artifactId>
17         <version>2.0.4.RELEASE</version>
18         <relativePath/> <!-- lookup parent from repository -->
19     </parent>
20 
21     <properties>
22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24         <java.version>1.8</java.version>
25         <spring-cloud.version>Finchley.SR1</spring-cloud.version>
26     </properties>
27 
28     <dependencies>
29         <dependency>
30             <groupId>org.springframework.cloud</groupId>
31             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
32         </dependency>
33 
34         <dependency>
35             <groupId>org.springframework.boot</groupId>
36             <artifactId>spring-boot-starter-test</artifactId>
37             <scope>test</scope>
38         </dependency>
39     </dependencies>
40 
41     <dependencyManagement>
42         <dependencies>
43             <dependency>
44                 <groupId>org.springframework.cloud</groupId>
45                 <artifactId>spring-cloud-dependencies</artifactId>
46                 <version>${spring-cloud.version}</version>
47                 <type>pom</type>
48                 <scope>import</scope>
49             </dependency>
50         </dependencies>
51     </dependencyManagement>
52 
53     <build>
54         <plugins>
55             <plugin>
56                 <groupId>org.springframework.boot</groupId>
57                 <artifactId>spring-boot-maven-plugin</artifactId>
58             </plugin>
59         </plugins>
60     </build>
61 
62 
63 </project>

屬性文件配置

將resources目錄下的application.properties重命名為application.yml(建議使用格式化yaml語言,好處的話用過就知道),添加如下內容

server:
  port: 8761 #eureka默認端口號

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

解釋:

  • eureka.client.registerWithEureka:表示是否將自己注冊到Eureka Server,默認為true。由於當前應用就是Eureka Server,故而設置為false。
  • eureka.client.fetchRegistry:表示是否從Eureka Server獲取注冊信息,默認為true。因為這是一個單點的Eureka Server,不需要同步其他的Eureka Server節點的數據,故而設置為false。
  • eureka.client.serviceUrl.defalseZone:設置與Eureka Server交互的地址,查詢服務和注冊服務都需要依賴這個地址,多個地址可用逗號(英文的)分割。

啟動類配置

添加@EnableEurekaServer注解即可

 1 package com.carry.springcloud;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 6 
 7 @EnableEurekaServer
 8 @SpringBootApplication
 9 public class EurekaServerApplication {
10 
11     public static void main(String[] args) {
12         SpringApplication.run(EurekaServerApplication.class, args);
13     }
14 }

使用SpringBootApp啟動,訪問地址:localhost:8761

正常打開以上網頁,說明Eureka注冊中心配置成功。

三、創建客戶端項目

創建項目步驟同上,這里我們選擇Eureka Discovery、Web

pom.xml內容:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <groupId>com.carry.springcloud</groupId>
 7     <artifactId>service-eureka-client</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>service-eureka-client</name>
12     <description>Demo project for Spring Boot</description>
13 
14     <parent>
15         <groupId>org.springframework.boot</groupId>
16         <artifactId>spring-boot-starter-parent</artifactId>
17         <version>2.0.4.RELEASE</version>
18         <relativePath/> <!-- lookup parent from repository -->
19     </parent>
20 
21     <properties>
22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24         <java.version>1.8</java.version>
25         <spring-cloud.version>Finchley.SR1</spring-cloud.version>
26     </properties>
27 
28     <dependencies>
29         <dependency>
30             <groupId>org.springframework.boot</groupId>
31             <artifactId>spring-boot-starter-web</artifactId>
32         </dependency>
33         <dependency>
34             <groupId>org.springframework.cloud</groupId>
35             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
36         </dependency>
37 
38         <dependency>
39             <groupId>org.springframework.boot</groupId>
40             <artifactId>spring-boot-starter-test</artifactId>
41             <scope>test</scope>
42         </dependency>
43     </dependencies>
44 
45     <dependencyManagement>
46         <dependencies>
47             <dependency>
48                 <groupId>org.springframework.cloud</groupId>
49                 <artifactId>spring-cloud-dependencies</artifactId>
50                 <version>${spring-cloud.version}</version>
51                 <type>pom</type>
52                 <scope>import</scope>
53             </dependency>
54         </dependencies>
55     </dependencyManagement>
56 
57     <build>
58         <plugins>
59             <plugin>
60                 <groupId>org.springframework.boot</groupId>
61                 <artifactId>spring-boot-maven-plugin</artifactId>
62             </plugin>
63         </plugins>
64     </build>
65 
66 
67 </project>

屬性文件配置

application.yml內容如下:

server:
  port: 8080
spring:
  application:
    name: service-eureka-clienteureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

啟動類配置

添加@EnableEurekaClient注解即可

 1 package com.carry.springcloud;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 6 
 7 @EnableEurekaClient
 8 @SpringBootApplication
 9 public class ServiceEurekaClientApplication {
10 
11     public static void main(String[] args) {
12         SpringApplication.run(ServiceEurekaClientApplication.class, args);
13     }
14 }

啟動項目,如下圖,客戶端注冊成功

四、給Eureka添加安全認證

默認情況下我們就直接直接訪問到eureka的界面了。如果不想讓所有人都能訪問到eureka的界面,可以加上權限認證,輸入賬號密碼才能訪問。

服務端:

Eureka Server中pom文件添加spring-boot-starter-security依賴

1 <dependency>
2     <groupId>org.springframework.boot</groupId>
3     <artifactId>spring-boot-starter-security</artifactId>
4 </dependency>

yml文件內容修改為

server:
  port: 8761 #eureka默認端口號
spring:
  security:
    user:
      name: admin
      password: 123456
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/

啟動項目,訪問地址:localhost:8761,會提示我們輸入用戶名和密碼

輸入用戶名和密碼后就回進入Eureka的主頁面

客戶端:

 application.yml配置文件內容修改:

eureka:
  client:
    serviceUrl:
      defaultZone: http://admin:123456@localhost:8761/eureka/

在地址里加上用戶名密碼,然后運行還是報錯無法注冊到eureka,是因為新版本的security默認開啟csrf了,關掉就好了

服務端Eureka Server項目中新建一個配置類,如下

 1 package com.carry.springcloud.config;
 2 
 3 import org.springframework.context.annotation.Configuration;
 4 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
 5 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
 6 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
 7 
 8 @EnableWebSecurity
 9 @Configuration
10 public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
11 
12     @Override
13     protected void configure(HttpSecurity http) throws Exception {
14         http.csrf().disable(); // 關閉csrf
15         http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); // 開啟認證
16     }
17 
18 }

重新啟動eureka,訪問localhost:8761,出現不一樣的登錄提示

輸入用戶名和密碼登錄后,啟動client項目,此時再刷新eureka頁面,發現已經成功注冊!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM