1. 程式人生 > >《springcloud — Finchley.RELEASE版》第一篇 服務註冊與發現(eureka)

《springcloud — Finchley.RELEASE版》第一篇 服務註冊與發現(eureka)

構建高可用Eureka註冊中心

Eureka通過執行多個例項,使其更具有高可用性。

建立一個maven工程micro-erueka-server(micro-parent參見第一篇),pom.xml如下:

<?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>
	<artifactId>micro-eureka-server</artifactId>
	<packaging>jar</packaging>
	<name>micro-eureka-server</name>
	<description>微服務學習</description>	
	<parent>
		<groupId>com.sun</groupId>
		<artifactId>micro-parent</artifactId>
	    <version>0.0.1-SNAPSHOT</version>
	</parent>
	
	<dependencies>
	 	 <dependency>
	      	<groupId>org.springframework.cloud</groupId>
	      	<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
	     </dependency>
	</dependencies>
	
</project>

啟動類需要加上@EnableEurekaServer註解如下:

package com.sun.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

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

配置檔案application.yml中配置兩套profiles 8080埠的ha1和8081埠的ha2,這裡注意eureka.client.serviceUrl.defaultZone配置項的地址,不能使用localhost,要使用域名,自己修改hosts檔案即可,具體配置如下:

spring:
  application:
    name: micro-erueka-server
---
server:
  port: 8080
spring:
  profiles: ha1 
eureka:
  instance:
    hostname: peer1
  client:
    service-url:
      defaultZone: http://peer2:8081/eureka
---
server:
  port: 8081
spring:
  profiles: ha2 
eureka:
  instance:
    hostname: peer2
  client:
    service-url:
      defaultZone: http://peer1:8080/eureka

 eclipse 右擊run as configurations 配置兩套啟動配置profile分別為ha1,ha2

分別執行配置的ha1,ha2,訪問http://localhost:8080/,可以看到如下8081和8080已經註冊到了eureka server上,並且8080的available-replicas是8081,這裡如果出現在unavailable-replicas說明沒有形成高可用

為eureka新增使用者認證

pom.xml中新增依賴:


<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
</dependency>

application.yml修改為:

spring:
  security:  #設定登入認證
    user:
      name: user
      password: opop
      roles: USER
  application:
    name: micro-erueka-server
---
server:
  port: 8080
spring:
  profiles: ha1 
eureka:
  instance:
    hostname: peer1
  client:
    service-url:
      defaultZone: http://user:[email protected]:8081/eureka #這裡要使用使用者名稱密碼
---
server:
  port: 8081
spring:
  profiles: ha2 
eureka:
  instance:
    hostname: peer2
  client:
    service-url:
      defaultZone: http://user:[email protected]:8080/eureka #這裡要使用使用者名稱密碼

由於Finchey版本預設開啟csrf  為了可以使用 http://${userName}:${password}@${host}:${port}/eureka/ 這種方式登入,所以寫一個配置類關掉它

package com.sun.eureka.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;

@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.authorizeRequests().anyRequest().authenticated().and().httpBasic(); //開啟認證
    }

}

這時啟動再訪問,需要輸入使用者名稱密碼:

相關推薦

springcloudFinchley.RELEASE第一 服務註冊發現(eureka)

構建高可用Eureka註冊中心 Eureka通過執行多個例項,使其更具有高可用性。 建立一個maven工程micro-erueka-server(micro-parent參見第一篇),pom.xml如下: <?xml version="1.0" encoding=

Spring Cloud(二):服務註冊發現 EurekaFinchley

LEDE .com Go eureka clean 英文逗號 開始 效果 sam Spring Cloud(二):服務註冊與發現 Eureka【Finchley 版】 發表於 2018-04-15 | 更新於 2018-05-07 | 上一篇主要介紹了相關理論,這一

SpringCloud進擊 | 一淺出:服務註冊發現Eureka)【Finchley版本】

1.前言 Spring Cloud 已經幫我們實現了服務註冊中心,我們只需要很簡單的幾個步驟就可以完成。關於理論知識,我想大家都已經有不同程度上的瞭解和認識,這裡,我們最後再進行總結。本系列 Spring Cloud 介紹基於 Spring Boot 2.0.5 版本和 Spring C

Java後端愛上SpringCloud 第一節:服務註冊發現 Eureka

Java後端愛上SpringCloud 第一節:服務註冊和發現 Eureka 一些連結 構建服務註冊中心EurekaServer 構建服務提供者EurekaClient 整合SpringBoot做視覺化監控 PS:還是老規矩,一些概念的東

SpringCloud(二) 服務註冊發現Eureka

圖片 borde 引入 vat red images .cn epo round 1.eureka是幹什麽的? 上篇說了,微服務之間需要互相之間通信,那麽通信就需要各種網絡信息,我們可以通過使用硬編碼的方式來進行通信,但是這種方式

SpringCloud服務註冊發現Eureka

Eureka是Netflix開源的一款提供服務註冊和發現的產品,它提供了完整的Service Registry(註冊登記)和Service Discovery(發現)實現。也是springcloud體系中最重要最核心的元件之一。   服務中心:   服務中心又稱註冊中心,管理各種服務功能包括服務的註冊、發

4--SpringCloud搭建高可用的服務註冊中心 1--SpringCloud服務註冊發現Eureka 1--SpringCloud服務註冊發現Eureka 1--SpringCloud服務註冊發現Eureka

  我們已經介紹了1--SpringCloud的服務註冊與發現Eureka,其中,主要演示瞭如何構建和啟動服務註冊中心Eureka Server,以及如何將服務註冊到Eureka Server中,但是在之前的示例中,這個服務註冊中心是單點的,顯然這並不適合應用於線上生產環境,那麼下面在前文的基礎上,我們來看看

SpringCloud入門 ――服務註冊發現(Eureka)

  前言 Oracle轉讓Java,各種動態語言的曝光率上升,Java工程師的未來在哪裡?我覺得Spring Cloud讓未來有無限可能。拖了半年之久的Spring Cloud學習就從今天開始了。中文教材不多,而且大多都是簡單的離散的資訊,想要找到企業級的一體化解決方案很少。不過,

SpringCloud服務註冊發現Eureka

一、建立Eureka Server作為服務註冊中心 二、新增pom.xml檔案依賴 <!--eureka server --> <dependency> <groupId>org.springframework.cloud&l

1--SpringCloud服務註冊發現Eureka

Eureke   Eureka是Netflix開源的一款提供服務註冊和發現的產品,它提供了完整的Service Registry(註冊登記)和Service Discovery(發現)實現。也是springcloud體系中最重要最核心的元件之一。   服務中心:   服務中心又稱註冊中心,管理各種服務功

springcloud~服務註冊發現Eureka的使用

服務註冊與發現是微服務裡的概念,也是一個基本的元件,負責服務元件的認證,即實現『你是誰』的功能,在服務註冊與發現裡,存在兩種模式,即服務端發現和客戶端發現,咱們今天說的eureka屬於客戶端發現! 下面概念來自文章:https://blog.csdn.net/forezp/article/details/7

SpringCloud 服務註冊發現Eureka

個人學習SpringCloud系列 Eureka篇 Github Link: https://github.com/panjianlong13/SpringBoot-SpringCloud/tree/master/spring-cloud-eureka Spring Cloud簡介

SpringCloud(1)服務註冊發現Eureka

1.建立1個空白的工程 2.建立2個model工程 一個model(即SpringBoot)工程作為服務註冊中心,即Eureka Server,另一個作為Eureka Client。 Eureka Server建立完後的工程 pom.xml 檔案如下: <?xml version="1.0" e

SpringCloud 學習記錄(一):服務註冊發現(eureka+feign)

前面介紹過dubbo遠端介面呼叫的簡單使用,這裡我們將開始學習如何使用springcloud微服務架構中的服務註冊與發現。 一,eureka-server eureka分為服務端和客戶端兩部分, eureka server是一個服務註冊中心,類似於zookeeper,當

Spring Cloud 學習 | 第一服務註冊發現

一、簡介 微服務架構當中,最總要的就是服務的提供者和消費者。消費者消費提供者提供的服務,但是消費者不應該直接呼叫提供者的服務,因為這樣的話,各個子系統之間的耦合性太強,監控,容災,負載均衡這些功能實現起來就很困難。所以需要引入註冊中心的概念,服務提供者把自己提

0408-服務註冊發現-Eureka常用配置

範圍 heal enabled ash 及其 流量 nds ble 地址 一、概述 參看地址:https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_appendi

服務註冊發現-Eureka (高可用設計)

功能 num -a 特點 解決方案 一個 subst 同步 進程 什麽是高可用 部署需要考慮的是什麽: 1、系統遇到單點失效問題,如何能夠快速切換到其他節點完成任務 2、如何應對網絡故障,即系統如何設計成“故障開放型”(expecting failure)的 典型的問題包括

Spring Cloud構建微服務架構:服務註冊發現 Eureka

Spring Cloud構建微服務架構:服務註冊與發現Eureka 【Dalston版】 原創   2018-04-10  宗野   Spring Cloud 已經

Spring Cloud 之 服務註冊發現 Eureka元件,Eureka Server叢集

Eureka是Netflix公司開源的一個服務註冊與發現元件,類似的元件還有Zookeeper、Consul。 Eureka分為Eureka Server(服務註冊中心,每個服務都在這裡註冊資訊,提供服務名、IP、埠等資訊)和Eureka Client(客戶端

Spring Cloud 探索 | 服務註冊發現 Eureka(下)

接上文 …… 注意:以下是針對Eureka Client進行配置,如果需要對Eureka Server進行配置會有明顯的說明。 4 Spring Cloud Eureka Client 配置過程 4.1 pom.xml 新增依賴 新增Eureka Client依賴: