springboot+cloud 學習(一)高可用服務注冊中心(Eureka)


先說說Eureka

Eureka是Netflix開發的服務發現框架,本身是一個基於REST的服務,主要用於定位運行在AWS域中的中間層服務,以達到負載均衡和中間層服務故障轉移的目的。SpringCloud將它集成在其子項目spring-cloud-netflix中,以實現SpringCloud的服務發現功能。
 
Eureka包含兩個組件: Eureka ServerEureka Client
Eureka Server提供服務注冊服務,各個節點啟動后,會在Eureka Server中進行注冊,這樣EurekaServer中的服務注冊表中將會存儲所有可用服務節點的信息,服務節點的信息可以在界面中直觀的看到。
Eureka Client是一個java客戶端,用於簡化與Eureka Server的交互,客戶端同時也就別一個內置的、使用輪詢(round-robin)負載算法的負載均衡器。
 
在應用啟動后,將會向Eureka Server發送心跳,默認周期為30秒,如果Eureka Server在多個心跳周期內沒有接收到某個節點的心跳,Eureka Server將會從服務注冊表中把這個服務節點移除(默認90秒)。
Eureka Server之間通過復制的方式完成數據的同步,Eureka還提供了客戶端緩存機制,即使所有的Eureka Server都掛掉,客戶端依然可以利用緩存中的信息消費其他服務的API。綜上,Eureka通過 心跳檢查、客戶端緩存等機制,確保了系統的高可用性、靈活性和可伸縮性。
 

服務注冊中心高可用

簡單點說就是啟用多個Eureka Server(一般2個就可以了,我這里用了3個),然后讓3個Server 互相注冊。

 

創建服務注冊中心

創建一個普通的Spring Boot工程

首先我們需要創建一個普通的Spring Boot工程,命名為eureka-server。

添加Eureka依賴

工程創建成功之后,向pom.xml文件中添加eureka-server的依賴(這里有個javaJDK的坑,1.8版本以上的會有報錯,需要額外添加依賴),添加完依賴之后,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>eureka-server</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <!-- 必須要引入 springboot parent ,幫我們實現了很多jar包的依賴管理 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <dependencyManagement>
        <dependencies>
            <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-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>
        <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-netflix-eureka-server</artifactId>
        </dependency>

      <!-- 用於安全校驗 -->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
      </dependency>

        <!-- JDK 版本1.8以上的需要額外添加依賴 -->
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

 

 

啟動一個服務注冊中心

啟動一個服務注冊中心的方式很簡單,就是在Spring Boot的入口類上添加一個@EnableEurekaServer注解,如下: 

@EnableEurekaServer
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

 

配置服務注冊中心

在Spring Boot的配置文件application.yml中(如果3個Eureka Server的端口分別是8761、8762、8763,以8761為例,這里有個坑,開始yml配置里,

serviceUrl沒有用駝峰式的,服務一直注冊不上去),另外這里配置的hostname,記得改系統的host文件,配置對應的ip地址,server 端yml寫法如下:
 
spring:
  application:
    name: eureka-server2
  #安全登入用戶設置
  security:
    user:
      name: skyworth
      password: skyworth2
server:
  port: 8762
eureka:
  instance:
    hostname: eureka-server2
  client:
    #表示是否將自己注冊到Eureka Server
    register-with-eureka: false
    #表示是否從Eureka Server獲取注冊信息
    fetch-registry: false
    serviceUrl:
      #設置與Eureka Server交互的地址,查詢服務和注冊服務都需要依賴這個地址
      defaultZone: http://skyworth:skyworth1@eureka-server:8761/eureka/,http://skyworth:skyworth3@eureka-server3:8763/eureka/

 

到這里 Server 端就差不多了,下面是Cline端,Cline端的依賴基本是一樣的,主要區別在啟動類和配置文件的區別

client端yml配置如下:

spring:
  application:
    name: eureka-client
server:
  port: 8760
eureka:
  instance:
    hostname: eureka-client
  client:
    serviceUrl:
      defaultZone: http://skyworth:skyworth1@eureka-server:8761/eureka/,http://skyworth:skyworth2@eureka-server2:8762/eureka/,http://skyworth:skyworth3@eureka-server3:8763/eureka/

 

 client端啟動類:

@SpringBootApplication
@EnableDiscoveryClient public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

測試

項目啟動后 查看eureka-server: http://localhost:8761/,http://localhost:8762/,http://localhost:8763/,發現服務已經注冊進去。

另外提一下,為了安全,client端可以增加至多個,並增加用戶名,密碼的登入驗證。

 注:當增加用戶密碼的登入模式后,可能會出現服務注冊不進去的情況,解決方案,在eurka服務中添加一個安全認證類

/**
 * 
 */
package com.skyworth.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;

/**   
* Copyright: Copyright (c) 2018 skyworth
* 
* @ClassName: WebSecurityConfig.java
* @Description: 該類的功能描述
*
* @version: v1.0.0
* @author: Administrator
* @date: 2018年8月1日 下午4:14:12 
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
* 2018年8月1日     Administrator           v1.0.0               修改原因
*/
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * 高版本的丟棄了 
     * 
     * security: 
     *   basic: 
     *    enabled: true 
     * 
     * 配置,應該使用以下方式開啟
     *
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // Configure HttpSecurity as needed (e.g. enable http basic).
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
        http.csrf().disable();
        //注意:為了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 這種方式登錄,所以必須是httpBasic,
        // 如果是form方式,不能使用url格式登錄
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }
}

 

 


免責聲明!

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



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