diff --git a/README.md b/README.md index 307a4f536..70bb3dc2c 100644 --- a/README.md +++ b/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 diff --git a/README_EN.md b/README_EN.md index 1f0e232e8..9dae69ed0 100644 --- a/README_EN.md +++ b/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 + --- diff --git a/spring-boot-memcache-spymemcached/pom.xml b/spring-boot-memcache-spymemcached/pom.xml new file mode 100644 index 000000000..307afdb75 --- /dev/null +++ b/spring-boot-memcache-spymemcached/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + + com.neo + spring-boot-memcache-spymemcached + 1.0.0 + jar + + spring-boot-memcache-spymemcached + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 2.0.4.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + net.spy + spymemcached + 2.12.2 + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/spring-boot-memcache-spymemcached/src/main/java/com/neo/MemcacheApplication.java b/spring-boot-memcache-spymemcached/src/main/java/com/neo/MemcacheApplication.java new file mode 100644 index 000000000..434519a5b --- /dev/null +++ b/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); + } +} diff --git a/spring-boot-memcache-spymemcached/src/main/java/com/neo/config/MemcacheSource.java b/spring-boot-memcache-spymemcached/src/main/java/com/neo/config/MemcacheSource.java new file mode 100644 index 000000000..e3e421ac8 --- /dev/null +++ b/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; + } +} diff --git a/spring-boot-memcache-spymemcached/src/main/java/com/neo/config/MemcachedRunner.java b/spring-boot-memcache-spymemcached/src/main/java/com/neo/config/MemcachedRunner.java new file mode 100644 index 000000000..e1175b654 --- /dev/null +++ b/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; + } + +} \ No newline at end of file diff --git a/spring-boot-memcache-spymemcached/src/main/resources/application.properties b/spring-boot-memcache-spymemcached/src/main/resources/application.properties new file mode 100644 index 000000000..4d0269621 --- /dev/null +++ b/spring-boot-memcache-spymemcached/src/main/resources/application.properties @@ -0,0 +1,2 @@ +memcache.ip=192.168.0.161 +memcache.port=11211 \ No newline at end of file diff --git a/spring-boot-memcache-spymemcached/src/test/java/com/neo/MemcacheApplicationTests.java b/spring-boot-memcache-spymemcached/src/test/java/com/neo/MemcacheApplicationTests.java new file mode 100644 index 000000000..d2a66dc4d --- /dev/null +++ b/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() { + } + +} diff --git a/spring-boot-memcache-spymemcached/src/test/java/com/neo/RepositoryTests.java b/spring-boot-memcache-spymemcached/src/test/java/com/neo/RepositoryTests.java new file mode 100644 index 000000000..ceaef2205 --- /dev/null +++ b/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()); + } + +} \ No newline at end of file