温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Springboot2X中怎么实现负载均衡和反向代理

发布时间:2021-08-06 16:31:53 来源:亿速云 阅读:92 作者:Leah 栏目:编程语言

Springboot2X中怎么实现负载均衡和反向代理,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

zuul 是netflix开源的一个API Gateway 服务器

所有从设备或网站来的请求都会经过Zuul到达后端的Netflix应用程序。

作为一个边界性质的应用程序,Zuul提供了动态路由、监控、弹性负载和安全功能。

实现反向代理

1.服务注册发现中心Consul

启动

consul agent -dev

2.服务端

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=8010spring.application.name=service-providerspring.cloud.consul.host=localhostspring.cloud.consul.port=8500spring.cloud.consul.discovery.health-check-path=/actuator/healthspring.cloud.consul.discovery.service-name=${spring.application.name}spring.cloud.consul.discovery.heartbeat.enabled=truemanagement.endpoints.web.exposure.include=*management.endpoint.health.show-details=always

(3)测试方法

package com.xyz.provider.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class demoController {  @RequestMapping("/hello")  public String Hello(){    return "hello,provider";  }}

(4)启动类

package com.xyz.provider;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@EnableDiscoveryClient@SpringBootApplicationpublic class ProviderApplication {  public static void main(String[] args) {    SpringApplication.run(ProviderApplication.class, args);  }}

provide1的

server.port=8011

测试方法

package com.xyz.provider1.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class demoController {  @RequestMapping("/hello")  public String Hello(){    return "hello,another provider";  }}

3.网关

zuul  Spring boot版本 2.1.8.RELEASE

上面用的Spring boot版本为 2.2.1.RELEASE

但是启动时遇到了报错,因此改成了这个版本

(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=8090spring.application.name=service-zuulspring.cloud.consul.host=localhostspring.cloud.consul.port=8500spring.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=alwayszuul.routes.api.path=/api/**zuul.routes.api.serviceId=service-provider

(3)启动类

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@SpringBootApplicationpublic class ZuulApplication {  public static void main(String[] args) {    SpringApplication.run(ZuulApplication.class, args);  }}

启动Consul

启动provider、provider1

启动 zuul

访问http://127.0.0.1:8090/api/hello

结果输出:

  hello,provider和hello,another provider

关于Springboot2X中怎么实现负载均衡和反向代理问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI