x213212
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# 目前架構 ![](https://i.imgur.com/RKIxarj.png) 可以看到我們下面 服務好像有固定的數據庫可能是集群或是什麼 在前面的學習好像沒看到更複雜的調用,分布式很多問題,像是數據不同步,光是鎖就有一大堆,就以認證中心設計成微服務的話, 資料庫就會變成多個,這樣就不能判斷你到底是到底在哪一台調用,而出現你有雙重的登入資料緩存在分散的資料庫解決方式可能有 一 分布式鎖 二共用同一個資料庫 這部分先以後有空再弄,目前就是先拓展為多層調用,今天是來實現多層的話服務溝通使用 **resttemplate** 去溝通。 也就是當服務變成這樣 ![](https://i.imgur.com/HhGBdIU.png) # 多層服務的呼叫方式 調用服務三 但是要取得服務四 的 response 才算調用完成 那就變成 要完成server3 呼叫就要 server3 -> server 4 多經過一層 server 4 這樣的話 可能會覺得我那就用我們上次 doget doposs 這兩種方法就好了那有沒有更優雅的方式呢有的歐, spring 幫我們把這些東西再往上封裝了一層也就是 **restemplate** (如果你想用到更複雜的調用你就自己從底層慢慢研究了) # 分析 我們把服務擴展為兩層一下 ![](https://i.imgur.com/tR6IJ2Z.png) 也就是我們要關注的 ![](https://i.imgur.com/MQSS36D.png) 好吧開始 目前我們的 service 已經有 7個 我們新增了 兩個 一個是 EurekaServiceFeignConsumer2 EurekaServiceFeignProvider3 其中兩個各別都是從 EurekaServiceFeignProvider EurekaServiceFeignConsumer 複製出來 來看一下代碼 那麼我們原本的服務會變怎樣呢 EurekaServiceFeignProvider1 EurekaServiceFeignProvider2 要調用我們的 EurekaServiceFeignProvider3 所以調用順序就是 Zuul - > Consumer1 - > Provider1 or Provider2 -> Consumer2 > Provider3 所以先針對我們的 Consumer1 來修改 # Consumer1 Controller 我們新增了 ```java @GetMapping("/helloEX/{id}/{id2}") public String helloEX(@PathVariable(name="id") Integer employeeId,@PathVariable(name="id2") Integer employeeId2) { System.out.print(employeeId2); String message = homeClient.home2(employeeId,employeeId2); logger.info("[eureka-fegin][ConsumerController][hello], message={}", message); // log.info("[eureka-ribbon][EurekaRibbonConntroller][syaHello], message={}", message); return message ; ``` 好了之後 我們要看要調用哪一層就是我們的 # Consumer1 homeClient ```java @GetMapping("/ep1") public String home2(@RequestParam (value="id", required = false) Integer employeeId,@RequestParam (value="id2", required = false) Integer employeeId2) ; ``` # Provider 1 or 2 這邊就要稍微用到前面的技巧了 我們知道有 resttemplete 有可以去跟我們 Eureka 去要我們已經註冊的節點的伺服器名字 整體調用在最下面 https://openhome.cc/Gossip/Spring/RestTemplate.html https://www.jianshu.com/p/462790156554 我們用到比較關鍵的是 預設的 **restemplete** 是沒有辦法呼叫負載平衡的,有印象的話在前幾天用過 [SPRING CLOUD 微服務入門 (三) EUREKA + CONSUMER (FEIGN) 調用 SERVICE](https://x8795278.blogspot.com/2019/12/spring-cloud-eureka-consumer-feign.html) 在這邊的話我們把 **restemplete** 注入一個有 loadblance 特性 https://blog.csdn.net/qq_18416057/article/details/79432504 這邊說的蠻清楚的 不過少了一些東西 ```java @LoadBalanced @Bean RestTemplate restTemplate() { return new RestTemplate(); } @Autowired private RestTemplate restTemplate; ``` 總而言之就是讓我們的 **restemplete**有可以 去 訪問 Eureka 的能力 # 配置 provider application.yml ```yml eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ register-with-eureka : true <====就是這行 讓我們的 的 服務可以去請求其他已經註冊的伺服器 ``` 那麼現在我們的 **restemplete** loadblance 特性 # 細看 Provider 這邊要注意要跟我們的 Consumer1 homeClient 裡面的 method 相對應,這邊可以看到我們對伺服器的 ip已經不是像我們之前的在寫註冊中心那樣直接doget dopost 去呼叫,**restemplete** ,當然也可以用這種方式去呼叫我們的註冊中心,只不過,有空再寫好了這邊做兩個範例。 ```java @GetMapping("/ep1") public String home2(@RequestParam (value="id", required = false) final Integer employeeId,@RequestParam (value="id2", required = false) final Integer employeeId2) { final String message = "Hello world" + port+ employeeId+employeeId2; logger.info("[eureka-provide][EurekaServiceProviderApplication][home], message={}", message); // restTemplate = new RestTemplate(); final String fooResourceUrl = "http://feign-consumer2"; final ResponseEntity<String> response = restTemplate.getForEntity( fooResourceUrl + "/hello2/" + employeeId.toString() + "/" + employeeId2.toString(), String.class); //assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); System.out.println(response.getStatusCode().toString()+(HttpStatus.OK).toString()); System.out.println(response.getBody()+"test"); return response.getBody(); } ``` 好了之後複製到 Provider2 兩個 Controller 一樣。 # Provider3 Controller 反正 他是複製 Provider 我們直接看源碼 這邊很簡單沒做什麼我們看 Consumer2 ```java @GetMapping("/ep2") public String home(@RequestParam (value="id", required = false) Integer employeeId,@RequestParam (value="id2", required = false) Integer employeeId2) { String message = "Hello world" + port+ employeeId+employeeId2; logger.info("[eureka-provide2][EurekaServiceProviderApplication][home], message={}", message); return message; } ``` # Provider 3 配置文件 記得換為 eureka-provider2 ,以便讓我們的 Consumer2 可以調用到 ``` name: eureka-provider2 ``` # Consumer2 Controller 注意看 Url這邊可以看到 我們沒有再讓往Zuul 去訪問我們的 consumer2 了 因為我們是讓 Provider 去直接 調用 我們的 consumer2 , 可以注意 url 的調用 address 裡面的 hello2 ```java @RestController public class ConsumerController { private final Logger logger = LoggerFactory.getLogger(ConsumerController.class); @Autowired private HomeClient homeClient; @GetMapping("/hello2/{id}/{id2}") public String hello(@PathVariable(name="id") Integer employeeId,@PathVariable(name="id2") Integer employeeId2) { System.out.print(employeeId2); String message = homeClient.home(employeeId,employeeId2); logger.info("[eureka-fegin2][ConsumerController][hello], message={}", message); // log.info("[eureka-ribbon][EurekaRibbonConntroller][syaHello], message={}", message); return message ; } } ``` # Consumer2 homeClient 可以注意我們的 這邊就是填 Provider3 註冊到 Eureka的名字 也就是 **eureka-provider2** ```java @FeignClient(value ="eureka-provider2",fallbackFactory=HystrixClientFallbackFactory.class) public interface HomeClient { @GetMapping("/ep2") public String home(@RequestParam (value="id", required = false) Integer employeeId,@RequestParam (value="id2", required = false) Integer employeeId2) ; } ``` 好了都沒問題的話 就 啟動 吧! # 啟動 前面步驟就不說了 ![](https://i.imgur.com/dm2GHVg.png) 可以看到我訪問的是 helloEx ```url http://localhost:9000/feign-consumer/helloEX/1/12?token=Bearer%20eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1Nzc5MTUxNTksInVzZXJuYW1lIjoiamFjayJ9.UMUZWzh_ZTkhKxoSBfglSualToOTn9dm-WE9D3_huT4 ``` 這邊會覺得,有沒有更視覺化的東西可以看阿 別忘了我們的 **Zipkin** 就是在做這些事,我們打開 花了點時間 我把 zipkin 弄進去 docker-compose了,請注意 zipkin 啟動順序有差 ```bash version: '2' services: elasticsearch: build: context: elasticsearch/ volumes: - ./elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml:ro ports: - "9200:9200" - "9300:9300" environment: ES_JAVA_OPTS: "-Xmx256m -Xms256m" networks: - elk logstash: build: context: logstash/ volumes: - ./logstash/config/logstash.yml:/usr/share/logstash/config/logstash.yml:ro - ./logstash/pipeline:/usr/share/logstash/pipeline:ro ports: - "5000:5000" environment: LS_JAVA_OPTS: "-Xmx256m -Xms256m" networks: - elk depends_on: - elasticsearch kibana: build: context: kibana/ volumes: - ./kibana/config/:/usr/share/kibana/config:ro ports: - "5601:5601" networks: - elk depends_on: - elasticsearch rabbitmq : build: context: rabbitmq / volumes: - ./rabbitmq/config/:/usr/share/rabbitmq/config:ro ports: - "5672:5672" - "15672:15672" networks: - elk depends_on: - elasticsearch zipkin: build: context: zipkin / volumes: - ./zipkin/config/:/usr/share/zipkin/config:ro # Environment settings are defined here https://github.com/openzipkin/zipkin/tree/1.19.0/zipkin-server#environment-variables environment: - RABBIT_ADDRESSES=192.168.99.100:5672 - RABBIT_USER=guest - RABBIT_PASSWORD=guest - RABBIT_QUEUE=zipkin - SELF_TRACING_ENABLED=false #- RABBIT_VIRTUAL_HOST=/admin_host - JAVA_OPTS=-Dlogging.level.zipkin #=DEBUG -Dlogging.level.zipkin2=DEBUG ports: # Port used for the Zipkin UI and HTTP Api - "9411:9411" # Uncomment if you set SCRIBE_ENABLED=true # - 9410:9410 networks: - elk depends_on: - elasticsearch networks: elk: driver: bridge ``` 應該每次初次啟動 zipkin都會找不到 rabbitmq 而退出 ``` docker start "zipkin name" ``` ![](https://i.imgur.com/937NaX4.png) 服務啟動 ![](https://i.imgur.com/DRqjfjS.png) ![](https://i.imgur.com/iARveAz.png) 好了之後 我們的追蹤鏈也很清楚 ![](https://i.imgur.com/4TdzDv6.png)

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully