纯洁的微笑 6 years ago
parent
commit
519e81fc55

+ 2 - 0
README.md

@@ -19,6 +19,7 @@ Spring Boot 使用的各种示例,以最简单、最实用为标准
 - [dockercompose-springboot-mysql-nginx](https://github.com/ityouknow/spring-boot-examples/tree/master/dockercompose-springboot-mysql-nginx) :Docker Compose + Spring Boot + Nginx + Mysql 示例  
 - [spring-boot-commandLineRunner](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-commandLineRunner) :Spring Boot 使用 commandLineRunner 实现项目启动时资源初始化示例  
 - [spring-boot-web-thymeleaf](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-web-thymeleaf) :Spring Boot 使用 thymeleaf 实现布局、验参、增删改查示例    
+- [spring-boot-memcache-spymemcached](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-memcache-spymemcached) :Spring Boot 使用 spymemcached 集成  memcache 示例
 
 
 **参考文章**
@@ -29,6 +30,7 @@ Spring Boot 使用的各种示例,以最简单、最实用为标准
 - [Spring Boot 2.0(四):使用 Docker 部署 Spring Boot](http://www.ityouknow.com/springboot/2018/03/19/spring-boot-docker.html)  
 - [Spring Boot 2.0(五):Docker Compose + Spring Boot + Nginx + Mysql 实践](http://www.ityouknow.com/springboot/2018/03/28/dockercompose-springboot-mysql-nginx.html)  
 - [Spring Boot 2.0(六):使用 Docker 部署 Spring Boot 开源软件云收藏](http://www.ityouknow.com/springboot/2018/04/02/docker-favorites.html) 
+- [Spring Boot 2.0(七):Spring Boot 如何解决项目启动时初始化资源](http://www.ityouknow.com/springboot/2018/05/03/spring-boot-commandLineRunner.html) 
 ---
 
 ## Spring Boot 1.0

+ 2 - 0
README_EN.md

@@ -18,6 +18,8 @@ Spring Boot examples, using the simplest and the most useful scene demos.
 - [dockercompose-springboot-mysql-nginx](https://github.com/ityouknow/spring-boot-examples/tree/master/dockercompose-springboot-mysql-nginx) :Docker Compose + Spring Boot + Nginx + Mysql example
 - [spring-boot-commandLineRunner](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-commandLineRunner) :Example of resource initialization at project startup using Spring Boot and commandLineRunner  
 - [spring-boot-web-thymeleaf](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-web-thymeleaf) :Spring Boot uses thymeleaf to implement layout, check parameters and CURD
+- [spring-boot-memcache-spymemcached](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-memcache-spymemcached) :Spring Boot uses spymemcached to  memcache
+
 
 ---
 

+ 54 - 0
spring-boot-memcache-spymemcached/pom.xml

@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+
+	<groupId>com.neo</groupId>
+	<artifactId>spring-boot-memcache-spymemcached</artifactId>
+	<version>1.0.0</version>
+	<packaging>jar</packaging>
+
+	<name>spring-boot-memcache-spymemcached</name>
+	<description>Demo project for Spring Boot</description>
+
+	<parent>
+		<groupId>org.springframework.boot</groupId>
+		<artifactId>spring-boot-starter-parent</artifactId>
+		<version>2.0.4.RELEASE</version>
+		<relativePath/> <!-- lookup parent from repository -->
+	</parent>
+
+	<properties>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+		<java.version>1.8</java.version>
+	</properties>
+
+	<dependencies>
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>net.spy</groupId>
+			<artifactId>spymemcached</artifactId>
+			<version>2.12.2</version>
+		</dependency>
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-test</artifactId>
+			<scope>test</scope>
+		</dependency>
+	</dependencies>
+
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.springframework.boot</groupId>
+				<artifactId>spring-boot-maven-plugin</artifactId>
+			</plugin>
+		</plugins>
+	</build>
+
+
+</project>

+ 12 - 0
spring-boot-memcache-spymemcached/src/main/java/com/neo/MemcacheApplication.java

@@ -0,0 +1,12 @@
+package com.neo;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class MemcacheApplication {
+
+	public static void main(String[] args) {
+		SpringApplication.run(MemcacheApplication.class, args);
+	}
+}

+ 29 - 0
spring-boot-memcache-spymemcached/src/main/java/com/neo/config/MemcacheSource.java

@@ -0,0 +1,29 @@
+package com.neo.config;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+@Component
+@ConfigurationProperties(prefix = "memcache")
+public class MemcacheSource {
+
+    private String ip;
+
+    private int port;
+
+    public String getIp() {
+        return ip;
+    }
+
+    public void setIp(String ip) {
+        this.ip = ip;
+    }
+
+    public int getPort() {
+        return port;
+    }
+
+    public void setPort(int port) {
+        this.port = port;
+    }
+}

+ 35 - 0
spring-boot-memcache-spymemcached/src/main/java/com/neo/config/MemcachedRunner.java

@@ -0,0 +1,35 @@
+package com.neo.config;
+
+import net.spy.memcached.MemcachedClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.boot.CommandLineRunner;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.io.IOException;
+import java.net.InetSocketAddress;
+
+@Component
+public class MemcachedRunner implements CommandLineRunner {
+    protected Logger logger =  LoggerFactory.getLogger(this.getClass());
+
+    @Resource
+    private  MemcacheSource memcacheSource;
+
+    private MemcachedClient client = null;
+
+    @Override
+    public void run(String... args) throws Exception {
+        try {
+            client = new MemcachedClient(new InetSocketAddress(memcacheSource.getIp(),memcacheSource.getPort()));
+        } catch (IOException e) {
+            logger.error("inint MemcachedClient failed ",e);
+        }
+    }
+
+    public MemcachedClient getClient() {
+        return client;
+    }
+
+}

+ 2 - 0
spring-boot-memcache-spymemcached/src/main/resources/application.properties

@@ -0,0 +1,2 @@
+memcache.ip=192.168.0.161
+memcache.port=11211

+ 16 - 0
spring-boot-memcache-spymemcached/src/test/java/com/neo/MemcacheApplicationTests.java

@@ -0,0 +1,16 @@
+package com.neo;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest
+public class MemcacheApplicationTests {
+
+	@Test
+	public void contextLoads() {
+	}
+
+}

+ 26 - 0
spring-boot-memcache-spymemcached/src/test/java/com/neo/RepositoryTests.java

@@ -0,0 +1,26 @@
+package com.neo;
+
+import com.neo.config.MemcachedRunner;
+import net.spy.memcached.MemcachedClient;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import javax.annotation.Resource;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest
+public class RepositoryTests {
+
+	@Resource
+    private MemcachedRunner memcachedRunner;
+
+	@Test
+	public void testSetGet()  {
+		MemcachedClient memcachedClient = memcachedRunner.getClient();
+		memcachedClient.set("testkey",1000,"666666");
+		System.out.println("***********  "+memcachedClient.get("testkey").toString());
+	}
+
+}