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

微服務(wù) springcloud 08.zuul框架,API網(wǎng)關(guān),整合ribbon和hystrix框架

這篇具有很好參考價值的文章主要介紹了微服務(wù) springcloud 08.zuul框架,API網(wǎng)關(guān),整合ribbon和hystrix框架。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

01.zuul是作為springcloud中的一個網(wǎng)關(guān)框架來使用的。
zuul API 網(wǎng)關(guān),為微服務(wù)應(yīng)用提供統(tǒng)一的對外訪問接口。
zuul 還提供過濾器,對所有微服務(wù)提供統(tǒng)一的請求校驗(yàn)。
在項(xiàng)目中的位置:
微服務(wù) springcloud 08.zuul框架,API網(wǎng)關(guān),整合ribbon和hystrix框架
02.新建 sp11-zuul 項(xiàng)目
微服務(wù) springcloud 08.zuul框架,API網(wǎng)關(guān),整合ribbon和hystrix框架
微服務(wù) springcloud 08.zuul框架,API網(wǎng)關(guān),整合ribbon和hystrix框架
03.pom.xml
需要添加 sp01-commons 依賴

<?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>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>cn.tedu</groupId>
	<artifactId>sp11-zuul</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>sp11-zuul</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Hoxton.SR12</spring-cloud.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>cn.tedu</groupId>
			<artifactId>sp01-commons</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
	<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>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

04.application.yml
zuul 路由配置可以省略,缺省以服務(wù) id 作為訪問路徑


spring:
  application:
    name: zuul
    
server:
  port: 3001
  
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka

zuul:
  routes:
    item-service: /item-service/**
    user-service: /user-service/**
    order-service: /order-service/**

zuul上的routes都是從eureka中獲取的
05.主程序
添加 @EnableZuulProxy 和 @EnableDiscoveryClient 注解

package cn.tedu.sp11;

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

@EnableZuulProxy
@EnableDiscoveryClient
@SpringBootApplication
public class Sp11ZuulApplication {

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

}

zuul + ribbon 負(fù)載均衡
01.pom.xml 添加 spring-retry 依賴

<dependency>
	  <groupId>org.springframework.retry</groupId>
	  <artifactId>spring-retry</artifactId>
</dependency>

02.配置 zuul 開啟重試,并配置 ribbon 重試參數(shù)
需要開啟重試,默認(rèn)不開啟

spring:
  application:
    name: zuul
    
server:
  port: 3001
  
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka

zuul:
  retryable: true
  routes:
    item-service: /item-service/**
    user-service: /user-service/**
    order-service: /order-service/**
    
ribbon:
  ConnectTimeout: 1000
  ReadTimeout: 1000
  MaxAutoRetriesNextServer: 1
  MaxAutoRetries: 1

zuul + hystrix 降級
01.創(chuàng)建降級類
getRoute() 方法中指定應(yīng)用此降級類的服務(wù)id,星號或null值可以通配所有服務(wù)
02.ItemServiceFallback

package cn.tedu.sp11.fallback;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

import cn.tedu.web.util.JsonResult;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class ItemServiceFallback implements FallbackProvider {
	@Override
	public String getRoute() {
	    //當(dāng)執(zhí)行item-service失敗,
	    //應(yīng)用當(dāng)前這個降級類
		return "item-service";
		//星號和null都表示所有微服務(wù)失敗都應(yīng)用當(dāng)前降級類
		//"*"; //null;
	}

    //該方法返回封裝降級響應(yīng)的對象
    //ClientHttpResponse中封裝降級響應(yīng)
	@Override
	public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
        return response();
	}

	private ClientHttpResponse response() {
        return new ClientHttpResponse() {
            //下面三個方法都是協(xié)議號
            @Override
            public HttpStatus getStatusCode() throws IOException {
                return HttpStatus.OK;
            }
            @Override
            public int getRawStatusCode() throws IOException {
                return HttpStatus.OK.value();
            }
            @Override
            public String getStatusText() throws IOException {
                return HttpStatus.OK.getReasonPhrase();
            }

            @Override
            public void close() {
            }

            @Override
            public InputStream getBody() throws IOException {
            	log.info("fallback body");
            	String s = JsonResult.err().msg("后臺服務(wù)錯誤").toString();
                return new ByteArrayInputStream(s.getBytes("UTF-8"));
            }

            @Override
            public HttpHeaders getHeaders() {
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_JSON);
                return headers;
            }
        };
    }
}

03.OrderServiceFallback

package cn.tedu.sp11.fallback;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

import cn.tedu.web.util.JsonResult;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class OrderServiceFallback implements FallbackProvider {
	@Override
	public String getRoute() {
		return "order-service"; //"*"; //null;
	}

	@Override
	public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
        return response();
	}

	private ClientHttpResponse response() {
        return new ClientHttpResponse() {
            @Override
            public HttpStatus getStatusCode() throws IOException {
                return HttpStatus.OK;
            }
            @Override
            public int getRawStatusCode() throws IOException {
                return HttpStatus.OK.value();
            }
            @Override
            public String getStatusText() throws IOException {
                return HttpStatus.OK.getReasonPhrase();
            }

            @Override
            public void close() {
            }

            @Override
            public InputStream getBody() throws IOException {
            	log.info("fallback body");
            	String s = JsonResult.err().msg("后臺服務(wù)錯誤").toString();
                return new ByteArrayInputStream(s.getBytes("UTF-8"));
            }

            @Override
            public HttpHeaders getHeaders() {
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_JSON);
                return headers;
            }
        };
    }
}

04.降低 hystrix 超時時間,以便測試降級
在aplication.yml中:

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 500

05.啟動服務(wù),測試降級
訪問:http://localhost:3001/item-service/35
微服務(wù) springcloud 08.zuul框架,API網(wǎng)關(guān),整合ribbon和hystrix框架

zuul + hystrix 數(shù)據(jù)監(jiān)控
01.暴露 hystrix.stream 監(jiān)控端點(diǎn)
zuul 已經(jīng)包含 actuator 依賴

management:
  endpoints:
    web:
      exposure:
        include: hystrix.stream

啟動服務(wù),查看暴露的監(jiān)控端點(diǎn),這個項(xiàng)目的端口號是3001。
http://localhost:3001/actuator
http://localhost:3001/actuator/hystrix.stream
02.
啟動 sp08-hystrix-dashboard,填入 zuul 的監(jiān)控端點(diǎn)路徑,開啟監(jiān)控
http://localhost:4001/hystrix
填入監(jiān)控端點(diǎn):
http://localhost:3001/actuator/hystrix.stream
微服務(wù) springcloud 08.zuul框架,API網(wǎng)關(guān),整合ribbon和hystrix框架
必須通過zuul網(wǎng)關(guān)訪問后臺服務(wù)才會產(chǎn)生監(jiān)控?cái)?shù)據(jù)
http://localhost:3001/item-service/35

zuul + turbine 聚合監(jiān)控
01.修改 turbine 項(xiàng)目,聚合 zuul 服務(wù)實(shí)例

spring:
  application:
    name: turbine
    
server:
  port: 5001
  
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
#app-config是說聚合監(jiān)控的服務(wù)的名稱,這里的名稱是在spring中的name屬性聲明的。      
turbine:
  app-config: order-service,zuul
  cluster-name-expression: new String("default")

02.使用hystrix儀表盤, 對 turbine 監(jiān)控端點(diǎn)進(jìn)行監(jiān)控, 此端點(diǎn)聚合了訂單服務(wù)和zull網(wǎng)關(guān)服務(wù)的監(jiān)控?cái)?shù)據(jù)

http://localhost:5001/turbine.stream微服務(wù) springcloud 08.zuul框架,API網(wǎng)關(guān),整合ribbon和hystrix框架文章來源地址http://www.zghlxwxcb.cn/news/detail-490514.html

到了這里,關(guān)于微服務(wù) springcloud 08.zuul框架,API網(wǎng)關(guān),整合ribbon和hystrix框架的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

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

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

相關(guān)文章

  • SpringCloud之微服務(wù)API網(wǎng)關(guān)Gateway介紹

    SpringCloud之微服務(wù)API網(wǎng)關(guān)Gateway介紹

    1.1.1 簡介 如果沒有網(wǎng)關(guān),難道不行嗎?功能上是可以的,我們直接調(diào)用提供的接口就可以了。那為什么還需要網(wǎng)關(guān)? 因?yàn)榫W(wǎng)關(guān)的作用不僅僅是轉(zhuǎn)發(fā)請求而已。我們可以試想一下,如果需要做一個請求認(rèn)證功能,我們可以接入到 API 服務(wù)中。但是倘若后續(xù)又有服務(wù)需要接入,我

    2024年02月14日
    瀏覽(48)
  • 微服務(wù)springcloud 06.feign框架,配合ribbon 負(fù)載均衡和重試,配合hystrix 降級,監(jiān)控和熔斷測試

    微服務(wù)springcloud 06.feign框架,配合ribbon 負(fù)載均衡和重試,配合hystrix 降級,監(jiān)控和熔斷測試

    feign是ribbon +hystrix 的整合 01.新建 sp09-feign 項(xiàng)目 第一步: 第二步:選擇依賴: pom.xml 需要添加 sp01-commons 依賴: 第三步:修改sp09-feign項(xiàng)目的application.yml 第四步:sp09-feign的主程序添加 @EnableDiscoveryClient 和 @EnableFeignClients 02.feign 聲明式客戶端 第一步:聲明三個代理接口 這里的

    2024年02月10日
    瀏覽(22)
  • Spring Cloud 實(shí)戰(zhàn)系列之 Zuul 微服務(wù)網(wǎng)關(guān)搭建及配置

    Spring Cloud 實(shí)戰(zhàn)系列之 Zuul 微服務(wù)網(wǎng)關(guān)搭建及配置

    用mavan搭建也可以。(重要的是后面pom里應(yīng)該引入那些依賴,application.yml怎么配置) 由于開始構(gòu)建項(xiàng)目時選擇了Eureka Server,所以pom.xml中不需要手動添加依賴了 首先在啟動類SpringcloudApplication中添加EurekaServer的注解:@EnableEurekaServer 訪問注冊中心:按照配置的端口號訪問。我這

    2024年03月10日
    瀏覽(19)
  • SpringCloud - 新版淘汰 Ribbon,在 OpenFeign 中整合 LoadBalancer 負(fù)載均衡

    SpringCloud - 新版淘汰 Ribbon,在 OpenFeign 中整合 LoadBalancer 負(fù)載均衡

    目錄 一、LoadBalancer 負(fù)載均衡 1.1、前言 1.2、LoadBalancer 負(fù)載均衡底層實(shí)現(xiàn)原理 二、整合 OpenFeign + LoadBalancer 2.1、所需依賴 2.2、具體實(shí)現(xiàn) ?2.3、自定義負(fù)載均衡策略 在 2020 年以前的 SpringCloud 采用 Ribbon 作為負(fù)載均衡,但是 2020 年之后,SpringCloud 吧 Ribbon 移除了,而是使用自己編

    2024年02月03日
    瀏覽(22)
  • 微服務(wù)網(wǎng)關(guān):Spring Cloud Zuul 升級 Spring Cloud Gateway 的核心要點(diǎn)

    微服務(wù)網(wǎng)關(guān):Spring Cloud Zuul 升級 Spring Cloud Gateway 的核心要點(diǎn)

    在routes路由規(guī)則中,根據(jù)path去匹配,如果匹配中,就使用對應(yīng)的路由規(guī)則進(jìn)行請求轉(zhuǎn)發(fā) 如果無法從routes中匹配,則根據(jù)path用“/”去截取第一段作為服務(wù)名進(jìn)行請求轉(zhuǎn)發(fā),轉(zhuǎn)發(fā)時,默認(rèn)將第一段截取調(diào) 如果截取的服務(wù)名不在注冊中心中存在服務(wù),則報(bào)錯404 在routes路由規(guī)則中

    2024年02月07日
    瀏覽(27)
  • springcloud 中RestTemplate 是怎么和 ribbon整合,實(shí)現(xiàn)負(fù)載均衡的?源碼分析

    springcloud 中RestTemplate 是怎么和 ribbon整合,實(shí)現(xiàn)負(fù)載均衡的?源碼分析

    RestTemplate 內(nèi)置了一個 ClientHttpRequestInterceptor ,這個是一個攔截器操作,我們可以在請求的前后做一些事情。然后我們看一下這個類,這個類里面 有一個 intercept 方法。我們看下這個實(shí)現(xiàn)類,里面有一個 LoadBalancerInterceptor 實(shí)現(xiàn)類。 我們來看 LoadBalancerInterceptor 實(shí)現(xiàn)類。 然后我

    2024年02月09日
    瀏覽(21)
  • springcloud Ribbon負(fù)載均衡服務(wù)調(diào)用

    springcloud Ribbon負(fù)載均衡服務(wù)調(diào)用

    地址:https://github.com/13thm/study_springcloud/tree/main/days6_Ribbon Spring Cloud Ribbon是基于Netflix Ribbon實(shí)現(xiàn)的一套客戶端 負(fù)載均衡的工具。 簡單的說,Ribbon是Netflix發(fā)布的開源項(xiàng)目,主要功能是提供客戶端的軟件負(fù)載均衡算法和服務(wù)調(diào)用。Ribbon客戶端組件提供一系列完善的配置項(xiàng)如連接超時

    2024年01月20日
    瀏覽(19)
  • 【微服務(wù) SpringCloud】實(shí)用篇 · Ribbon負(fù)載均衡

    【微服務(wù) SpringCloud】實(shí)用篇 · Ribbon負(fù)載均衡

    微服務(wù)(4) 在前面,我們添加了@LoadBalanced注解,即可實(shí)現(xiàn)負(fù)載均衡功能,這是什么原理、什么策略呢? SpringCloud底層其實(shí)是利用了一個名為 Ribbon 的組件 ,來實(shí)現(xiàn)負(fù)載均衡功能的。 那么我們發(fā)出的請求明明是http://userservice/user/1,怎么變成了http://localhost:8081的呢? 為什么我

    2024年02月08日
    瀏覽(28)
  • SpringCloud學(xué)習(xí)筆記(六)_Ribbon服務(wù)調(diào)用

    SpringCloud學(xué)習(xí)筆記(六)_Ribbon服務(wù)調(diào)用

    Spring Cloud Ribbon是基于Netflix Ribbon實(shí)現(xiàn)的一套客戶端負(fù)載均衡的工具 Ribbon是Netflix發(fā)布的開源項(xiàng)目,主要功能是提供客戶端的軟件負(fù)載均衡算法和服務(wù)調(diào)用。Ribbon客戶端組件提供一系列完善的配置項(xiàng)如連接超時、重試等。簡單的說,就是在配置文件中列出Load Balance(簡稱LB)后面

    2024年02月11日
    瀏覽(20)
  • SpringCloud微服務(wù)注冊中心:Nacos介紹,微服務(wù)注冊,Ribbon通信,Ribbon負(fù)載均衡,Nacos配置管理詳細(xì)介紹

    SpringCloud微服務(wù)注冊中心:Nacos介紹,微服務(wù)注冊,Ribbon通信,Ribbon負(fù)載均衡,Nacos配置管理詳細(xì)介紹

    注冊中心可以說是微服務(wù)架構(gòu)中的”通訊錄“,它記錄了服務(wù)和服務(wù)地址的映射關(guān)系。在分布式架構(gòu)中,服務(wù)會注冊到這里,當(dāng)服務(wù)需要調(diào)用其它服務(wù)時,就這里找到服務(wù)的地址,進(jìn)行調(diào)用。 服務(wù)注冊中心(簡稱注冊中心)是微服務(wù)框架的一個重要組件,在微服務(wù)架構(gòu)里主要

    2024年02月22日
    瀏覽(97)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包