मैं SCOPE_REQUEST स्कोप के साथ स्प्रिंग बूट एप्लिकेशन में CacheManager का उपयोग कर रहा हूं।
@Bean
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager();
}
मैं माइक्रोसर्विसेज के बीच संचार के लिए काफ्का का भी उपयोग कर रहा हूं। असल में मुझे काफ्का उपभोक्ता के माध्यम से एक घटना प्राप्त हो रही है और मुझे निम्न त्रुटि मिलती है:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.cacheManager': Scope 'request' is not active for the current thread;
...
Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread?
यह स्पष्ट है कि श्रोता थ्रेड पर CacheManager बीन अनुपलब्ध है। मेरा लक्ष्य है कि स्प्रिंग बूट/काफ्का ढांचे को प्रत्येक उपभोग किए गए काफ्का घटनाओं के लिए औसत बनाने के लिए वेब अनुरोधों के समान ही बनाने दें। मुझे नहीं पता कि मैं इसे कैसे प्राप्त कर सकता हूं, क्या कोई मेरी मदद कर सकता है?
बहुत बहुत धन्यवाद, आपका दिन शुभ हो!
2 जवाब
@ गैरी रसेल एक ही समय में सही और गलत है, इस बीच मैं एक समाधान खोजने में सफल रहा, निम्न वर्ग बनाएं:
public class KafkaRequestScopeAttributes implements RequestAttributes {
private Map<String, Object> requestAttributeMap = new HashMap<>();
@Override
public Object getAttribute(String name, int scope) {
if (scope == RequestAttributes.SCOPE_REQUEST) {
return this.requestAttributeMap.get(name);
}
return null;
}
@Override
public void setAttribute(String name, Object value, int scope) {
if (scope == RequestAttributes.SCOPE_REQUEST) {
this.requestAttributeMap.put(name, value);
}
}
@Override
public void removeAttribute(String name, int scope) {
if (scope == RequestAttributes.SCOPE_REQUEST) {
this.requestAttributeMap.remove(name);
}
}
@Override
public String[] getAttributeNames(int scope) {
if (scope == RequestAttributes.SCOPE_REQUEST) {
return this.requestAttributeMap.keySet().toArray(new String[0]);
}
return new String[0];
}
@Override
public void registerDestructionCallback(String name, Runnable callback, int scope) {
// Not Supported
}
@Override
public Object resolveReference(String key) {
// Not supported
return null;
}
@Override
public String getSessionId() {
return null;
}
@Override
public Object getSessionMutex() {
return null;
}
}
फिर अपने काफ्का लिस्टनर विधि के प्रारंभ और अंत में निम्नलिखित दो पंक्तियाँ जोड़ें:
RequestContextHolder.setRequestAttributes(new KafkaRequestScopeAttributes());
RequestContextHolder.resetRequestAttributes();
ऐसा करने से आप काफ्का श्रोता में REQUEST_SCOPE बनाने के लिए बाध्य कर सकते हैं।
अनुरोध का दायरा केवल वेब अनुप्रयोगों के लिए है; इसका उपयोग काफ्का उपभोक्ताओं के साथ नहीं किया जा सकता है।
RecordInterceptor
जोड़ सकते हैं; अगली रिलीज़ (2.5.6) में एक सलाह श्रृंखला है।
संबंधित सवाल
नए सवाल
spring-boot
स्प्रिंग बूट एक ऐसा ढांचा है, जो पूर्ण न्यूनतम उपद्रव के साथ आसानी से वसंत-संचालित, उत्पादन-ग्रेड अनुप्रयोगों और सेवाओं को बनाने की अनुमति देता है। यह स्प्रिंग प्लेटफ़ॉर्म के नए और अनुभवी उपयोगकर्ताओं के लिए काम करने के लिए डिज़ाइन किए गए स्प्रिंग प्लेटफ़ॉर्म के बारे में एक राय लेता है।