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

实现反向代理

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=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)测试方法
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)启动类
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
测试方法
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.网关
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=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)启动类
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);
}
}关于怎么在Spring boot2X中实现负载均衡和反向代理问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联网站建设公司行业资讯频道了解更多相关知识。
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
当前题目:怎么在Springboot2X中实现负载均衡和反向代理-创新互联
网页路径:http://www.jxjierui.cn/article/jjggo.html


咨询
建站咨询
