• SpringBoot:使用jpa实现对Apache Geode的基本操作


    ===当前版本:jdk1.8apache-geode-1.15.0=

    1. 声明

    当前内容主要为记录SpringBoot中使用jpa方式访问当前的Apache Geode,实现基本的操作demo

    关于Geode的使用参考官方文档:添加链接描述

    2. pom依赖

    <parent>
    		<groupId>org.springframework.bootgroupId>
    		<artifactId>spring-boot-starter-parentartifactId>
    		<version>2.4.13version>
    	parent>
    	<groupId>springboot-geodegroupId>
    	<artifactId>springboot-geodeartifactId>
    	<version>0.0.1-SNAPSHOTversion>
    	<packaging>jarpackaging>
    
    	<name>springboot-geodename>
    	<url>http://maven.apache.orgurl>
    
    	<properties>
    		<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    		<spring-geode.version>1.4.13spring-geode.version>
    	properties>
    
    	<dependencyManagement>
    		<dependencies>
    			<dependency>
    				<groupId>org.springframework.geodegroupId>
    				<artifactId>spring-geode-bomartifactId>
    				<version>${spring-geode.version}version>
    				<scope>importscope>
    				<type>pomtype>
    			dependency>
    		dependencies>
    	dependencyManagement>
    
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.bootgroupId>
    			<artifactId>spring-boot-starter-webartifactId>
    		dependency>
    		<dependency>
    			<groupId>org.springframework.geodegroupId>
    			<artifactId>spring-geode-starterartifactId>
    		dependency>
    		<dependency>
    			<groupId>org.springframework.geodegroupId>
    			<artifactId>spring-geode-starter-sessionartifactId>
    		dependency>
    		<dependency>
    			<groupId>org.springframework.geodegroupId>
    			<artifactId>spring-geode-starter-testartifactId>
    			<scope>testscope>
    		dependency>
    	dependencies>
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.springframework.bootgroupId>
    				<artifactId>spring-boot-maven-pluginartifactId>
    			plugin>
    		plugins>
    	build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    3. 基本的demo

    1. 由于使用连接的是本地的地址,需要开启pdx

    configure pdx --disk-store=DEFAULT
    
    • 1

    2. 重新按照文档中启动server和一个locator,并指定特定的端口(方便整合时自动连接)

    start locator --name=locator1 --port=10334
    start server --name=server1 --server-port=40404
    
    • 1
    • 2

    3. 开始创建一个Region(People)

    create region --name=People --type=REPLICATE_PERSISTENT
    
    • 1

    4. 开始编写代码:入口类

    @SpringBootApplication
    @EnableEntityDefinedRegions(basePackageClasses = People.class, clientRegionShortcut = ClientRegionShortcut.PROXY ) // 只是当代理转发的操作
    @EnablePdx
    @EnableCachingDefinedRegions
    @EnableGemfireCaching 
    public class Application {
    	public static void main(String[] args) {
    		ConfigurableApplicationContext run = SpringApplication.run(Application.class, args);
    		ConfigurableEnvironment environment = run.getEnvironment();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    5. 实体层

    @Region(value = "People")
    public class People implements Serializable {
    	@Id
    	private  String name;
    	private  int age;
    	// 省略get\set方法
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    6. Repository层

    @Repository
    public interface PersonRepository extends CrudRepository<People, String> {}
    
    • 1
    • 2

    7. Controller层

    @RestController
    public class TestController {
    	@Autowired
    	PersonRepository personRepository;
    
    	@RequestMapping(value = "/findById", method = RequestMethod.GET)
    	public Object findById(String id) {
    		return personRepository.findById(id);
    	}
    
    	@RequestMapping(value = "/findAll", method = RequestMethod.GET)
    	public Object findAll() {
    		return personRepository.findAll();
    	}
    
    	@RequestMapping(value = "/insert", method = RequestMethod.POST)
    	public Object insert(People bean) {
    		personRepository.save(bean);
    		return "add OK";
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    4. 测试

    使用postman进行数据添加操作
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    这里可以发现实际的数据中增加了,而且可以查询出来
    在这里插入图片描述
    执行清空操作

    remove --all --region=People
    
    • 1

    在这里插入图片描述
    在这里插入图片描述
    测试成功!

    5. 总结

    1. 注意当前使用的注解和使用的模式clientRegionShortcut = ClientRegionShortcut.PROXY和LOCAL的方式不同,LOCAL不会连接到server上面并发送写入数据

    2. 如果出现The PDX metadata must be persistent in a member that has persistent data. See CacheFactory.setPdxPersistent.错误你需要开启Pdx(先配置configure pdx --disk-store=DEFAULT并重启所有服务

    3. 一定要先创建Region然后再使用jpa,否则会出现没有/XXX的错误

  • 相关阅读:
    Echarts 实现两两柱图重叠(背景和实际值柱图)
    SpringBoot项目jar发布获取jar包所在目录路径
    专本贯通 转段考试pta C语言
    python安全工具开发笔记(一)——python正则表达式
    ResNet-RS架构复现--CVPR2021
    wpf中的StaticResource和DynamicResource
    Mac 安装Nodejs
    uniapp制作--简单的tab切换
    神经网络激活函数有哪些,人工神经网络激活函数
    Uniapp中的事件处理:uni.emit和uni.on/uni.once
  • 原文地址:https://blog.csdn.net/weixin_45492007/article/details/126319832