Ribbon【入門】

来源:https://www.cnblogs.com/idoljames/archive/2019/10/17/11687359.html
-Advertisement-
Play Games

公共依賴: 1、創建eureka-server註冊中心工程 a、eureka-server工程pom依賴: b、eureka-server啟動類: c、eureka-server工程配置文件:eureka-server\src\main\resources\bootstrap.yml d、啟動註冊中 ...


公共依賴:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
</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>

<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>

 

1、創建eureka-server註冊中心工程

a、eureka-server工程pom依賴:

<!--加上上面的公共依賴-->

<
dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

 

b、eureka-server啟動類:

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

 

c、eureka-server工程配置文件:eureka-server\src\main\resources\bootstrap.yml

server:
  port: 8888
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

 

d、啟動註冊中心

mvn spring-boot:run

 

2、創建demo-client服務工程

a、demo-client工程pom配置:

<!--加上上面的公共依賴-->

<
dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

 

b、demo-client啟動類:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

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

 

c、demo-client工程配置文件:demo-client\src\main\resources\bootstrap.yml

server:
  port: 7070
spring:
  application:
    name: client
eureka:
  client:
    serviceUrl:
      defaultZone: http://${eureka.host:127.0.0.1}:${eureka.port:8888}/eureka/
  instance:
    prefer-ip-address: true

 

d、編寫測試介面:

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 測試API
 */
@RestController
public class TestController {

    @GetMapping("/testApi")
    public String testApi(Integer a, Integer b, HttpServletRequest request) {
        return " From Port: " + request.getServerPort() + ", Result: " + (a + b);
    }
}

 

e、啟動工程

mvn spring-boot:run 

mvn spring-boot:run -Dserver.port=7171

啟動了2個工程實例,一個埠是7070,一個是7171

 

3、創建demo-ribbon客戶端工程 

a、demo-ribbon工程pom依賴:

<!--加上上面的公共依賴-->

<
dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

 

b、demo-ribbon工程啟動類:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class RibbonLoadbalancerApplication {

    public static void main(String[] args) {
        SpringApplication.run(RibbonLoadbalancerApplication.class, args);
    }
    
   //聲明該RestTemplate用於負載均衡 @Bean @LoadBalanced
public RestTemplate restTemplate() { return new RestTemplate(); } }

 

c、編寫一個測試介面:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class TestController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/testApi")
    public String testApi(Integer a, Integer b) {
     //使用RestTemplate調用demo-client工程暴露的測試API介面 String result
= restTemplate.getForObject("http://CLIENT/testApi?a=" + a + "&b=" + b, String.class); System.out.println(result); return result; } }

 

d、demo-ribbon工程配置文件:demo-ribbon\src\main\resources\bootstrap.yml

spring:
  application:
    name: ribbon-loadbalancer
server:
  port: 7777
eureka:
  client:
    serviceUrl:
      defaultZone: http://${eureka.host:127.0.0.1}:${eureka.port:8888}/eureka/
  instance:
    prefer-ip-address: true

 

e、啟動demo-ribbon工程:

mvn spring-boot:run

 

4、訪問 localhost:7777/testApi?a=2019&=2020

從結果可以看到這次訪問的是7171埠服務。

從下麵的控制台輸出可以看出,ribbon在做負載均衡的時候,預設使用的是輪詢的方式!

 

 入門成功!!!


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 占位符 %s 字元串 %d 整型 %f 浮點型 判斷變數名是不是數字 if 變數名.isdigit(): for...else 和 while...else 當for迴圈與while迴圈正常退出時,則執行else里的語句,非正常退出時(比如break),則不執行else里的語句 列表 格式 列表名 ...
  • 2019-10-17-21:18:33 方法 定義格式: public static void 方法名稱() { 方法體 } 完整格式: 修飾符 返回值類型 方法名稱(參數類型 參數名稱,...){ 方法體 return 返回值; } 修飾符:現階段的固定寫法,public. static 返回值類 ...
  • PHP-FPM的錯誤日誌建議打開,這樣可以看到PHP的錯誤信息:一般是這個配置路徑 /etc/php/7.3/fpm/pool.d/www.conf,日誌目錄如果需要自己建立PHP目錄,一定要把許可權賦給www-data用戶,否則沒有創建目錄的許可權,就無法記錄日誌chown www-data:www- ...
  • 本文源碼: "GitHub·點這裡" || "GitEE·點這裡" 一、解釋器模式 1、基礎概念 解釋器模式是對象的行為模式。給定一個語言之後,解釋器模式可以定義出其文法的一種表示,並同時提供一個解釋器。客戶端可以使用這個解釋器來解釋這個語言中的表達式。 2、模式圖解 3、核心角色 (1)、抽象表達 ...
  • 1. 安裝 輸入 pip install PIL報錯: ERROR: Could not find a version that satisfies the requirement PIL (from versions: none) ERROR: No matching distribution f ...
  • 1. JavaScript的概述 1.1 什麼是JavaScript JavaScript是web上一種功能強大的編程語言,用於開發互動式的web頁面。它不需要進行編譯,而是直接嵌入在HTML頁面中,由瀏覽器執行。 JavaScript被設計用來向HTML頁面添加交互行為。 JavaScript是一 ...
  • 用途 Clock函數可以有效地針對一些只能用隨機化做的題目 為了提高該類代碼的正確性,我們期望它運行的次數在要求時限內運行足夠多 因此將Clock函數充當計時器 調用 Clock函數所在頭文件ctime/time.h ClOCKS_PER_SEC為常量 時長的計算: 註:begin在程式開頭進行賦值 ...
  • 使用maven搭建Hibernate框架(web項目) 1 create table USERS 2 ( 3 ID NUMBER not null primary key, 4 NAME VARCHAR2(50), 5 PASSWORD VARCHAR2(50), 6 TELEPHONE VARCH ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...