redis를 HA로 구성했을 경우, SDR(spring-data-redis 1.4) 와 jedis커넥션을 이용한 spring과 redis 연동.


RedisSentinelConfiguration 클래스를 이용하면, HA를 지원하는 redis의 이중화 상태에서도 spring을 이용한 redis연동이 가능하다.

java코드로 연동하는 방법은 여러곳에서 나와있으므로, 본 글 에서는

xml을 이용한 DI(Dependency Injection)방식으로   spring과 HA redis를 연동하는 방식을 설명한다.



1. pom.xml 수정   (SDR , jedis를 추가)

이전글과 동일하다. 단 common-pool 대신에 이를 상속받은 JedisPoolConfig을 써봄으로 인해 common-pool은 제거.


<dependency>

<groupId>org.springframework.data</groupId>

<artifactId>spring-data-redis</artifactId>

<version>1.4.0.RELEASE</version>

</dependency>


<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

<version>2.5.1</version>

<type>jar</type>

<scope>compile</scope>

</dependency>




2. application-config.xml 수정


2.1 네임스페이스에  xmlns:p,c, util 추가.


2.2.1 mymaster와 setntinels을 xml로 구성


2.2.2 RedisSentinelContiguration을 이용해서 HA구성.

         => SDR에 미흡한 부분이 하나 존재하여, MySentinelConfiguration  를 별도로 생성해서 이용하도록 함.

             (이유는 아래 xml의 코멘트 참조)   


2.2.3 jedis를 이용한 Pool과 connection구성.


이렇게만 하면 이전 예제와 동일하게 그냥 redisTemplate만을 이용해서 개발하면 자동으로 HA구성을 지원하는 개발이 된다.




<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:c="http://www.springframework.org/schema/c"

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/context http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">


<!-- Uncomment and add your base-package here:-->

<context:component-scan base-package="my"/>


<!-- myster -->

<bean id="myMaster" class= "org.springframework.data.redis.connection.RedisNode" c:host="127.0.0.1" c:port="6379" p:name="mymaster"/>


<!-- Sentinels -->

<bean id="mySent1" class= "org.springframework.data.redis.connection.RedisNode" c:host="127.0.0.1" c:port="26379"/>

<bean id="mySent2" class= "org.springframework.data.redis.connection.RedisNode" c:host="127.0.0.1" c:port="26381"/>

<bean id="mySents" class= "java.util.HashSet">

 <constructor-arg>

  <list>

   <ref bean="mySent1" />

   <ref bean="mySent2" />

  </list>

 </constructor-arg>

</bean>


<!-- RedisSentinelConfiguration: org.springframework.data.redis.connection.RedisSentinelConfiguration" 

2.2.2 이 부분이 유일하게 아직 SDR에서 미흡한 부분이다.  이 부분은 ReidsSentinelConfiguration class 가 

getter와 setter 파라미터가 다른 오류가 발생하므로, 자체 class를 하나 만들어서 해결하였다.

-->

<bean id="redisSentinelConf" class="my.MySentinelConfiguration"

p:master-ref="myMaster"

p:mySentinels-ref="mySents"

/>

<!-- p:sentinels-ref="mySents" : ERROR 발생-->



<!--2.2.3 POOL & Connection-->

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"/>

<bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"

    p:use-pool="true"

    p:poolConfig-ref="jedisPoolConfig"

    c:sentinelConfig-ref="redisSentinelConf"

/>

<!--p:host-name="127.0.0.1" p:port="6379" -->



<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>

<!-- redis template definition -->

<bean id="redisTemplate"

class="org.springframework.data.redis.core.RedisTemplate"

p:connection-factory-ref="jedisConnFactory"

p:keySerializer-ref="stringRedisSerializer"

p:hashKeySerializer-ref="stringRedisSerializer"

p:valueSerializer-ref="stringRedisSerializer"/>

</beans>





3. JAVA  코드 - 유일하게 추가한 MySentinelConfiguration코드이다.  추가만 해 놓으면 된다.


- 단지, SDR의 getter와 setter의 오류를 피하기 위한 클래스이다. 향후에는 SDR에서 개선이 될 것으로 기대된다.


public class MySentinelConfiguration extends RedisSentinelConfiguration {

MySentinelConfiguration(){

super();

}


public Set<RedisNode> getMySentinels(){

Set<RedisNode> sets=super.getSentinels();

return sets;

}


public void setMySentinels( Set<RedisNode> sentinels){

super.setSentinels(sentinels);

}


}


Posted by yongary
,

spring과 redis연동

Spring 2014. 9. 18. 09:26

SDR(spring-data-redis) 와 jedis커넥션을 이용한 spring을 이용한 redis 연동.


spring에서 redis를 연결하기 위한 기본적인 방법을 커넥션 위주로 설명.

실제로는 redis에 저장할 데이타 class를 별도로 만들어 serialize기능을 이용해 {key,class}형태로 저장하는 방식이 바람직하지만, 여기서는 이 부분은 생략하고 기본적인 연결과정만 설명한다. 


1. pom.xml 수정   (SDR , common-pool,  jedis를 추가)


<dependency>

<groupId>org.springframework.data</groupId>

<artifactId>spring-data-redis</artifactId>

<version>1.4.0.RELEASE</version>

</dependency>

<dependency>

<groupId>commons-pool</groupId>

<artifactId>commons-pool</artifactId>

<version>1.6</version>

</dependency>

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-pool2</artifactId>

<version>2.0</version>

</dependency>

<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

<version>2.5.1</version>

<type>jar</type>

<scope>compile</scope>

</dependency>






2. application-config.xml 수정


2.1 네임스페이스에  xmlns:p 추가.

2.2  jedisConnnection, Serializer, redisTemplate 추가.


<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <!-- Uncomment and add your base-package here:-->

    <context:component-scan base-package="my"/>


<bean id="jedisConnFactory"

    class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"

    p:use-pool="true" p:host-name="127.0.0.1" p:port="6379" />


<bean id="stringRedisSerializer"

    class="org.springframework.data.redis.serializer.StringRedisSerializer"/>


<!-- redis template definition -->

<bean id="redisTemplate"

class="org.springframework.data.redis.core.RedisTemplate"

p:connection-factory-ref="jedisConnFactory"

p:keySerializer-ref="stringRedisSerializer"

p:hashKeySerializer-ref="stringRedisSerializer"

p:valueSerializer-ref="stringRedisSerializer"/>


</beans>



  이 중에서 serializer는 추가하지 않아도 동작은 잘 되지만, 

 기본 serializer로 동작하게 되면 redis에 데이타가 들어갈 때, 앞 부분에 xac xed x00 x05t x00 등의 character가 몇개 들어가게 된다.

(이런 이상한 캐릭터들은 standard serializer에서 사용하는 class정보이다.)




3. JAVA Service 코드


 @Service

public class RedisService implements IRedisService{


@Autowired

private RedisTemplate<String,String> redisTemplate;


public Set<String> keysAll() {


//값을 넣고 싶을 땐, 이렇게..

//redisTemplate.opsForValue().set("abcKey", "valueABC");


return redisTemplate.keys("*");

}



}     


Posted by yongary
,

redis 실행법

mongoDB, redis 2014. 9. 16. 17:49

redis 설치 완료 후, src 폴더로 이동해서


1. stand-alone 모드 실행.

./redis-server  로 서버실행

./redis-cli  로 client 실행.

==> stand alone 모드로 default세팅 되어 실행된다.


reds> info          하면 정보 조회가능(데이타 개수도 있음)

redis>  keys *     하면 모든키 조회 가능(데이타가 많을경우는 조심)

 1)"MPx_TYPE_Audiox"  

 2)"FS_HB"

 3)"MP_TYPE_CTRL"
 4)"XCOD_ROOM_x"

 5)"CNFR_ROOM_x"

 

 



2. 마스터-슬레이브 모드

만약, master-slave모드로 테스트를 해보고 싶으면  redis-server를 아래명령으로 하나 더 실행한다.


$>./redis-server --port 6380 --slaveof 127.0.0.1 6379    



혹시, 이 상태에서 상태를 확인해보고 싶으면 client창에서


127.0.0.1:6379>  info  를 입력하면

#Replication 영역에

role:master

connected_slaves:1

      임을  을 확인할 수 있다.  즉 6379서버의 slave가 잘 뜬 것이다. 




3. Sentinel감시를 통한 HA 구성


위의 1,2번이 된 상태에서  아래와 같이 sentinel을 2개 정도 실행시키고....

$> src/redis-server sentinel1.conf --sentinel

$> src/redis-server sentinel2.conf --sentinel

==> 이러면 기본적인 HA완성.


이 때 1번 서버를 kill등으로 죽이면, 절체(HA)가 발생한다.


절체발생 후, 

client는 새로이 아래와 같은 명령으로 접속한다. 즉 2번 서버로 접속한다. 

$> redis-cli -p 6380  



ㅁ 좀 더 쉽게 절체를 발생시키는 방법은

redis-cli -p 26379 로 센티널로 cli접속을 한 다음에

>  sentinel failover mymaster    를 입력하면 바로 절체가 발생한다. 


  ( http://redis.io/topics/sentinel  참조)

  







 

Posted by yongary
,

vi에디터 한글세팅

Linux 2014. 9. 16. 17:12

한글인코딩을 euc-kr로 할지, utf-8로 할지 고민이 되는 경우,

한국에서만 쓸 것이라면 둘 다 상관이 없고 euc-kr이 좀 더 나을 수도 있을 것 같고

혹시 외국에서도 쓸지 모른다는 생각이 든다면 utf-8이 좋을 것 같다.


대세는 utf-8이라고 보고.. utf-8로 세팅하는 방법을 보면 다음과 같다.


$> env  혹은 locale 을 해보면  os의 LANG을 알 수 있다.

대부분 xx_xx.utf-8 로 되어있다면 문제가 없다.


1. vi 에디터에서 파일변환

:set fileencodings=utf-8  후에 :w 하고 다시 시작.



2. 프롬프트 상에서 파일변환은

iconv  -f euc-kr -t utf-8   euc-kr.html > utf8.html



혹시 

3. vi에디터에서 안보이면

:set enc=utf-8 




참고:   http://www.technet.co.kr/bbs/board.php?bo_table=tip&wr_id=263&page=3 

인코딩 설명:  http://helloworld.naver.com/helloworld/19187 

Posted by yongary
,

< 함수의 실행시간을 milisecond단위로 측정하는 방법. > 



struct timeval을 이용하여 함수의 실행시간을 측정하는 방법인데,


 struct timeval은 초와 micro초로 이루어져 있으므로,

- 초에는 *1000을 

 - micro초에는 /1000 을 해야 milisecond가 된다.


struct timeval { long tv_sec; // 초 long tv_usec; // micro }



=======사용예제================================================

#include <sys/time.h>


struct timeval before,after;  //선언


//사용 
    gettimeofday(&before,NULL);
    run_my_function( );
    gettimeofday(&after,NULL);

    printf("function running time[ms] : %ld\n",
         1000*(after.tv_sec-before.tv_sec) +   
         (after.tv_usec-before.tv_usec)/1000);





Posted by yongary
,


Linux에서 c언어 개발 중,


make는 완료 했으나

실행 중에 .so(공유 library)를 못 찾는 경우 다음과 같이 해결하면 된다.

(이 때, 에러메시지:  cannot open shared object file - no such file or directory)



(UBUNTU)

1. /etc/ld.so.conf.d/  밑에  myApp.conf 파일 추가

2. myApp.conf 파일 첫 줄에

  /usr/lib/myApp/lib  과 같이 특정한 위치 지정    

3. sudo ldconfig (Reload임)



(Debian)

1. /etc/ld.so.conf 편집

2. 마지막 줄에 /usr/lib/myApp/lib 과 같은 특정한 위치 추가

3. ldconfig (Reload임)    (조회는 ldconfig -v)



참조: http://blog.andrewbeacock.com/2007/10/how-to-add-shared-libraries-to-linuxs.html 



또다른 간단한 방법으로는

run 쉘스크립트를 하나 만드는 것이다.


#!/bin/bash

LD_LIBRARY_PATH=/home/myid/common/lib:$LD_LIBRARY_PATH

export LD_LIBRARY_PATH

./test_program

Posted by yongary
,

spring을 이용해서 mongoDB를 연동할 경우 방법은 다음과 같다.


1. pom.xml 수정 : spring-data-mongodb 추가


         <dependency>

       <groupId>org.springframework.data</groupId>

       <artifactId>spring-data-mongodb</artifactId>

       <version>1.5.1.RELEASE</version>

</dependency>

        

      참고: http://projects.spring.io/spring-data-mongodb/

      




2. application-config.xml 수정.   ( 주로 resources/spring/ 밑에 위치)


    2.1 네임스페이스 수정   :  xmlns:mongo 한 줄 및  xsi:schemaLocation 2줄 추가.

    2.2 mongo:db-factory 추가 및  mongoTemplate 추가



   <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mongo="http://www.springframework.org/schema/data/mongo"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/data/mongo

        http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">

    

<!-- Uncomment and add your base-package here:-->

    <context:annotation-config /> 

    <context:component-scan base-package="my"/> 

   

    <mongo:mongo host="localhost" port="27017"/>

    <mongo:db-factory   dbname="kcdb"/>   


    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">

 <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>

    </bean>  

</beans>





3. Vo(value object) 와 유사한   java파일 작성. ( @Document와  @Id를 포함하도록 한다. )

       

    @Document    //mongoDB에서 persistent한 도메인으로 인식하게 되는 어노테이션. 즉 테이블로 인식.

     public class User{


@Id       // mongoDB의 테이블(category)에서 데이타별 고유id로 자동생성 되는 id에 매핑하기 위함) 

private String id;


private String username;


//setters & getters ... 

}




4. Service에서 mongoTemplate를 이용한 query작성.

 

  @Service

  public class MongoService implements IMongoService{


@Autowired

private MongoTemplate mongoTemplate;

     

  public User getUser(String userName) {


Query query = new Query( where("username").is(userName) );

User user=mongoTemplate.findOne(query, User.class);

return user;

}


   }

Posted by yongary
,

스마트폰을 이용한 서비스를 개발하다 보면

DB에 사용자를 저장하거나, 사용자별로 정보를 관리하거나 하기 위해서 사용자의 고유ID가 필요하게 된다.


고유ID로 제일 쉬운 방법으로는


1. 회원ID: 회원가입 유도 후 사용자별 ID생성  (혹은 eMail ID 등) 


그러나 회원가입 자체를 부담스러워 하는 서비스도 많으므로,

회원가입없이 유저별로 구분을 하려면

2, 3, 4번과 같이 전화번호나 기기의 고유번호를 사용할 수 있다.


2. MSISDN: 전화번호  =  국가코드(82) + 전화번호(010-1234-5678 )      

3. IMSI: 통신사가입 아이디(15자리) = MCC(3자리) + MNC(2~3자리) + MSIN(9~10자리)   

                                            = 450(한국) + 05(특정이통사) + 1234567890


4. IMEI :  하드웨어 고유값 (15자리) - 휴대폰에서  *#06# 을 입력하면 확인 됨.  
                                              (전화통화를 위해서 기기에 꼭 필요)



(최근에는 주민번호에 이어 전화번호도 수집하지 못하게 한다는~~

그리고 2,3,4번의 경우 WiFi전용 갤플레이어나 IPod 같은 경우 값이 없다,

 - 2,3번은 USIM카드에 있고, 4번은 usim장착이 가능한 기기에 박혀 나옴)


따라서 스마트폰 뿐 아니라 WiFi전용기기도 수용하는 서비스의 경우에는 아래 5번을 이용하는 방법을 고려한다.


5. android_ID 및 인스턴스ID( IOS)

   => 기기 초기화나 리셋시에 값이 바뀌는 문제는 존재



요약하면, 모든 상황을 커버할 수 있는 최적의 고유ID는 없으므로

서비스의 상황에 가장 적합한 항목을

상기 항목들 중에 선택해서 서비스를 개발해야 할 것이다.  




     

Posted by yongary
,

우분투 14.04 LTS를 설치하고 나서 눈에 띄는 불편함 및 해결 포인트는 


1. 터미널은 어떻게 실행하지?

   CTRL+ALT+T 로 실행.

   ->  한 번 실행만 하면, 사이드 바에 뜨므로 이 때 사이드 바의 아이콘에 우측마우스를 클릭해서 고정이 가능하다.



2.  한글입력이 안된다. (혹은 한영 전환이 안된다)

   - 상단 status바에 한글(태극기 마크 아이콘)을 우측클릭한다.

   - "텍스트 입력창 설정..." 메뉴를 선택한다.

   -  좌측 사용할 입력 소스:  에다가 영어(혹은 English)를 추가한다.

      => 우측 소스 전환시 키를 바꿀 수 있다.  (보통 Shift + 스페이스  선호.)



3. 화면밝기 조절이 안된다. 

   - Intel 그래픽 카드로 설정이 안되서 그런 경우..    아래 파일 추가.

    $> vi  /usr/share/X11/xorg.conf.d/20-intel.conf

  Section "Device"

Identifier "card0" Driver "intel" Option "Backlight" "intel_backlight" BusID "PCI:0:2:0" EndSection

     ( http://itsfoss.com/fix-brightness-ubuntu-1310/  참고)




4. 밝기 조절은 되지만, 껐다 켜면 reset이 되는 경우.  
   ( xbacklight 를 설치하고,   .profile에 값 설정.)


    - sudo apt-get install xbacklight

- 홈 디렉토리의 .profile에 xbacklight -set 40 이라고 삽입. (값은 0~100 백분률임)



5. ipv6관련 오류가 한번씩 뜨는 게 감지되어서 ipv6 disable. (이것으로 인해 대기모드나 절전모드로 자동으로 한번씩 빠졌음) 


$> vi /etc/sysctl.conf

해서 아래 3~4줄 추가.    

#disable ipv6
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1

   시스템 rebooting 후

   $>  cat /proc/sys/net/ipv6/conf/all/disable_ipv6  해보고,

       1  

    이라고 나오면 정상적으로 ipv6기능이 disable된 것임.



==>(한달 후)  추가적으로 특정 wifi에 접속시 avahi데몬 오류가 나는 경우도 발생되어

/etc/avahi/avahi-daemon.conf  에서 use_ipv6=no 로 수정.



Posted by yongary
,

Ubuntu설치 완료 후, windows를 설치하고 싶어졌다.

windows 노트북이 따로 있긴 하지만, 휴대성이 불편해서 간혹 필요할지도 모르는 windows를 설치하게되었다.



1. Windows용 NTFS 파티션 확보 필요.


  - 우분투의 /home 폴더를 분할해서 NTFS파티션 확보 필요

  - 우분투에 Gparted를 설치해서 파티션을 분할하면 되지만, Gparted에서 /home영역은 마운트 해제가 되지 않는다.

        => 왜냐하면, 로그인한 유저가 /home을 사용하고 있기 때문이다.


  - 그래서, 부랴부랴 새로운 유저를 /tmphome이라는 폴더를 home 사용하도록 만듦.

     $> adduser tmpuser -d /tmphome      참고) http://mintnlatte.tistory.com/256 


  - 그 후 tmpuser로 로그인해서, Gparted에서 마운트해제 후  파티션을 재조정 함.    (Windows NTFS용으로 주 파티션 24G 확보)



2. WIndows USB를 만들어서, Windows 설치.

   

   - 자세한 사항은 고수분들 사이트 참조:       http://deviantcj.tistory.com/469      http://jjorae.com/posts/394  




3. WIndows 설치가 완료되면 항상 Window로 부팅이 됨. boot-repair를 설치하여  GRUB 수정 필요 

  

     boot-repair설치 명령이 14.04에서 좀 바뀜. ( http://askubuntu.com/questions/449428/unable-to-locate-package-boot-repair-in-14-04 )

   - sudo add-apt-repository ppa:yannubuntu/boot-repair

- sudo sh -c "sed -i 's/trusty/saucy/g' /etc/apt/sources.list.d/yannubuntu-boot-repair-trusty.list"

- sudo apt-get update sudo apt-get install -y boot-repair && boot-repair



4. 완료. 

Posted by yongary
,