Oracle Coherence:分布式数据管理
二手技術:
JDK 1.6.0_21
Maven的3.0.2
連貫性3.7.0 SolarisOS 5.10
步驟1:建立已完成的專案
創建一個Maven項目,如下所示。 (可以使用Maven或IDE插件來創建它)。
步驟2:下載相干套餐
可通過http://www.oracle.com/technetwork/middleware/coherence/downloads/index.html下載Coherence軟件包
步驟3:圖書館
首先,將Coherence庫安裝到Local Maven Repository,并將其描述添加到pom.xml中,如下所示。 另外,如果不使用maven,則可以將coherence.jar文件添加到classpath中。
<!-- Coherence library(from local repository) --><dependency><groupId>com.tangosol</groupId><artifactId>coherence</artifactId><version>3.7.0</version></dependency>下面的插件可用于創建runnable-jar 。
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><configuration><descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs><archive><manifest><mainClass>com.otv.exe.TestCacheExecutor</mainClass></manifest></archive></configuration><executions><execution><phase>package</phase><goals><goal>single</goal></goals></execution></executions></plugin>步驟4:建立otv-coherence-cache-config.xml
otv-coherence-cache-config.xml包含(分布式或復制的)緩存方案和緩存方案映射配置。 創建后,所有緩存映射都應添加到coherence-cache-config.xml中 。
<?xml version="1.0"?><cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config"xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-configcoherence-cache-config.xsd"><caching-scheme-mapping><cache-mapping><cache-name>user-map</cache-name><scheme-name>MapDistCache</scheme-name></cache-mapping></caching-scheme-mapping><caching-schemes><distributed-scheme><scheme-name>MapDistCache</scheme-name><service-name>MapDistCache</service-name><backing-map-scheme><local-scheme><unit-calculator>BINARY</unit-calculator></local-scheme></backing-map-scheme><autostart>true</autostart></distributed-scheme></caching-schemes> </cache-config>步驟5:創建tangosol-coherence-override.xml
tangosol-coherence-override.xml包含集群, 成員身份和可配置的緩存工廠配置。 同樣在配置xml文件下面顯示了集群的第一個成員。
集群的第一個成員的tangosol-coherence-override.xml:
<?xml version='1.0'?><coherence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.oracle.com/coherence/coherence-operational-config"xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-operational-config coherence-operational-config.xsd"><cluster-config><member-identity><cluster-name>OTV</cluster-name><!-- Name of the first member of the cluster --><role-name>OTV1</role-name></member-identity><unicast-listener><well-known-addresses><socket-address id="1"><!-- IP Address of the first member of the cluster --><address>x.x.x.x</address><port>8089</port></socket-address><socket-address id="2"><!-- IP Address of the second member of the cluster --><address>y.y.y.y</address><port>8089</port></socket-address></well-known-addresses><!-- Name of the first member of the cluster --><machine-id>OTV1</machine-id><!-- IP Address of the first member of the cluster --><address>x.x.x.x</address><port>8089</port><port-auto-adjust>true</port-auto-adjust></unicast-listener></cluster-config><configurable-cache-factory-config><init-params><init-param><param-type>java.lang.String</param-type><param-value system-property="tangosol.coherence.cacheconfig">otv-coherence-cache-config.xml</param-value></init-param></init-params></configurable-cache-factory-config> </coherence>集群的第二個成員的tangosol-coherence-override.xml:
<?xml version='1.0'?><coherence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.oracle.com/coherence/coherence-operational-config"xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-operational-config coherence-operational-config.xsd"><cluster-config><member-identity><cluster-name>OTV</cluster-name><!-- Name of the second member of the cluster --><role-name>OTV2</role-name></member-identity><unicast-listener> <well-known-addresses><socket-address id="1"><!-- IP Address of the first member of the cluster --><address>x.x.x.x</address><port>8089</port></socket-address><socket-address id="2"><!-- IP Address of the second member of the cluster --><address>y.y.y.y</address><port>8089</port></socket-address></well-known-addresses><!-- Name of the second member of the cluster --><machine-id>OTV2</machine-id><!-- IP Address of the second member of the cluster --><address>y.y.y.y</address><port>8089</port><port-auto-adjust>true</port-auto-adjust></unicast-listener></cluster-config><configurable-cache-factory-config><init-params><init-param><param-type>java.lang.String</param-type><param-value system-property="tangosol.coherence.cacheconfig">otv-coherence-cache-config.xml</param-value></init-param></init-params></configurable-cache-factory-config></coherence>第6步:創建用戶頭像
創建一個新的User bean。 該bean將分布在OTV集群中的兩個節點之間。 對于序列化,已經實現了java.io.Serializable接口,但是可以實現PortableObject以獲得更好的性能。
package com.otv.user;import java.io.Serializable;/*** @author onlinetechvision.com* @since 9 Oct 2011* @version 1.0.0**/ public class User implements Serializable {private static final long serialVersionUID = 1L;private String name;private String surname;public User(String name, String surname) {this.name = name;this.surname = surname;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSurname() {return surname;}public void setSurname(String surname) {this.surname = surname;}@Overridepublic String toString() {StringBuffer strBuff = new StringBuffer();strBuff.append("name : ").append(name);strBuff.append(", surname : ").append(surname);return strBuff.toString();} }第7步:創建緩存類
創建一個新的TestCache類。 此類初始化分布式(已分類)數據管理,并創建一個名為user-map的緩存對象。
package com.otv;import org.apache.log4j.Logger;import com.otv.listener.UserMapListener; import com.tangosol.net.CacheFactory; import com.tangosol.net.NamedCache;/*** @author onlinetechvision.com* @since 9 Oct 2011* @version 1.0.0**/ public class TestCache {private static Logger log = Logger.getLogger(TestCache.class);private static TestCache instance = null;private NamedCache cache = null;private static final String USER_MAP = "user-map";private static final long LOCK_TIMEOUT = -1;public TestCache() {setCache(CacheFactory.getCache(USER_MAP));getCache().addMapListener(new UserMapListener());}public static TestCache getInstance() {if(instance == null) {instance = new TestCache();}return instance;}public static void setInstance(TestCache instance) {TestCache.instance = instance;}public NamedCache getCache() {return cache;}public void setCache(NamedCache cache) {this.cache = cache;}public void addToCache(Object key, Object value) {// key is lockedgetCache().lock(key, LOCK_TIMEOUT);try {// application logicgetCache().put(key, value);} finally {// key is unlockedgetCache().unlock(key);}}public void deleteFromCache(Object key) {// key is lockedgetCache().lock(key, LOCK_TIMEOUT);try {// application logicgetCache().remove(key);} finally {// key is unlockedgetCache().unlock(key);}} }步驟8:建立UserMapListener IMPL類別
創建一個新的UserMapListener類。 該偵聽器接收分布式用戶映射事件。
package com.otv.listener;import org.apache.log4j.Logger;import com.tangosol.util.MapEvent; import com.tangosol.util.MapListener;/*** @author onlinetechvision.com* @since 9 Oct 2011* @version 1.0.0**/ public class UserMapListener implements MapListener {private static Logger logger = Logger.getLogger(UserMapListener.class);public void entryDeleted(MapEvent me) {logger.debug("Deleted Key = " + me.getKey() + ", Value = " + me.getOldValue());}public void entryInserted(MapEvent me) {logger.debug("Inserted Key = " + me.getKey() + ", Value = " + me.getNewValue());}public void entryUpdated(MapEvent me) { // logger.debug("Updated Key = " + me.getKey() + ", New_Value = " + // me.getNewValue() + ", Old Value = " + me.getOldValue());} }步驟9:建立TestCacheExecutor類別
創建TestCacheExecutor類以運行該應用程序。
package com.otv.exe;import java.util.Iterator;import org.apache.log4j.Logger;import com.otv.TestCache; import com.otv.user.User;/*** @author onlinetechvision.com* @since 9 Oct 2011* @version 1.0.0**/ public class TestCacheExecutor implements Runnable {private static Logger log = Logger.getLogger(TestCacheExecutor.class);public static void main(String[] args) {try {TestCacheExecutor testCacheExecutor = new TestCacheExecutor();while (true) {testCacheExecutor.run();Thread.sleep(10000);}} catch (InterruptedException e) {e.printStackTrace();}}public void run() {execute();}public void execute() {//Entries which will be inserted via first member of the cluster so before the project is built// in order to deploy first member of the cluster, this code block should be opened and below //code block should be commented-out...User firstUser = new User("Bruce", "Willis");User secondUser = new User("Clint", "Eastwood");TestCache.getInstance().addToCache("user1", firstUser);TestCache.getInstance().addToCache("user2", secondUser); //Entries which will be inserted via second member of the cluster so before the project is //built in order to deploy second member of the cluster, this code block should be opened //and above code block should be commented-out...//User firstUser = new User("Anna", "Kornikova");//User secondUser = new User("Natalie", "Portman");//TestCache.getInstance().addToCache("user3", firstUser);//TestCache.getInstance().addToCache("user4", secondUser); Iterator it = TestCache.getInstance().getCache().values().iterator();log.debug("***************************************");while(it.hasNext()){User user = (User)it.next();log.debug("1. Cache Content : "+user);}log.debug("***************************************");}}步驟10:建立專案
生成OTV_Coherence項目時,將創建OTV_Coherence-0.0.1-SNAPSHOT-jar-with-dependencies.jar 。
注意:構建過程應分別應用于集群的每個成員。
步驟11:在集群的第一個成員上運行項目
在群集成員上運行OTV_Coherence-0.0.1-SNAPSHOT-jar-with-dependencies.jar文件后,將在第一個成員的控制臺上顯示以下輸出日志:
xxxx:第一個成員的IP地址
yyyy:第二個成員的IP地址
第二成員的控制臺:
root@wpbxwebt # java -jar OTV_Coherence-0.0.1-SNAPSHOT-jar-with-dependencies.jar 2011-10-09 21:25:37.623/3.056 Oracle Coherence n/a <Info> (thread=main, member=n/a): Loaded operational configuration from "jar:file:/root/OTV/New/OTV_Coherence-0.0.1-SNAPSHOT-jar-with-dependencies.jar!/tangosol-coherence.xml"2011-10-09 21:25:38.085/3.517 Oracle Coherence n/a <Info> (thread=main, member=n/a): Loaded operational overrides from "jar:file:/root/OTV/New/OTV_Coherence-0.0.1-SNAPSHOT-jar-with-dependencies.jar!/tangosol-coherence-override-dev.xml"2011-10-09 21:25:38.522/3.954 Oracle Coherence n/a <Info> (thread=main, member=n/a): Loaded operational overrides from "jar:file:/root/OTV/New/OTV_Coherence-0.0.1-SNAPSHOT-jar-with-dependencies.jar!/tangosol-coherence-override.xml"2011-10-09 21:25:38.554/3.986 Oracle Coherence n/a <D5> (thread=main, member=n/a): Optional configuration override "/custom- mbeans.xml" is not specifiedOracle Coherence Version n/a Build n/aGrid Edition: Development mode Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.2011-10-09 21:25:40.946/6.378 Oracle Coherence GE n/a <Info> (thread=main, member=n/a): Loaded cache configuration from "jar:file:/root/OTV/New/OTV_Coherence-0.0.1-SNAPSHOT-jar-with-dependencies.jar!/otv-coherence-cache-config.xml"2011-10-09 21:25:42.665/8.097 Oracle Coherence GE n/a <D4> (thread=main, member=n/a): TCMP bound to /y.y.y.y:8089 using SystemSocketProvider2011-10-09 21:25:43.266/8.698 Oracle Coherence GE n/a <Info> (thread=Cluster, member=n/a): Failed to satisfy the variance: allowed=16, actual=312011-10-09 21:25:43.266/8.698 Oracle Coherence GE n/a <Info> (thread=Cluster, member=n/a): Increasing allowable variance to 17 2011-10-09 21:25:43.599/9.031 Oracle Coherence GE n/a <Info> (thread=Cluster, member=n/a): This Member(Id=2, Timestamp=2011-10-09 21:25:38.68, Address=y.y.y.y:8089, MachineId=12097, Location=machine:ebt,process:29580, Role=OTV2, Edition=Grid Edition, Mode=Development, CpuCount=32, SocketCount=32) joined cluster "OTV" with senior Member(Id=1, Timestamp=2011-10-09 21:24:46.935, Address=x.x.x.x:8089, MachineId=12096, Location=machine:dbt,process:13723, Role=OTV1, Edition=Grid Edition, Mode=Development, CpuCount=64, SocketCount=64)2011-10-09 21:25:43.649/9.081 Oracle Coherence GE n/a <D5> (thread=Cluster, member=n/a): Member 1 joined Service Cluster with senior member 12011-10-09 21:25:43.650/9.082 Oracle Coherence GE n/a <D5> (thread=Cluster, member=n/a): Member 1 joined Service Management with senior member 12011-10-09 21:25:43.650/9.082 Oracle Coherence GE n/a <D5> (thread=Cluster, member=n/a): Member 1 joined Service MapDistCache with senior member 12011-10-09 21:25:43.656/9.088 Oracle Coherence GE n/a <Info> (thread=main, member=n/a): Started cluster Name=OTVWellKnownAddressList(Size=2,WKA{Address=y.y.y.y, Port=8089}WKA{Address=x.x.x.x, Port=8089})MasterMemberSet(ThisMember=Member(Id=2, Timestamp=2011-10-09 21:25:38.68, Address=y.y.y.y:8089, MachineId=12097, Location=machine:ebt,process:29580, Role=OTV2) OldestMember=Member(Id=1, Timestamp=2011-10-09 21:24:46.935, Address=x.x.x.x:8089, MachineId=12096, Location=machine:dbt,process:13723, Role=OTV1)ActualMemberSet=MemberSet(Size=2, BitSetCount=2Member(Id=1, Timestamp=2011-10-09 21:24:46.935, Address=x.x.x.x:8089, MachineId=12096, Location=machine:dbt,process:13723, Role=OTV1)Member(Id=2, Timestamp=2011-10-09 21:25:38.68, Address=y.y.y.y:8089, MachineId=12097, Location=machine:ebt,process:29580, Role=OTV2))RecycleMillis=1200000RecycleSet=MemberSet(Size=0, BitSetCount=0))TcpRing{Connections=[1]} IpMonitor{AddressListSize=1}2011-10-09 21:25:43.812/9.248 Oracle Coherence GE n/a <D5> (thread=Invocation:Management, member=2): Service Management joined the cluster with senior service member 12011-10-09 21:25:45.230/10.662 Oracle Coherence GE n/a <D5> (thread=DistributedCache:MapDistCache, member=2): Service MapDistCache joined the cluster with senior service member 12011-10-09 21:25:45.482/10.914 Oracle Coherence GE n/a <D4> (thread=DistributedCache:MapDistCache, member=2): Asking member 1 for 128 primary partitions2011-10-09 21:25:45.840/11.272 Oracle Coherence GE n/a <D5> (thread=DistributedCache:MapDistCache, member=2): Deferring the distribution due to 128 pending configuration updates09.10.2011 21:25:46 DEBUG (UserMapListener.java:23) - Inserted Key = user3, Value = name : Anna, surname : Kornikova 09.10.2011 21:25:46 DEBUG (UserMapListener.java:23) - Inserted Key = user4, Value = name : Natalie, surname : Portman 09.10.2011 21:25:46 DEBUG (TestCacheExecutor.java:43) - *************************************** 09.10.2011 21:25:46 DEBUG (TestCacheExecutor.java:46) - Distributed Cache Content : name : Anna, surname : Kornikova 09.10.2011 21:25:46 DEBUG (TestCacheExecutor.java:46) - Distributed Cache Content : name : Bruce, surname : Willis 09.10.2011 21:25:46 DEBUG (TestCacheExecutor.java:46) - Distributed Cache Content : name : Natalie, surname : Portman 09.10.2011 21:25:46 DEBUG (TestCacheExecutor.java:46) - Distributed Cache Content : name : Clint, surname : Eastwood 09.10.2011 21:25:46 DEBUG (TestCacheExecutor.java:48) - ***************************************步驟12:下載
OTV_Coherence
參考: Online Technology Vision博客上的JCG合作伙伴 Eren Avsarogullari提供的Oracle Coherence中的分布式數據管理 。
翻譯自: https://www.javacodegeeks.com/2012/06/oracle-coherence-distributed-data.html
總結
以上是生活随笔為你收集整理的Oracle Coherence:分布式数据管理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 9WinMap 映射
- 下一篇: 爬取猫眼数据