在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
,本例子中在主方法处添加该注解