修改服务转发方式
This commit is contained in:
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