2.dlc-core配置


2.1. 引入dlc-core构件

dlc-core与应用集成,需在应用系统的pom文件中引入dlc-core构件:

<dependency>
    <groupId>com.happygo.dlc</groupId>
    <artifactId>dlc-core</artifactId>
    <version>最新版本</version>
</dependency>

2.2. 配置dlc-ignite文件

在应用系统中src/main/resources下建立一个目录,命名为config,在config下新建一个dlc-default.xml以及dlc-ignite.xml文件,下面两个xml文件的配置,拿来即用,需将<value>127.0.0.1:47500..47509</value>替换成真实IP以及如下中的value替换成对应应用名称,key无需替换

<map>
    <entry key="ROLE" value="dlc-example"/>
</map>

模板如下:

dlc-default.xml

<?xml version="1.0" encoding="UTF-8"?>

<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<!--
    Ignite configuration with all defaults and enabled p2p deployment and enabled events.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd">

    <!-- Datasource for sample in-memory H2 database. -->
    <bean id="h2-dlc-db" class="org.h2.jdbcx.JdbcDataSource">
        <property name="URL" value="jdbc:h2:tcp://localhost/mem:DlcDb" />
        <property name="user" value="sa" />
    </bean>

    <bean abstract="true" id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">
        <!-- Set to true to enable distributed class loading for examples, default is false. -->
        <property name="peerClassLoadingEnabled" value="true"/>

        <!-- set cluster node attributes-->
        <property name="userAttributes">
            <map>
                <entry key="ROLE" value="dlc-example"/>
            </map>
        </property>

        <!-- Enable task execution events for examples. -->
        <property name="includeEventTypes">
            <list>
                <!--Task execution events-->
                <util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_STARTED"/>
                <util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_FINISHED"/>
                <util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_FAILED"/>
                <util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_TIMEDOUT"/>
                <util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_SESSION_ATTR_SET"/>
                <util:constant static-field="org.apache.ignite.events.EventType.EVT_TASK_REDUCED"/>

                <!--Cache events-->
                <util:constant static-field="org.apache.ignite.events.EventType.EVT_CACHE_OBJECT_PUT"/>
                <util:constant static-field="org.apache.ignite.events.EventType.EVT_CACHE_OBJECT_READ"/>
                <util:constant static-field="org.apache.ignite.events.EventType.EVT_CACHE_OBJECT_REMOVED"/>
            </list>
        </property>

        <!-- Explicitly configure TCP discovery SPI to provide list of initial nodes. -->
        <property name="discoverySpi">
            <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
                <property name="ipFinder">
                    <!--
                        Ignite provides several options for automatic discovery that can be used
                        instead os static IP based discovery. For information on all options refer
                        to our documentation: http://apacheignite.readme.io/docs/cluster-config
                    -->
                    <!-- Uncomment static IP finder to enable static-based discovery of initial nodes. -->
                    <!--<bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder">-->
                    <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder">
                        <property name="addresses">
                            <list>
                                <!-- 替换成部署机器的真实IP -->
                                <value>127.0.0.1:47500..47509</value>
                            </list>
                        </property>
                    </bean>
                </property>
            </bean>
        </property>
    </bean>
</beans>

dlc-ignite.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<!--
    Ignite configuration with all defaults and enabled p2p deployment and enabled events.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- Imports default Ignite configuration -->
    <import resource="dlc-default.xml"/>

    <bean parent="ignite.cfg"/>
</beans>

2.3. 日志组件文件引入Lucene Appender

  • log4j1.X日志组件log4j.xml引入Lucene Appender
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd">
<log4j:configuration>
    <!-- 自定义LuceneAppender -->
    <appender name="luceneAppender" class="com.happygo.dlc.logging.Log4jLuceneAppender">
        <param name="Target" value="/opt/applog/lucene_index" />
        <param name="SystemName" value="demo-log4j" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n" />
        </layout>
    </appender>

    <root>
        <level value="info" />
        <appender-ref ref="luceneAppender" />
    </root>
</log4j:configuration>

说明:由于log4j1.x自身问题,索引字段只能写死在Appender中,如果想在配置文件中增加索引字段,需扩展Log4jLuceneAppender类

  • log4j2.X日志组件log4j2.xml引入Lucene Appender

    通过IndexField标签记录lucene需要记录的索引字段,用户可自己定制需要哪些字段建立索引,配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<!--设置log4j2的自身log级别为warn-->
<Configuration packages="com.happygo.dlc.logging">
    <Appenders>
        <!-- dlc-core自定义LuceneAppender,target:索引文件存放路径,ignoreExceptions:如果加载log4j时出错,是否忽略异常(可选),expiryTime:索引定期清理时间段设置(单位ms,可选)-->
        <Lucene name="LuceneAppender" target="/opt/applog/lucene_index" ignoreExceptions="true" expiryTime="1296000">
            <!-- 用户可选 -->
            <IndexField name="level" pattern="%-5level"/>
            <!-- 默认必填 -->
            <IndexField name="time" pattern="%d{UNIX_MILLIS}" type="LongField"/>
            <!-- 默认必填 -->
            <IndexField name="content" pattern="%p %c - %m%n"/>
            <!-- 默认必填 -->
            <IndexField name="systemName" pattern="dlc-example"/>
        </Lucene>
    </Appenders>

    <Loggers>
        <Root level="info">
            <AppenderRef ref="LuceneAppender"/>
        </Root>
    </Loggers>

</Configuration>

2.4. 发布dlc服务(Spring)

dlc服务的发布分为两种,clusterSingleton和nodeSingleton,clusterSingleton(集群单例)指部署一个集群范围的单例服务,Ignite会保证集群内会一直有一个该服务的实例。当部署该服务的节点故障或者停止时,Ignite会自动在另一个节点上重新部署该服务;nodeSingleton(节点单例)指部署一个节点范围的单例服务,Ignite会保证每个节点都会有一个服务的实例在运行。当在集群组中启动了新的节点时,Ignite会自动地在每个新节点上部署一个新的服务实例。

  • 在Spring的application.xml中配置
<bean id="dlcServiceExporter" class="com.happygo.dlc.ignite.DlcIgniteServicesExporter" init-method="export">
    <property name="mode" value="nodeSingleton" />
    <property name="service">
        <bean id="dlcService" class="com.happygo.dlc.ignite.service.DlcIgniteServiceImpl" />
    </property>
</bean>
  • Spring java配置
@Bean
public DlcIgniteServicesExporter expoter() {
    DlcIgniteServicesExporter expoter = new DlcIgniteServicesExporter();
    expoter.setMode(DlcConstants.DEPLOY_NODE_SINGLETON);
    DlcIgniteService service = new DlcIgniteServiceImpl();
    expoter.setService(service);
    expoter.export();
    return expoter;
}

results matching ""

    No results matching ""