基於 Spring Cloud 完整的微服務架構實戰

 

本項目是一個基於 Spring Boot、Spring Cloud、Spring Oauth2 和 Spring Cloud Netflix 等框架構建的微服務項目。php

@做者:Sheldon地址:https://github.com/zhangxd1989java

技術棧

  • Spring boot - 微服務的入門級微框架,用來簡化 Spring 應用的初始搭建以及開發過程。
  • Eureka - 雲端服務發現,一個基於 REST 的服務,用於定位服務,以實現雲端中間層服務發現和故障轉移。
  • Spring Cloud Config - 配置管理工具包,讓你能夠把配置放到遠程服務器,集中化管理集羣配置,目前支持本地存儲、Git 以及 Subversion。
  • Hystrix - 熔斷器,容錯管理工具,旨在經過熔斷機制控制服務和第三方庫的節點,從而對延遲和故障提供更強大的容錯能力。
  • Zuul - Zuul 是在雲平臺上提供動態路由,監控,彈性,安全等邊緣服務的框架。Zuul 至關因而設備和 Netflix 流應用的 Web 網站後端全部請求的前門。
  • Spring Cloud Bus - 事件、消息總線,用於在集羣(例如,配置變化事件)中傳播狀態變化,可與 Spring Cloud Config 聯合實現熱部署。
  • Spring Cloud Sleuth - 日誌收集工具包,封裝了 Dapper 和 log-based 追蹤以及 Zipkin 和 HTrace 操做,爲 SpringCloud 應用實現了一種分佈式追蹤解決方案。
  • Ribbon - 提供雲端負載均衡,有多種負載均衡策略可供選擇,可配合服務發現和斷路器使用。
  • Turbine - Turbine 是聚合服務器發送事件流數據的一個工具,用來監控集羣下 hystrix 的 metrics 狀況。
  • Spring Cloud Stream - Spring 數據流操做開發包,封裝了與 Redis、Rabbit、Kafka 等發送接收消息。
  • Feign - Feign 是一種聲明式、模板化的 HTTP 客戶端。
  • Spring Cloud OAuth2 - 基於 Spring Security 和 OAuth2 的安全工具包,爲你的應用程序添加安全控制。

應用架構

該項目包含 8 個服務git

  • registry - 服務註冊與發現
  • config - 外部配置
  • monitor - 監控
  • zipkin - 分佈式跟蹤
  • gateway - 代理全部微服務的接口網關
  • auth-service - OAuth2 認證服務
  • svca-service - 業務服務A
  • svcb-service - 業務服務B

體系架構

 

architecture

 

應用組件

 

components

 

啓動項目

  • 使用 Docker 快速啓動
    1. 配置 Docker 環境
    2. mvn clean package 打包項目及 Docker 鏡像
    3. 在項目根目錄下執行 docker-compose up -d 啓動全部項目
  • 本地手動啓動
    1. 配置 rabbitmq
    2. 修改 hosts 將主機名指向到本地
      127.0.0.1 registry config monitor rabbitmq auth-service
      或者修改各服務配置文件中的相應主機名爲本地 ip
    3. 啓動 registry、config、monitor、zipkin
    4. 啓動 gateway、auth-service、svca-service、svcb-service

項目預覽

註冊中心

訪問 http://localhost:8761/ 默認帳號 user,密碼 passwordgithub

 

registry

 

監控

訪問 http://localhost:8040/ 默認帳號 admin,密碼 admindocker

控制面板

 

monitor

 

應用註冊歷史

 

monitor

 

Turbine Hystrix面板

 

monitor

 

應用信息、健康情況、垃圾回收等詳情

 

monitor

 

計數器

 

monitor

 

查看和修改環境變量

 

monitor

 

管理 Logback 日誌級別

 

monitor

 

查看並使用 JMX

 

monitor

 

查看線程

 

monitor

 

認證歷史

 

monitor

 

查看 Http 請求軌跡

 

monitor

 

Hystrix 面板

 

monitor

 

鏈路跟蹤

訪問 http://localhost:9411/ 默認帳號 admin,密碼 adminjson

控制面板

 

zipkin

 

鏈路跟蹤明細

 

zipkin

 

服務依賴關係

 

zipkin

 

RabbitMQ 監控

Docker 啓動訪問 http://localhost:15673/ 默認帳號 guest,密碼 guest(本地 rabbit 管理系統默認端口15672)後端

 

rabbit

 

接口測試

  1. 獲取 Token
curl -X POST -vu client:secret http://localhost:8060/uaa/oauth/token -H "Accept: application/json" -d "password=password&username=anil&grant_type=password&scope=read%20write" 

返回以下格式數據:安全

  1.  
    {
  2.  
    "access_token": "eac56504-c4f0-4706-b72e-3dc3acdf45e9",
  3.  
    "token_type": "bearer",
  4.  
    "refresh_token": "da1007dc-683c-4309-965d-370b15aa4aeb",
  5.  
    "expires_in": 3599,
  6.  
    "scope": "read write"
  7.  
    }
  1. 使用 access token 訪問 service a 接口
curl -i -H "Authorization: Bearer eac56504-c4f0-4706-b72e-3dc3acdf45e9" http://localhost:8060/svca 

返回以下數據:ruby

  1.  
    svca-service ( 172.18.0.8:8080)===>name:zhangxd
  2.  
    svcb-service ( 172.18.0.2:8070)===>Say Hello
  1. 使用 access token 訪問 service b 接口
curl -i -H "Authorization: Bearer eac56504-c4f0-4706-b72e-3dc3acdf45e9" http://localhost:8060/svcb 

返回以下數據:服務器

svcb-service (172.18.0.2:8070)===>Say Hello 
  1. 使用 refresh token 刷新 token
curl -X POST -vu client:secret http://localhost:8060/uaa/oauth/token -H "Accept: application/json" -d "grant_type=refresh_token&refresh_token=da1007dc-683c-4309-965d-370b15aa4aeb" 

返回更新後的 Token:

  1.  
    {
  2.  
    "access_token": "63ff57ce-f140-482e-ba7e-b6f29df35c88",
  3.  
    "token_type": "bearer",
  4.  
    "refresh_token": "da1007dc-683c-4309-965d-370b15aa4aeb",
  5.  
    "expires_in": 3599,
  6.  
    "scope": "read write"
  7.  
    }
  1. 刷新配置
curl -X POST -vu user:password http://localhost:8888/bus/refresh 
 

 

 

 

 

 

轉自:https://blog.csdn.net/m0_37803704/article/details/79677111

相關文章
相關標籤/搜索