huangzhiguo 1 jaar geleden
bovenliggende
commit
90c12eae1f

+ 17 - 3
caimei365-cloud-gateway/src/main/java/com/caimei365/cloud/config/WebConfiguration.java

@@ -2,6 +2,8 @@ package com.caimei365.cloud.config;
 
 import com.caimei365.cloud.service.TouristService;
 import org.apache.commons.lang.StringUtils;
+import org.apache.rocketmq.spring.core.RocketMQTemplate;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.http.server.reactive.ServerHttpRequest;
 import org.springframework.stereotype.Component;
@@ -11,6 +13,8 @@ import org.springframework.web.server.WebFilter;
 import org.springframework.web.server.WebFilterChain;
 import reactor.core.publisher.Mono;
 
+import javax.annotation.PostConstruct;
+import javax.annotation.Resource;
 import java.net.InetSocketAddress;
 import java.util.Objects;
 
@@ -23,6 +27,16 @@ import java.util.Objects;
 @Configuration
 public class WebConfiguration implements WebFluxConfigurer {
 
+     @Autowired
+     private TouristService touristService;
+
+     private static WebConfiguration webConfiguration;
+
+     @PostConstruct
+     public void init() {
+         webConfiguration = this;
+         webConfiguration.touristService = this.touristService;
+     }
     /**
      * 获取客户端IP
      * https://stackoverflow.com/questions/51192630/how-do-you-get-clients-ip-address-spring-webflux-websocket?rq=1
@@ -46,10 +60,10 @@ public class WebConfiguration implements WebFluxConfigurer {
             ServerWebExchange mutatedServerWebExchange = exchange.mutate().request(mutatedServerHttpRequest).build();
             chain.filter(mutatedServerWebExchange);
             // 游客编号
-            TouristService touristService = new TouristService();
-            String touristId = touristService.touristInfo();
+            //TouristService touristService = new TouristService();
+            String touristId = webConfiguration.touristService.touristInfo();
             return exchange.getSession().flatMap(session -> {
-                session.getAttributes().put("touristId","touristId");
+                session.getAttributes().put("touristId", touristId);
                 return chain.filter(exchange);
             });
         }

+ 5 - 1
caimei365-cloud-gateway/src/main/java/com/caimei365/cloud/service/TouristService.java

@@ -1,6 +1,9 @@
 package com.caimei365.cloud.service;
 
 import com.alibaba.fastjson.JSON;
+import lombok.AllArgsConstructor;
+import lombok.NoArgsConstructor;
+import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang.StringUtils;
 import org.apache.rocketmq.client.producer.SendCallback;
@@ -21,12 +24,13 @@ import java.util.Random;
  */
 @Slf4j
 @Component
+@RequiredArgsConstructor
 public class TouristService {
 
     @Value("${rocketmq.producer.send-message-timeout}")
     private Long timeout;
 
-    private RocketMQTemplate rocketMQTemplate;
+    private final RocketMQTemplate rocketMQTemplate;
 
 
     public String touristInfo() {