1. 程式人生 > >第一篇:服務的註冊與發現Eureka(Finchley版本)

第一篇:服務的註冊與發現Eureka(Finchley版本)

size src [] plugin authent erl ava 成了 public

一 springcloud簡介

  目前的springcloud的版本 Finchley.RELEASE springboot的版本 2.0.3.RELEASE

Finchley版本的官方文檔如下:

http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html

spring cloud 為開發人員提供了快速構建分布式系統的一些工具,包括配置管理、服務發現、斷路器、路由、微代理、事件總線、全局鎖、決策競選、分布式會話等等。它運行環境簡單,可以在開發人員的電腦上跑。另外說明spring cloud是基於springboot的,所以需要開發中對springboot有一定的了解。另外對於“微服務架構” 不了解的話,可以通過搜索引擎搜索“微服務架構”了解下!

二 創建服務註冊中心

在這裏,我還是采用Eureka作為服務註冊與發現的組件,至於其他的服務發現組件之後會出文章詳細介紹。

2.1 首先創建一個maven主工程。

  首先創建一個主Maven工程,在其pom文件引入依賴,spring Boot版本為2.0.3.RELEASE,Spring Cloud版本為Finchley.RELEASE。這個pom文件作為父pom文件,起到依賴版本控制的作用,其他module工程繼承該pom。這一系列文章全部采用這種模式,其他文章的pom跟這個pom一樣。再次說明一下,以後不再重復引入

2.2 創建eureka-server Module

  技術分享圖片

  技術分享圖片

  技術分享圖片

  繼續next 創建eureka-server Module 其依賴如下:

  

<?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.hmzj</groupId> <artifactId>eureka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>eureka-server</name> <description>EurekaServer project for Spring Boot</description> <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> <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <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-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-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 //開啟EurekaServer 
@SpringBootApplication
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

2.3 Eureka是一個高可用的組件,它沒有後端緩存,每一個實例註冊之後需要向註冊中心發送心跳(因此可以在內存中完成),在默認情況下erureka server也是一個eureka client ,必須要指定一個 server。eureka server的配置文件appication.yml:

  

server:
  port: 8060
spring:
  application:
    name: eureka-server
eureka:
  server:
    enable-self-preservation: true # 測試時關閉自我保護機制,保證不可用服務及時踢出
  client:
    register-with-eureka: false 
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://127.0.0.1:8060/eureka/

通過eureka.client.registerWithEurekafalsefetchRegistryfalse來表明自己是一個eureka server.

2.4 eureka server 是有界面的,啟動工程,打開瀏覽器訪問: http://localhost:8060 ,界面如下:

技術分享圖片

No application available 沒有服務被發現 因為沒有註冊服務。

  到此一個簡單的服務端就完成了!

Eureka Server加上安全驗證

  1. 引入依賴 
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
</dependency>

  2.application.yml中添加配置

spring:
  security:
    user:
      name: **
      password: **

修改defaultZone: http://pyq:123456@localhost:8060/eureka/

serviceUrl格式:http://<username>:<password>@ip:port/eureka

  

# 此種方法已經過時   
security:    
  basic:    
    enabled: true    
  user:    
    name: admin  # 用戶名    
    password: admin123   # 用戶密碼

  3.由於Finchey版本默認開啟csrf 為了可以使用 http://${userName}:${password}@${host}:${port}/eureka/ 這種方式登錄,所以必須是httpBasic,寫一個配置類

package com.hmzj.eurekaserver;

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;

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
        http.csrf().disable(); //關閉csrf
        //註意:為了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 這種方式登錄,所以必須是httpBasic,如果是form方式,不能使用url格式登錄
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); //開啟認證
    }

}

  4.此時再重新啟動eureka-server 打開瀏覽器訪問: http://localhost:8060 就需要賬號和密碼了

四 高可用的服務註冊中心 (雙註冊節點)

  技術分享圖片

新建一個application-peer1.yml和application-peer2.yml

server:
  port: 8060
spring:
  application:
    name: eureka-server
  security:
    user:
      name: **
      password: **
  profiles: peer1
eureka:
  instance:
    hostname: peer1
  client:
    register-with-eureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://**:**@127.0.0.1:8061/eureka/ #註意端口


註意:在使用雙節點註冊時應在註冊時將 defaultZone: http://**:**@127.0.0.1:8061/eureka/ 的端口寫成另外一個節點的端口

server:
  port: 8061
spring:
  application:
    name: eureka-server
  security:
    user:
      name: **
      password: **
  profiles: peer2
eureka:
  instance:
    hostname: peer2
  client:
    register-with-eureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://**:**@127.0.0.1:8060/eureka/ #註意端口

按照官方文檔的指示,需要改變etc/hosts,linux系統通過vi /etc/hosts ,加上:
127.0.0.1 peer1

127.0.0.1 peer2

windows電腦,在c:/windows/systems/drivers/etc/hosts 修改

linux上將eureka-server打包 編譯後 運行jar包

nohup java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1 >/dev/null 2>&1 &

nohup java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2 >/dev/null 2>&1 &

技術分享圖片

技術分享圖片

完美註冊!

第一篇:服務的註冊與發現Eureka(Finchley版本)