Redis做不了负载超垃圾,用ignite实现了可负载节点自动发现的内存数据库集群和内存集群
直接上代码:
初始化代码:
package com.xxxxxxxx.config;
import com.xxxxxxxx.hzdao;
import com.xxxxxxxx.model.Person;
import org.apache.ignite.Ignite;
import org.apache.ignite.Ignition;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author: 黄智
* @create: 2018-07-17 19:08
**/
@Configuration
public class IgniteCfg {
/**
* 初始化ignite节点信息
* @return Ignite
*/
@Bean
public Ignite igniteInstance() {
if (hzdao.ignite != null) {
return hzdao.ignite;
} else {
// 配置IgniteConfiguration
IgniteConfiguration cfg = new IgniteConfiguration();
// 设置节点名称
// cfg.setIgniteInstanceName("springDataNode");
// 启用Peer类加载器
cfg.setPeerClassLoadingEnabled(true);
// 创建一个新的Cache以供Ignite节点使用
CacheConfiguration ccfg = new CacheConfiguration("PersonCache");
// 设置SQL的Schema
ccfg.setIndexedTypes(Long.class, Person.class);
cfg.setCacheConfiguration(ccfg);
//自己追加的代码begin
TcpDiscoverySpi spi = new TcpDiscoverySpi();
TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryVmIpFinder();
ipFinder.setAddresses(Arrays.asList("0.0.0.0:47500..47501", "134.98.1.37:47500..47501"));
spi.setIpFinder(ipFinder);
cfg.setDiscoverySpi(spi);
// Ignition.setClientMode(true);
//自己追加的代码end
//return Ignition.start("D:\\apache-ignite-fabric-2.5.0-bin\\examples\\config\\example-ignite.xml");
hzdao.ignite = Ignition.start(cfg);
return hzdao.ignite;
}
}
}
实体代码
package com.xxxxxxxx.model;
import org.apache.ignite.cache.query.annotations.QuerySqlField;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
/**
* @author: 黄智
* @create: 2018-07-17 18:50
**/
public class Person {
private static final AtomicLong ID_GEN = new AtomicLong();
/** Person ID (indexed) */
@QuerySqlField(index = true)
private long id;
/** Person name(indexed) */
@QuerySqlField(index = true)
private String username;
/** Person phone(not-indexed) */
@QuerySqlField
private String password;
public Person() {
}
public Person(long id, String name, String password) {
this.id = id;
this.username = name;
this.password = password;
}
public Person(String name, String password) {
this.id = ID_GEN.incrementAndGet();
this.username = name;
this.password = password;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString(){
return "Person [id=" + id +
", username=" + username +
", password=" + password + "]";
}
}
调用范例
package com.xxxxxxxx.controller;
import com.xxxxxxxx.FrameWork;
import com.xxxxxxxx.hzdao;
import com.xxxxxxxx.model.Person;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.cache.query.QueryCursor;
import org.apache.ignite.cache.query.SqlFieldsQuery;
import org.apache.ignite.configuration.CacheConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
@Controller
@Component
public class myFirstController {
@RequestMapping("/hello")
@ResponseBody
String hello(HttpSession session)
{
return "123i2847y3sdkjfh";
}
@RequestMapping("/cacheput")
@ResponseBody
String cacheput(HttpSession session)
{
IgniteCache<Integer, String> cache = hzdao.ignite.getOrCreateCache("myCache");
cache.put(1, "Hello");
cache.put(2, "World!");
return "put ok!";
}
@RequestMapping("/cacheget")
@ResponseBody
String cacheget(HttpSession session)
{
IgniteCache<Integer, String> cache = hzdao.ignite.getOrCreateCache("myCache");
String s1 = cache.get(1);
String s2 = cache.get(2);
return "get=" + s1 + " " + s2;
}
@RequestMapping("/cacheinsert")
@ResponseBody
String cacheInsert(HttpSession session)
{
CacheConfiguration<Long, Person> cacheCfg = new CacheConfiguration<>("personCache");
cacheCfg.setIndexedTypes(Long.class, Person.class);
IgniteCache<Long, Person> cache = hzdao.ignite.getOrCreateCache(cacheCfg);
long id = hzdao.ID_GEN.incrementAndGet();
cache.query(new SqlFieldsQuery("INSERT INTO Person(_key, username, password) VALUES(?, ?, ?)")
.setArgs(id, "John", "123456"));
id = hzdao.ID_GEN.incrementAndGet();
cache.query(new SqlFieldsQuery("INSERT INTO Person(_key, username, password) VALUES(?, ?, ?)")
.setArgs(id, "Hz", "666666"));
return "insert ok!";
}
@RequestMapping("/cacheupdate")
@ResponseBody
String cacheUpdate(HttpSession session)
{
CacheConfiguration<Long, Person> cacheCfg = new CacheConfiguration<>("personCache");
cacheCfg.setIndexedTypes(Long.class, Person.class);
IgniteCache<Long, Person> cache = hzdao.ignite.getOrCreateCache(cacheCfg);
cache.query(new SqlFieldsQuery("update Person set password = ? WHERE username = ?").setArgs("666666", "John"));
cache.query(new SqlFieldsQuery("UPDATE Person set password = ? WHERE username = ?").setArgs("888888", "Hz"));
return "update ok!";
}
@RequestMapping("/cachequery")
@ResponseBody
String cacheQuery(HttpSession session)
{
CacheConfiguration<Long, Person> cacheCfg = new CacheConfiguration<>("personCache");
cacheCfg.setIndexedTypes(Long.class, Person.class);
IgniteCache<Long, Person> cache = hzdao.ignite.getOrCreateCache(cacheCfg);
SqlFieldsQuery sql = new SqlFieldsQuery("select username, password from Person");
try (QueryCursor<List<?>> cursor = cache.query(sql)) {
for (List<?> row : cursor)
System.out.println("username=" + row.get(0) + " " + "password=" + row.get(1));
}
catch (Exception e){ return "query error";}
return "query ok!";
}
@RequestMapping("/cachedelete")
@ResponseBody
String cacheDelete(HttpSession session)
{
CacheConfiguration<Long, Person> cacheCfg = new CacheConfiguration<>("personCache");
cacheCfg.setIndexedTypes(Long.class, Person.class);
IgniteCache<Long, Person> cache = hzdao.ignite.getOrCreateCache(cacheCfg);
cache.query(new SqlFieldsQuery("delete FROM Person WHERE username >= ?").setArgs("John"));
return "Delete ok!";
}
}