在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,本例子中在主方法處添加該註解

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章