网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Springboot2X中怎么实现负载均衡和反向代理-创新互联

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

为金水等地区用户提供了全套网页设计制作服务,及金水网站建设行业解决方案。主营业务为成都做网站、网站建设、金水网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

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

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

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

实现反向代理

1.服务注册发现中心Consul

启动

consul agent -dev

2.服务端

provider和provider1

spring boot 版本 2.2.1.RELEASE

(1)添加依赖

   1.8    Greenwich.SR3              org.springframework.boot      spring-boot-starter-web              org.springframework.cloud      spring-cloud-starter-consul-discovery                          org.springframework.cloud        spring-cloud-dependencies        ${spring-cloud.version}        pom        import            

(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)添加依赖

   1.8    Greenwich.SR2              org.springframework.cloud      spring-cloud-starter-netflix-zuul              org.springframework.cloud      spring-cloud-starter-consul-discovery                          org.springframework.cloud        spring-cloud-dependencies        ${spring-cloud.version}        pom        import            

(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中怎么实现负载均衡和反向代理问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


网站标题:Springboot2X中怎么实现负载均衡和反向代理-创新互联
浏览地址:http://bjjierui.cn/article/pgpde.html

其他资讯