如何将Memcached与Spring MVC API连接以获取和设置数据

时间:2018-10-10 11:51:33

标签: rest spring-mvc spring-boot memcached spymemcached

我是memcached和Spring MVC的新手,我试图创建一个rest api,它将在memcached中设置一些值。我也可以检索。 但是我没有正确的方法来连接到memcached。

消费者配置:

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;   import com.btmatthews.springboot.memcached.EnableMemcached;

@EnableMemcached
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "msg91.consumers.consumer")
public class consumerConfiguration {

}

ConsumerInitializer类:

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class consumerInitializer  extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { consumerConfiguration.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

}

控制器类:

@RestController
public class consumerController {
    @Autowired
    private MemcachedClient memcachedClient;

    @RequestMapping(value="/queues/{queueName}/thread-counts/{threadCount}", method = RequestMethod.POST)
    public void setThreadCount(@PathVariable String queueName, @PathVariable int threadCount) {
        System.out.println("Queue name is "+queueName);
        System.out.println("Thread count is "+threadCount);     
        memcachedClient.set(queueName, 0, threadCount);//Setting the values in memcache which I get in api
    }

    @RequestMapping(value="/queues/{queueName}/thread-counts", method = RequestMethod.GET)
    public void getThreadCount(@PathVariable String queueName) {
        System.out.println("Queue value is"+queueName);
        int threadCount = (Integer)memcachedClient.get(queueName);
        System.out.println("Thread count for " + queueName + " is " + threadCount);
    }
}

Spring-dispatcher-servlet.xml:

<bean name="defaultMemcachedClient" class="com.google.code.ssm.CacheFactory">
  <property name="cacheClientFactory">
        <bean class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" />
  </property>
  <property name="addressProvider">
        <bean class="com.google.code.ssm.config.DefaultAddressProvider">
 <property name="address" value="127.0.0.1:11211" />
  </bean>
  </property>

它无法正常工作,并显示错误消息:

Web应用程序[/ consumer-api]中的Servlet [spring-dispatcher]引发load()异常 java.lang.ClassNotFoundException:org.springframework.web.servlet.DispatcherServlet

我认为我无法正确提供memcache的路径或无法连接到memcached。 请帮助我解决此问题。

0 个答案:

没有答案