Skip to content

Quick Start

ghl edited this page Aug 25, 2016 · 6 revisions

Koper Quick Start

1.1 System Requirements

Name Version
JDK 1.8+
Spring 4.1.2+
Kafka 0.8.2+
ZooKeeper 2.9.2+

1.2 Get Started with Koper

1.2.1 Prepare

  1. Git: download source code
git clone git@github.com:KoperGroup/koper.git
  1. Maven: compile and install to your repo
mvn install
  1. Kafka: download and start

download:

# we recommend you to unzip this file to /usr/local directory
cd /usr/local

wget http://mirrors.cnnic.cn/apache/kafka/0.8.2.2/kafka_2.11-0.8.2.2.tgz
tar xzf kafka_2.11-0.8.2.2.tgz

start: we need to launch zookeeper server first, cause kafka rely on it.

# launch zookeeper server, default port: 2181
cd kafka(directory)
bin/zookeeper-server-start.sh config/zookeeper.properties

# launch kafka server, default port: 9092
bin/kafka-server-start.sh config/server.properties

Zhaime Corporation also provides a server for you to quick start.

  • Kafka server: 120.26.225.169:9092
  • Zookeeper server: 120.26.225.169:2181

1.2.2 Develop a hello world with Koper

Go directly to the Koper project(demo module),

All you need do is to defind your own Message Producer and Message Consumer.

For example:

Write the code

1) Message Producer
@Service
public class MemberService {
    @Autowired
    private MemberMapper memberMapper;

    private static final String MEMBER_SIGNUP_TOPIC = "koper.demo.message.notifyMemberAfterSignup";

    @Autowired
    private MessageSender messageSender;

    public void signup(Member member) {
        memberMapper.createMember(member);
        messageSender.send(MEMBER_SIGNUP_TOPIC, "Signed up successfully! " + member.getPhoneNo());
    }

}
2) Message Consumer
@Component
public class MemberSignupListener extends AbstractMessageListener {

    @Autowired
    private SmsService smsService;

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Listen(topic = "koper.demo.message.notifyMemberAfterSignup")
    @Override
    public void onMessage(String msg) {
        logger.info("Got msg, msg: {}", msg);
        smsService.sendSms(msg);
    }

}
3) MemberSignUpDemo
public class MemberSignUpDemo {
    public static void main(String[] args) {
        final ApplicationContext context =
                new ClassPathXmlApplicationContext("classpath:kafka/context-data-producer.xml");
        final MemberService memberService = context.getBean(MemberService.class);
        
        Member member = new Member();
        member.setName("LeBron James");
        member.setPhoneNo("15097300863");
        memberService.signup(member);
    }
}
4) MemberSignUpListenerDemo
public class MemberSignUpListenerDemo {
    public static void main(String[] args) {
        final ApplicationContext context =
                new ClassPathXmlApplicationContext("classpath:kafka/context-data-consumer.xml");
        final ConsumerLauncher consumerLauncher = context.getBean(ConsumerLauncher.class);
        // we have close the switch in context-data-consumer.xml profile(autoStart) temporary
        consumerLauncher.start();
    }
}
    1. Execute MemberSignUpListenerDemo.main at first
    • It means launch Kafka Receiver
    1. Execute MemberSignUpDemo.main second
    • It will send message to kafka.
    • Topic: koper.demo.message.notifyMemberAfterSignup.
    • Message: "Signed up successfully! 15097300863".

At the same time, observe the log of the console,

console should contain "Got msg, msg: Signed up successfully! 15097300863".

Home

  • [Quick Start](Quick Start)
  • [User Guide](User Guide)
    • [Async Scenarios and examples](Async Scenarios and examples)
  • Performance
  • [Developer Guide](Developer Guide)
  • QA

主页

Clone this wiki locally