在Spring Boot上部署ignite数据库的小例子

在Spring Boot上部署ignite数据库的小例子

这是将 ignite 数据库部署到SpringBoot上的超简单REST服务(Github源码),实现了用户通过浏览器往数据库增加数据和查找数据的功能,api 接口为:
新增一个Person:http://localhost:8080/person?name=XXX&phone=XXX
查找一个Person:http://localhost:8080/persons?name=XXX
ignite数据库生成一个节点通过Spring Boot进行管理
使用Postman对API进行测试


项目构造过程

1.首先搭建一个Spring Boot的web项目

2.添加Ignite的依赖ignite-spring-data,根据自己安装的ignite实际版本来替换{$ignite.version}

 <dependency>
    <groupId>org.apache.ignite</groupId>
    <artifactId>ignite-spring-data</artifactId>
    <version>{$ignite.version}</version>
</dependency>

3.构造用于数据库操作的实体类Person


Person.java


 public class Person {

    private static final AtomicLong ID_GEN = new AtomicLong();

    /** Person ID (indexed) */
    @QuerySqlField(index = true)
    public long id;

    /** Person name(not-indexed) */
    @QuerySqlField
    public String name;

    /** Person phone(not-indexed) */
    @QuerySqlField
    public String phone;

    /**
     * Constructs Person record
     * @param id Person ID
     * @param name Person name
     * @param phone Person phone
     */
    public Person(long id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

    /**
     * Constructs Person record
     * @param name Person name.
     * @param phone Person phone.
     */
    public Person(String name, String phone) {
        // Generate unique ID for this person.
        this.id = ID_GEN.incrementAndGet();

        this.name = name;
        this.phone = phone;
    }

    /**
     * Get method
     * @return Person ID.
     */
    public long getId() {
        return id;
    }

    @Override
    public String toString(){
        return "Person [id=" + id +
                ", name=" + name +
                ", phone=" + phone + "]";
    }
}

4.构造 ignite 的启动配置@Configuration——IgniteCfg类来实现对 ignite 的初始化


IgniteCfg.java

 @Configuration
public class IgniteCfg {
    /**
     * Creating Apache Ignite Instance bean.
     * @return Ignite
     */
    @Bean
    public Ignite igniteInstance(){
        IgniteConfiguration cfg = new IgniteConfiguration();

        // Setting some custom name for the node.
        cfg.setIgniteInstanceName("springDataNode");

        // Enabling peer-class loading feature.
        cfg.setPeerClassLoadingEnabled(true);

        // Defining and creating a new cache to be used by Ignite Spring Data repository.
        CacheConfiguration ccfg = new CacheConfiguration("PersonCache");

        // Setting SQL schema for the cache.
        ccfg.setIndexedTypes(Long.class, Person.class);

        cfg.setCacheConfiguration(ccfg);

        return Ignition.start(cfg);
    }
}

5.构造ignite的数据库访问接口@RepositoryConfig(cacheName = “PersonCache”)——PersonRepository,表示该接口针对PersonCache这个Cache进行数据操作,构造接口继承自IgniteRepository,使之获取Ignite的数据操作功能,并可以根据需要构造不同功能的数据访问方法


PersonRepository.java

@RepositoryConfig(cacheName = "PersonCache")
public interface PersonRepository extends IgniteRepository<Person, Long>{

    /**
     * Find a person with given name in Ignite DB.
     * @param name Person name.
     * @return The person whose name is the given name.
     */
    Person findByName(String name);

}

6.构造服务接口@Service——PersonService及其实现类PersonServiceImpl,通过依赖注入的方式获取到PersonRepository的对象,通过调用PersonRepository的方法处理包装好的前端发送过来的请求的对象,用于承接Controller模块和数据库访问接口


PersonService.java

 public interface PersonService {
    /**
     *
     * @param person Person Object
     * @return The Person object saved in Ignite DB.
     */
    Person save(Person person);

    /**
     * Find a Person from Ignite DB with given name.
     * @param name Person name.
     * @return The person found in Ignite DB
     */
    Person findPersonByName(String name);
}

PersonServiceImpl.java

@Service
public class PersonServiceImpl implements PersonService{

    @Autowired
    private PersonRepository personRepository;

    /**
     * Save Person to Ignite DB
     * @param person Person object.
     * @return The Person object saved in Ignite DB.
     */
    public Person save(Person person) {
        return personRepository.save(person.getId(), person);
    }

    /**
     * Find a Person from Ignite DB with given name.
     * @param name Person name.
     * @return The person found in Ignite DB
     */
    public Person findPersonByName(String name){
        return personRepository.findByName(name);
    }

}

7.构造处理前端请求的@RestController——PersonController,通过依赖注入的方式获取到PersonServiceImpl的对象,用来实现前端的地址访问的映射,并将前端发送过来的请求包装成对象,让@Service去进行处理


PersonController.java

@RestController
public class PersonController {

    @Autowired
    private PersonService personService;

    /**
     * Save a person with name and phone sent by front end.
     * @param name Person name.
     * @param phone Person phone.
     * @return The person saved in ignite DB
     */
    @RequestMapping("/person")
    public Person savePerson(@RequestParam(value = "name") String name, @RequestParam(value = "phone") String phone){
        return personService.save(new Person(name, phone));
    }

    /**
     * Find a person with given name sent by front end.
     * @param name Person name.
     * @return The person found in ignite DB
     */
    @RequestMapping("/persons")
    public Person savePerson(@RequestParam(value = "name") String name){
        return personService.findPersonByName(name);
    }

}

运行结果

这里写图片描述

注意事项

SpringBoot 能够识别并自动转换成Bean的注解只有4个即@Controlle@Service@Repository@Component,ignite使用了新的Bean注解即@RepositoryConfig,为了能够让SpringBoot应用识别该注解并管理Bean,需要在使用到PersonRepository的地方加上注解@EnableIgniteRepositories,本例子中在主方法处添加该注解

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值