From 82d9786298d9ff8018c80ba86233175b9dffae41 Mon Sep 17 00:00:00 2001 From: ityouknow Date: Wed, 28 Dec 2022 17:33:43 +0800 Subject: [PATCH] =?UTF-8?q?Spring=20Boot=203.0=20=20=E9=9B=86=E6=88=90=20M?= =?UTF-8?q?emcached?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +- spring-boot-docker/pom.xml | 78 +++++++++++++++++++ spring-boot-docker/src/main/docker/Dockerfile | 4 + .../main/java/com/neo/DockerApplication.java | 12 +++ .../com/neo/controller/DockerController.java | 13 ++++ .../src/main/resources/application.properties | 0 .../java/com/neo/DockerApplicationTests.java | 17 ++++ spring-boot-memcache-spymemcached/pom.xml | 63 +++++++++++++++ .../java/com/neo/MemcacheApplication.java | 12 +++ .../java/com/neo/config/MemcacheSource.java | 29 +++++++ .../java/com/neo/config/MemcachedRunner.java | 35 +++++++++ .../src/main/resources/application.properties | 2 + .../com/neo/MemcacheApplicationTests.java | 16 ++++ .../test/java/com/neo/RepositoryTests.java | 26 +++++++ 14 files changed, 310 insertions(+), 2 deletions(-) create mode 100644 spring-boot-docker/pom.xml create mode 100644 spring-boot-docker/src/main/docker/Dockerfile create mode 100644 spring-boot-docker/src/main/java/com/neo/DockerApplication.java create mode 100644 spring-boot-docker/src/main/java/com/neo/controller/DockerController.java create mode 100644 spring-boot-docker/src/main/resources/application.properties create mode 100644 spring-boot-docker/src/test/java/com/neo/DockerApplicationTests.java create mode 100644 spring-boot-memcache-spymemcached/pom.xml create mode 100644 spring-boot-memcache-spymemcached/src/main/java/com/neo/MemcacheApplication.java create mode 100644 spring-boot-memcache-spymemcached/src/main/java/com/neo/config/MemcacheSource.java create mode 100644 spring-boot-memcache-spymemcached/src/main/java/com/neo/config/MemcachedRunner.java create mode 100644 spring-boot-memcache-spymemcached/src/main/resources/application.properties create mode 100644 spring-boot-memcache-spymemcached/src/test/java/com/neo/MemcacheApplicationTests.java create mode 100644 spring-boot-memcache-spymemcached/src/test/java/com/neo/RepositoryTests.java diff --git a/README.md b/README.md index 1c79dc4..3db1a9a 100644 --- a/README.md +++ b/README.md @@ -41,8 +41,9 @@ Spring Boot 使用的各种示例,以最简单、最实用为标准,此开 - [spring-boot-web-thymeleaf](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-web-thymeleaf):Spring Boot 3.0 thymeleaf 增删该查示例 - [spring-boot-jpa-thymeleaf-curd](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-jpa-thymeleaf-curd):Spring Boot 3.0 Jpa thymeleaf 列表、增删改查使用案例 - [spring-boot-file-upload](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-file-upload):Spring Boot 3.0 上传文件使用案例 -- [spring-boot-commandLineRunner](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-commandLineRunner):Spring Boot 3.0 上传文件使用案例 - +- [spring-boot-commandLineRunner](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-commandLineRunner):Spring Boot 3.0 目启动时初始化资源案例 +- [spring-boot-docker](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-docker):Spring Boot 3.0 Docker 使用案例 +- [spring-boot-memcache-spymemcached](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-memcache-spymemcached):Spring Boot 3.0 集成 Memcached 使用案例 > 如果大家想了解关于 Spring Boot 的其它方面应用,也可以以[issues](https://github.com/ityouknow/spring-boot-examples/issues)的形式反馈给我,我后续来完善。 diff --git a/spring-boot-docker/pom.xml b/spring-boot-docker/pom.xml new file mode 100644 index 0000000..0854143 --- /dev/null +++ b/spring-boot-docker/pom.xml @@ -0,0 +1,78 @@ + + + 4.0.0 + + com.neo + spring-boot-docker + 1.0 + jar + + spring-boot-docker + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 3.0.0 + + + + UTF-8 + UTF-8 + 17 + springboot + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + + + org.junit.vintage + junit-vintage-engine + test + + + org.hamcrest + hamcrest-core + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + com.spotify + docker-maven-plugin + 1.0.0 + + ${docker.image.prefix}/${project.artifactId} + src/main/docker + + + / + ${project.build.directory} + ${project.build.finalName}.jar + + + + + + + + + + diff --git a/spring-boot-docker/src/main/docker/Dockerfile b/spring-boot-docker/src/main/docker/Dockerfile new file mode 100644 index 0000000..153350c --- /dev/null +++ b/spring-boot-docker/src/main/docker/Dockerfile @@ -0,0 +1,4 @@ +FROM openjdk:17-jdk-alpine +VOLUME /tmp +ADD spring-boot-docker-1.0.jar app.jar +ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] \ No newline at end of file diff --git a/spring-boot-docker/src/main/java/com/neo/DockerApplication.java b/spring-boot-docker/src/main/java/com/neo/DockerApplication.java new file mode 100644 index 0000000..d3a0276 --- /dev/null +++ b/spring-boot-docker/src/main/java/com/neo/DockerApplication.java @@ -0,0 +1,12 @@ +package com.neo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DockerApplication { + + public static void main(String[] args) { + SpringApplication.run(DockerApplication.class, args); + } +} diff --git a/spring-boot-docker/src/main/java/com/neo/controller/DockerController.java b/spring-boot-docker/src/main/java/com/neo/controller/DockerController.java new file mode 100644 index 0000000..8b6ba67 --- /dev/null +++ b/spring-boot-docker/src/main/java/com/neo/controller/DockerController.java @@ -0,0 +1,13 @@ +package com.neo.controller; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class DockerController { + + @RequestMapping("/") + public String index() { + return "Hello Docker!"; + } +} \ No newline at end of file diff --git a/spring-boot-docker/src/main/resources/application.properties b/spring-boot-docker/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29 diff --git a/spring-boot-docker/src/test/java/com/neo/DockerApplicationTests.java b/spring-boot-docker/src/test/java/com/neo/DockerApplicationTests.java new file mode 100644 index 0000000..341a103 --- /dev/null +++ b/spring-boot-docker/src/test/java/com/neo/DockerApplicationTests.java @@ -0,0 +1,17 @@ +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 DockerApplicationTests { + + @Test + public void contextLoads() { + System.out.println("hello docker"); + } + +} diff --git a/spring-boot-memcache-spymemcached/pom.xml b/spring-boot-memcache-spymemcached/pom.xml new file mode 100644 index 0000000..ae6f0c5 --- /dev/null +++ b/spring-boot-memcache-spymemcached/pom.xml @@ -0,0 +1,63 @@ + + + 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 + 3.0.0 + + + + + UTF-8 + 17 + + + + + org.springframework.boot + spring-boot-starter + + + net.spy + spymemcached + 2.12.2 + + + org.springframework.boot + spring-boot-starter-test + + + org.junit.vintage + junit-vintage-engine + test + + + org.hamcrest + hamcrest-core + + + + + + + + + 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 0000000..434519a --- /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 0000000..e3e421a --- /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 0000000..07b0b88 --- /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 jakarta.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 0000000..3102bda --- /dev/null +++ b/spring-boot-memcache-spymemcached/src/main/resources/application.properties @@ -0,0 +1,2 @@ +memcache.ip=localhost +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 0000000..d2a66dc --- /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 0000000..cdeea20 --- /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 jakarta.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