国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

云原生微服務(wù) 第五章 Spring Cloud Netflix Eureka集成負(fù)載均衡組件Ribbon

這篇具有很好參考價(jià)值的文章主要介紹了云原生微服務(wù) 第五章 Spring Cloud Netflix Eureka集成負(fù)載均衡組件Ribbon。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

系列文章目錄

第一章 Java線程池技術(shù)應(yīng)用
第二章 CountDownLatch和Semaphone的應(yīng)用
第三章 Spring Cloud 簡(jiǎn)介
第四章 Spring Cloud Netflix 之 Eureka
第五章 Spring Cloud Netflix 之 Ribbon

云原生微服務(wù) 第五章 Spring Cloud Netflix Eureka集成負(fù)載均衡組件Ribbon,Java微服務(wù),云原生,微服務(wù),ribbon,Eureka,負(fù)載均衡,微服務(wù)治理,原力計(jì)劃


前言

Spring Cloud Ribbon 是一套基于 Netflix Ribbon 實(shí)現(xiàn)的客戶端負(fù)載均衡和服務(wù)調(diào)用工具,其主要功能是提供客戶端的負(fù)載均衡算法和服務(wù)調(diào)用。

今天我們以電商微服務(wù)為例,來講解Eureka 、Ribbon在微服務(wù)治理方面的實(shí)戰(zhàn)應(yīng)用。
云原生微服務(wù) 第五章 Spring Cloud Netflix Eureka集成負(fù)載均衡組件Ribbon,Java微服務(wù),云原生,微服務(wù),ribbon,Eureka,負(fù)載均衡,微服務(wù)治理,原力計(jì)劃
云原生微服務(wù) 第五章 Spring Cloud Netflix Eureka集成負(fù)載均衡組件Ribbon,Java微服務(wù),云原生,微服務(wù),ribbon,Eureka,負(fù)載均衡,微服務(wù)治理,原力計(jì)劃

1、負(fù)載均衡

負(fù)載均衡(Load Balance) ,簡(jiǎn)單點(diǎn)說就是將用戶的請(qǐng)求平攤分配到多個(gè)服務(wù)器上運(yùn)行,以達(dá)到擴(kuò)展服務(wù)器帶寬、增強(qiáng)數(shù)據(jù)處理能力、增加吞吐量、提高網(wǎng)絡(luò)的可用性和靈活性的目的。
常見的負(fù)載均衡方式有兩種:服務(wù)端負(fù)載均衡、客戶端負(fù)載均衡

1.1、服務(wù)端負(fù)載均衡

云原生微服務(wù) 第五章 Spring Cloud Netflix Eureka集成負(fù)載均衡組件Ribbon,Java微服務(wù),云原生,微服務(wù),ribbon,Eureka,負(fù)載均衡,微服務(wù)治理,原力計(jì)劃

1.2、客戶端負(fù)載均衡

云原生微服務(wù) 第五章 Spring Cloud Netflix Eureka集成負(fù)載均衡組件Ribbon,Java微服務(wù),云原生,微服務(wù),ribbon,Eureka,負(fù)載均衡,微服務(wù)治理,原力計(jì)劃

2、Ribbon實(shí)現(xiàn)服務(wù)間調(diào)用

Ribbon 可以與 RestTemplate(Rest 模板)配合使用,以實(shí)現(xiàn)微服務(wù)之間的調(diào)用
示例:
建立C端API工程customer-api

2.1、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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.hqyj</groupId>
        <artifactId>SpringCloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>customer-api</artifactId>
    <name>customer-api</name>
    <description>customer-api</description>

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--devtools 開發(fā)工具-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <!--Spring Boot 測(cè)試-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--junit 測(cè)試-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!-- 修改后立即生效,熱部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
            <version>1.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.hqyj</groupId>
            <artifactId>common-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

2.2、application.yml配置

server:
  port: 80

eureka:
  client:
    register-with-eureka: false #本微服務(wù)為服務(wù)消費(fèi)者,不需要將自己注冊(cè)到服務(wù)注冊(cè)中心
    fetch-registry: true  #本微服務(wù)為服務(wù)消費(fèi)者,需要到服務(wù)注冊(cè)中心搜索服務(wù)
    service-url:
      defaultZone: http://localhost:7001/eureka
      

2.3、bean配置類

配置RestTemplate、開啟負(fù)載均衡

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/***
 * @title bean配置類
 * @desctption 配置RestTemplate、開啟負(fù)載均衡
 * @author kelvin
 * @create 2023/5/11 14:33
 **/
@Configuration
public class ConfigBean {
    @Bean //將 RestTemplate 注入到容器中
    @LoadBalanced //在客戶端使用 RestTemplate 請(qǐng)求服務(wù)端時(shí),開啟負(fù)載均衡(Ribbon)
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

2.4、編寫調(diào)用Eureka的代碼

2.4.1、定義用戶服務(wù)接口

import com.hqyj.common.model.UserInfo;
import java.util.List;

/***
 * @title 用戶服務(wù) 接口
 * @desctption 用戶服務(wù)
 * @author kelvin
 * @create 2023/5/11 14:22
 **/
public interface UserConsumerService {

    /**
     * 獲取用戶信息列表
     * @return
     */
    public List<UserInfo> userInfoList();

}

2.4.2、編寫用戶服務(wù)實(shí)現(xiàn)類

import com.hqyj.common.model.UserInfo;
import com.hqyj.customerapi.service.UserConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/***
 * @title 用戶服務(wù) 實(shí)現(xiàn)類
 * @desctption 用戶服務(wù)
 * @author kelvin
 * @create 2023/5/11 14:22
 **/
@Service
public class UserConsumerServiceImpl implements UserConsumerService {

    private String REST_URL_PROVIDER_PREFIX = "http://USER-SERVICE";
    @Autowired
    private RestTemplate restTemplate;

    /**
     * 獲取用戶信息列表
     * @return
     */
    @Override
    public List<UserInfo> userInfoList() {
        return this.restTemplate.getForObject(this.REST_URL_PROVIDER_PREFIX + "/user/userInfoList",List.class);
    }
}

2.4.3、編寫用戶服務(wù)控制層代碼

import com.hqyj.common.model.UserInfo;
import com.hqyj.customerapi.service.UserConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/***
 * @title UserConsumerController
 * @desctption 用戶控制層
 * @author kelvin
 * @create 2023/5/11 14:22
 **/
@RestController
@RequestMapping("/user")
public class UserConsumerController {

    @Autowired
    private UserConsumerService userConsumerService;

    @GetMapping("/userInfoList")
    public List<UserInfo> userInfoList(){
        return userConsumerService.userInfoList();
    }
}

2.4.4、統(tǒng)一返回結(jié)果

在公共模塊common-api里面添加DTO

import lombok.Data;

/***
 * @title 統(tǒng)一返回格式類
 * @param <T>
 * @desctption 統(tǒng)一返回格式
 * @author kelvin
 * @create 2023/5/11 14:28
 **/
@Data
public class ResponseDTO<T> {

    /**
     * 返回編碼
     */
    private Integer code;
    /**
     * 統(tǒng)一返回消息
     */
    private String message;
    /**
     * 統(tǒng)一返回?cái)?shù)據(jù)體
     */
    private T data;

}

2.4.5、統(tǒng)一異常處理

實(shí)現(xiàn) ResponseBodyAdvice接口

import com.hqyj.common.dto.ResponseDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/***
 * @title 統(tǒng)一異常處理類
 * @desctption 統(tǒng)一異常處理
 * @author kelvin
 * @create 2023/5/11 14:33
 **/
@RestControllerAdvice(basePackages = "com.hqyj.customerapi.controller")
@Slf4j
public class ControllerResponseAdvice implements ResponseBodyAdvice<Object> {
    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        //true為織入通知
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        ResponseDTO<Object> objectResponseDTO = new ResponseDTO<>();
        objectResponseDTO.setCode(200);
        objectResponseDTO.setData(body);
        return objectResponseDTO;
    }

    /**
     * 統(tǒng)一異常處理
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public Object exception(Exception e){
        log.error("系統(tǒng)異常",e);
        ResponseDTO<Object> objectResponseDTO = new ResponseDTO<>();
        objectResponseDTO.setCode(500);
        objectResponseDTO.setMessage("系統(tǒng)異常");
        return objectResponseDTO;
    }
}

2.5、啟動(dòng)項(xiàng)目,訪問接口

2.5.1、啟動(dòng)項(xiàng)目

需要上一章節(jié)的2個(gè)項(xiàng)目先運(yùn)行
云原生微服務(wù) 第五章 Spring Cloud Netflix Eureka集成負(fù)載均衡組件Ribbon,Java微服務(wù),云原生,微服務(wù),ribbon,Eureka,負(fù)載均衡,微服務(wù)治理,原力計(jì)劃

2.5.2、訪問接口

訪問地址:http://localhost/user/userInfoList
云原生微服務(wù) 第五章 Spring Cloud Netflix Eureka集成負(fù)載均衡組件Ribbon,Java微服務(wù),云原生,微服務(wù),ribbon,Eureka,負(fù)載均衡,微服務(wù)治理,原力計(jì)劃


總結(jié)

在以前的分布式項(xiàng)目里,我們使用zookeeper、redis等來存放服務(wù)注冊(cè)信息,在客戶端調(diào)用服務(wù)時(shí),需要自己手動(dòng)獲取可用服務(wù)清單,使用起來非常麻煩,對(duì)初級(jí)開發(fā)人員特別不友好,一不小心就犯錯(cuò),比如zookeeper依賴版本沖突、zookeeper\redis集群地址填寫錯(cuò)誤、zookeeper\redis配置項(xiàng)缺失等。
Ribbon的出現(xiàn)解決了上述部分問題,而且Ribbon屬于Netflix生態(tài)里的組件,與Eureka可以很好的集成起來組合使用,非常方便。文章來源地址http://www.zghlxwxcb.cn/news/detail-713110.html

到了這里,關(guān)于云原生微服務(wù) 第五章 Spring Cloud Netflix Eureka集成負(fù)載均衡組件Ribbon的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【SpringCloudNetflix】一圖理解Spring Cloud Netflix解決了那些微服務(wù)問題?

    【SpringCloudNetflix】一圖理解Spring Cloud Netflix解決了那些微服務(wù)問題?

    注冊(cè)中心:Eureka 負(fù)載均衡:Ribbon、Feign 服務(wù)熔斷:Hystrix 服務(wù)降級(jí):Hystrix 服務(wù)監(jiān)控:Hystrix Dashboard 路由網(wǎng)關(guān):Zuul 配置中心:Config git資源倉庫:Spring Cloud Netflix 參考文章:https://zhuanlan.zhihu.com/p/353806201 坑:依賴版本對(duì)應(yīng)問題 不斷學(xué)習(xí)補(bǔ)充中,如理解有偏差的點(diǎn),煩請(qǐng)指教!

    2024年02月08日
    瀏覽(434)
  • 五,Eureka 第五章

    五,Eureka 第五章

    ? ? ? ? ? ? ? ? ? 7.3.2.注冊(cè)中心Eureka Server7001

    2024年02月15日
    瀏覽(16)
  • 【Spring Cloud 三】Eureka服務(wù)注冊(cè)與服務(wù)發(fā)現(xiàn)

    【Spring Cloud 三】Eureka服務(wù)注冊(cè)與服務(wù)發(fā)現(xiàn)

    【Spring Cloud一】微服務(wù)基本知識(shí) 目前公司項(xiàng)目使用的注冊(cè)中心主要是Spring Cloud Alibaba的Nacos做的注冊(cè)中心和配置中心。之前也是對(duì)Nacos的基本原理通過手寫代碼的方式進(jìn)行了實(shí)現(xiàn)。出于對(duì)于Eureka的好奇所以就對(duì)Spring Cloud Neflix的Eureka進(jìn)行理論學(xué)習(xí)和實(shí)踐。 Eureka是一個(gè) 注冊(cè)發(fā)現(xiàn)中

    2024年02月14日
    瀏覽(986)
  • Spring Cloud Eureka:服務(wù)注冊(cè)與發(fā)現(xiàn)

    Spring Cloud Eureka:服務(wù)注冊(cè)與發(fā)現(xiàn)

    ??wei_shuo的個(gè)人主頁 ??wei_shuo的學(xué)習(xí)社區(qū) ??Hello World ! Spring Cloud Eureka是Spring Cloud生態(tài)系統(tǒng)中的一個(gè)組件,它是用于實(shí)現(xiàn)服務(wù)注冊(cè)與發(fā)現(xiàn)的服務(wù)治理組件。在微服務(wù)架構(gòu)中,服務(wù)之間存在復(fù)雜的依賴關(guān)系,而Spring Cloud Eureka可以幫助解決服務(wù)之間相互查找和通信的問題 Eurek

    2024年02月09日
    瀏覽(88)
  • 2-Spring cloud之Eureka快速剔除失效服務(wù) 以及 Eureka原理

    2-Spring cloud之Eureka快速剔除失效服務(wù) 以及 Eureka原理

    添加如下配置: 每個(gè)服務(wù)的yml配置如下: 如下: 更多可以參考下面的文章,說的不錯(cuò) Eureka服務(wù)端掛了,為什么微服務(wù)還能調(diào)通?(原理分析).

    2024年02月13日
    瀏覽(296)
  • Eureka:Spring Cloud服務(wù)注冊(cè)與發(fā)現(xiàn)組件

    Eureka:Spring Cloud服務(wù)注冊(cè)與發(fā)現(xiàn)組件

    Eureka 一詞來源于古希臘詞匯,是“發(fā)現(xiàn)了”的意思。在軟件領(lǐng)域,Eureka 是 Netflix 公司開發(fā)的一款開源的服務(wù)注冊(cè)與發(fā)現(xiàn)組件。 Spring Cloud 將 Eureka 與 Netflix 中的其他開源服務(wù)組件(例如 Ribbon、Feign 以及 Hystrix 等)一起整合進(jìn) Spring Cloud Netflix 模塊中,整合后的組件全稱為 Spr

    2024年02月03日
    瀏覽(93)
  • 2-Spring cloud之Eureka快速剔除失效服務(wù)

    2-Spring cloud之Eureka快速剔除失效服務(wù)

    添加如下配置: 每個(gè)服務(wù)的yml配置如下: 如下: 更多可以參考下面的文章,說的不錯(cuò) Eureka服務(wù)端掛了,為什么微服務(wù)還能調(diào)通?(原理分析).

    2024年02月12日
    瀏覽(84)
  • 【spring cloud學(xué)習(xí)】2、Eureka服務(wù)注冊(cè)與發(fā)現(xiàn)

    【spring cloud學(xué)習(xí)】2、Eureka服務(wù)注冊(cè)與發(fā)現(xiàn)

    一套微服務(wù)架構(gòu)的系統(tǒng)由很多單一職責(zé)的服務(wù)單元組成,而每個(gè)服務(wù)單元又有眾多運(yùn)行實(shí)例。由于各服務(wù)單元顆粒度較小、數(shù)量眾多,相互之間呈現(xiàn)網(wǎng)狀依賴關(guān)系,因此需要服務(wù)注冊(cè)中心來統(tǒng)一管理微服務(wù)實(shí)例,維護(hù)各服務(wù)實(shí)例的健康狀態(tài)。 從宏觀角度,微服務(wù)架構(gòu)下的系統(tǒng)

    2024年02月10日
    瀏覽(230)
  • Eureka上集成Spring Cloud 微服務(wù)網(wǎng)關(guān) gateway

    Eureka上集成Spring Cloud 微服務(wù)網(wǎng)關(guān) gateway

    第一章 Java線程池技術(shù)應(yīng)用 第二章 CountDownLatch和Semaphone的應(yīng)用 第三章 Spring Cloud 簡(jiǎn)介 第四章 Spring Cloud Netflix 之 Eureka 第五章 Spring Cloud Netflix 之 Ribbon 第六章 Spring Cloud 之 OpenFeign 第七章 Spring Cloud 之 GateWay API 網(wǎng)關(guān)是一個(gè)搭建在客戶端和微服務(wù)之間的服務(wù),我們可以在 API 網(wǎng)關(guān)中

    2024年02月08日
    瀏覽(23)
  • 【Spring Cloud系列】-Eureka服務(wù)端高可用詳解

    【Spring Cloud系列】-Eureka服務(wù)端高可用詳解

    上一篇《Eureka使用詳解》一文中我們?cè)敿?xì)介紹了什么是Spring Cloud微服務(wù)。Spring Cloud中Service注冊(cè)中心及其Client如何實(shí)現(xiàn)并如何完成服務(wù)注冊(cè)的。這一篇我們將介紹Eureka注冊(cè)中心的高可用如何實(shí)現(xiàn)及其使用中的注意事項(xiàng)。 一. 序言 微服務(wù)就是開發(fā)一組小型服務(wù)的方式來完成對(duì)系

    2024年02月09日
    瀏覽(157)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包