Spring boot2X負(fù)載均衡和反向代理實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了Spring boot2X負(fù)載均衡和反向代理實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
zuul 是netflix開(kāi)源的一個(gè)API Gateway 服務(wù)器
所有從設(shè)備或網(wǎng)站來(lái)的請(qǐng)求都會(huì)經(jīng)過(guò)Zuul到達(dá)后端的Netflix應(yīng)用程序。
作為一個(gè)邊界性質(zhì)的應(yīng)用程序,Zuul提供了動(dòng)態(tài)路由、監(jiān)控、彈性負(fù)載和安全功能。
實(shí)現(xiàn)反向代理

1.服務(wù)注冊(cè)發(fā)現(xiàn)中心Consul
啟動(dòng)
consul agent -dev
2.服務(wù)端
provider和provider1
spring boot 版本 2.2.1.RELEASE
(1)添加依賴
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</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>
(2)配置
server.port=8010
spring.application.name=service-provider
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.health-check-path=/actuator/health
spring.cloud.consul.discovery.service-name=${spring.application.name}
spring.cloud.consul.discovery.heartbeat.enabled=true
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
(3)測(cè)試方法
package com.xyz.provider.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class demoController {
@RequestMapping("/hello")
public String Hello(){
return "hello,provider";
}
}
(4)啟動(dòng)類
package com.xyz.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
provide1的
server.port=8011
測(cè)試方法
package com.xyz.provider1.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class demoController {
@RequestMapping("/hello")
public String Hello(){
return "hello,another provider";
}
}
3.網(wǎng)關(guān)
- zuul
- Spring boot版本 2.1.8.RELEASE
上面用的Spring boot版本為 2.2.1.RELEASE
但是啟動(dòng)時(shí)遇到了報(bào)錯(cuò),因此改成了這個(gè)版本
(1)添加依賴
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</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>
(2)添加配置
server.port=8090
spring.application.name=service-zuul
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.service-name=${spring.application.name}
spring.cloud.consul.discovery.instance-id=${spring.application.name}:${server.port}
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
zuul.routes.api.path=/api/**
zuul.routes.api.serviceId=service-provider
(3)啟動(dòng)類
package com.xyz.zuul;
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
@SpringBootApplication
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
啟動(dòng)Consul
啟動(dòng)provider、provider1
啟動(dòng) zuul
訪問(wèn)http://127.0.0.1:8090/api/hello
結(jié)果輸出:
hello,provider和hello,another provider
結(jié)果交替出現(xiàn)的,負(fù)載均衡器采用的是輪詢的方式
示例 https://gitee.com/babybeibeili/zuul_consul.git
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot自定義mvc組件如何實(shí)現(xiàn)
這篇文章主要介紹了Springboot自定義mvc組件如何實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
Java數(shù)組集合的深度復(fù)制代碼實(shí)例
這篇文章主要介紹了Java數(shù)組集合的深度復(fù)制代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
DoytoQuery中關(guān)于N+1查詢問(wèn)題解決方案詳解
這篇文章主要為大家介紹了DoytoQuery中關(guān)于N+1查詢問(wèn)題解決方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
SpringMVC實(shí)現(xiàn)表單驗(yàn)證功能詳解
這篇文章主要為大家詳細(xì)介紹了SpringMVC 表單驗(yàn)證的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
springboot項(xiàng)目集成swagger-bootstrap-ui全過(guò)程
這篇文章主要介紹了springboot項(xiàng)目集成swagger-bootstrap-ui全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
SpringBoot集成內(nèi)存數(shù)據(jù)庫(kù)hsqldb的實(shí)踐
hsqldb只需要添加對(duì)應(yīng)的依賴,然后在配置文件進(jìn)行配置。不需要安裝一個(gè)數(shù)據(jù)庫(kù),本文就來(lái)介紹一下具體使用,感興趣的可以了解一下2021-09-09
Java基礎(chǔ)字符編碼與內(nèi)存流詳細(xì)解讀
這篇文章主要給大家介紹了關(guān)于Java中方法使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-08-08

