修改服务转发方式
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
FROM openjdk:17-jdk
|
||||
FROM openjdk:8-jdk
|
||||
COPY target/*.jar app.jar
|
||||
|
||||
ENTRYPOINT ["java", "-jar", \
|
||||
|
||||
31
pom.xml
31
pom.xml
@ -13,17 +13,17 @@
|
||||
<description>统一 API 网关服务</description>
|
||||
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<spring-boot.version>2.6.12</spring-boot.version>
|
||||
<spring-cloud.version>2021.0.5</spring-cloud.version>
|
||||
<spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
|
||||
<java.version>1.8</java.version>
|
||||
<spring-boot.version>2.3.12.RELEASE</spring-boot.version>
|
||||
<spring-cloud.version>Hoxton.SR12</spring-cloud.version>
|
||||
<spring-cloud-alibaba.version>2.2.9.RELEASE</spring-cloud-alibaba.version>
|
||||
</properties>
|
||||
|
||||
<!-- Parent -->
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.6.12</version>
|
||||
<version>2.3.12.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
@ -70,27 +70,6 @@
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- LoadBalancer 支持 lb:// 协议 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-commons</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.ben-manes.caffeine</groupId>
|
||||
<artifactId>caffeine</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-cache</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Actuator 监控 -->
|
||||
<dependency>
|
||||
|
||||
40
src/main/java/com/sczx/gateway/config/CustomRouteConfig.java
Normal file
40
src/main/java/com/sczx/gateway/config/CustomRouteConfig.java
Normal file
@ -0,0 +1,40 @@
|
||||
package com.sczx.gateway.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.gateway.route.RouteLocator;
|
||||
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@Configuration
|
||||
public class CustomRouteConfig {
|
||||
@Autowired
|
||||
private DiscoveryClient discoveryClient;
|
||||
|
||||
private final AtomicInteger index = new AtomicInteger(0);
|
||||
|
||||
@Bean
|
||||
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
|
||||
return builder.routes()
|
||||
.route("sczx_user", r -> r.path("/zc/user/**")
|
||||
.filters(f -> f.rewritePath("/zc/user/(?<segment>.*)", "/${segment}"))
|
||||
.uri(uriWithCustomLoadBalance("sczx_user")))
|
||||
.build();
|
||||
}
|
||||
|
||||
private URI uriWithCustomLoadBalance(String serviceName) {
|
||||
List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);
|
||||
if (instances == null || instances.isEmpty()) {
|
||||
throw new RuntimeException("No available instances for service: " + serviceName);
|
||||
}
|
||||
|
||||
ServiceInstance selected = instances.get(Math.abs(index.getAndIncrement()) % instances.size());
|
||||
return URI.create("http://" + selected.getHost() + ":" + selected.getPort());
|
||||
}
|
||||
}
|
||||
@ -1,7 +0,0 @@
|
||||
package com.sczx.gateway.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class LoadBalancerConfig {
|
||||
}
|
||||
@ -5,7 +5,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@ -17,10 +16,6 @@ public class TestController {
|
||||
@Autowired
|
||||
private DiscoveryClient discoveryClient;
|
||||
|
||||
|
||||
@Autowired
|
||||
private LoadBalancerClient loadBalancerClient;
|
||||
|
||||
@GetMapping("/test")
|
||||
public String test() {
|
||||
List<ServiceInstance> instances = discoveryClient.getInstances("sczx_user");
|
||||
@ -28,11 +23,6 @@ public class TestController {
|
||||
return "No instances found!";
|
||||
}
|
||||
|
||||
ServiceInstance instance = loadBalancerClient.choose("sczx_user");
|
||||
if (instance == null) {
|
||||
return "No instance chosen!";
|
||||
}
|
||||
log.info("Chosen instance: {}", instance.getUri());
|
||||
return "Chosen instance: " + instance.getUri();
|
||||
return "Chosen instance: " + instances.get(0).getUri();
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,19 +6,19 @@ spring:
|
||||
application:
|
||||
name: sczx_gateway
|
||||
cloud:
|
||||
gateway:
|
||||
discovery:
|
||||
locator:
|
||||
enabled: true # 开启从注册中心动态获取路由
|
||||
lower-case-service-id: true # 服务名转小写
|
||||
routes:
|
||||
- id: sczx_user
|
||||
# uri: http://172.18.0.4:8081
|
||||
uri: lb://sczx_user
|
||||
predicates:
|
||||
- Path=/zc/user/**
|
||||
filters:
|
||||
- StripPrefix=2
|
||||
# gateway:
|
||||
# discovery:
|
||||
# locator:
|
||||
# enabled: true # 开启从注册中心动态获取路由
|
||||
# lower-case-service-id: true # 服务名转小写
|
||||
# routes:
|
||||
# - id: sczx_user
|
||||
## uri: http://172.18.0.4:8081
|
||||
# uri: lb://sczx_user
|
||||
# predicates:
|
||||
# - Path=/zc/user/**
|
||||
# filters:
|
||||
# - StripPrefix=2
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: 115.190.8.52:8848 # Nacos 地址
|
||||
|
||||
Reference in New Issue
Block a user