-
Notifications
You must be signed in to change notification settings - Fork 17
Quick Start
ghl edited this page Aug 25, 2016
·
6 revisions
Name | Version |
---|---|
JDK | 1.8+ |
Spring | 4.1.2+ |
Kafka | 0.8.2+ |
ZooKeeper | 2.9.2+ |
- Git: download source code
git clone git@github.com:KoperGroup/koper.git
- Maven: compile and install to your repo
mvn install
- 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
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
@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());
}
}
@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);
}
}
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);
}
}
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();
}
}
-
- Execute MemberSignUpListenerDemo.main at first
- It means launch Kafka Receiver
-
- 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
".