Learn the contents in the last article, this article we write a local single service demo, let’s see what we need!!

1. Installation of ZooKeeper

(1) ZooKeeper installation is easy. Download the rack package and decompress it. In the decompressed directory, there is a conf directory, that is, the configuration file of ZooKeeper. For example, we have a single service, so we only need to modify the dataDir directory to configure the rest of the dataDir directory can be customized!!

2. Service providers

Create a Maven project and configure spring, Dubbo, ZooKeeper and other related JAR packages.

(1) Directory structure

(2) We configure pom. XML to download the jar package we need. The pom. XML configuration file is as follows:

(3) Write service interface and realize service interface

  1. package com.test.dubboser;
  2. public interface ServiceDemo {
  3. public String say(String str);
  4. }
  1. package com.test.dubboser;
  2. public class ServiceImp implements ServiceDemo{
  3. public String say(String str) {
  4. System.err.println(“server: “+str);
  5. return “hello: “+str;
  6. }
  7. }

(4) Register the service with the registry (expose the interface), in this case the registry is ZooKeeper

Let’s look at the configuration file applicationProvider.xml for the service.

  1. <beans xmlns=”http://www.springframework.org/schema/beans”
  2. xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
  3. xmlns:dubbo=”http://code.alibabatech.com/schema/dubbo”
  4. xsi:schemaLocation=”http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://code.alibabatech.com/schema/dubbo
  7. http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  8. “>
  9. <! — Provider application information, used to calculate dependencies, does not need to be consistent with client –>
  10. <dubbo:application name=”hello-world-app-my” />
  11. <! Use multicast broadcast registries to expose service addresses
  12. <! – “dubbo: registry address =” multicast: / / 192.168.0.101:2181 “/ > — >
  13. <! Expose the service address using the ZooKeeper broadcast registry –>
  14. “Dubbo: registry protocol =” zookeeper “address =” 192.168.0.101:2181 “/ >
  15. <! — Same effect as above –>
  16. <! – “dubbo: registry address =” zookeeper: / / 192.168.0.101:2181 “/ > — >
  17. <! — False cluster test –>
  18. <! – “dubbo: registry protocol =” zookeeper “address =” 192.9.145.19:2181192.9. 145.19:2182192.9. 145.19:2183 “/ > — >
  19. <! Expose service on port 20880 with dubbo
  20. <dubbo:protocol name=”dubbo” port=”20880″ />
  21. <! Declare the service interface to be exposed –>
  22. <dubbo:service interface=”com.test.dubboser.ServiceDemo”
  23. ref=”demoService” />
  24. <! Implement the service as a local bean
  25. <bean id=”demoService” class=”com.test.dubboser.ServiceImp”/>
  26. </beans>

(5) With all the preparations ready, let’s start the service provider (zooKeeper).

  1. package com.test.dubboser;
  2. import java.io.IOException;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class Main {
  5. public static void main(String[] args) throws IOException {
  6. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { “applicationProvider.xml” });
  7. context.start();
  8. System.out.println(” Press any key to exit “);
  9. System.in.read();
  10. }
  11. }

Ok, so our service provider will be fine. Is it very messy? I feel a little bit, so I will have my own summary later…

Well, our service provider, let’s look at our service consumer, the program that calls our service here…

3. Service consumer development

Service consumers are written in the same order as service providers

Willing to understand the framework technology or source code of friends directly beg exchange to share technology: 2042849237 distributed some solutions, a friend willing to understand can find our team to discuss more detailed source code reference source: minglisoft.cn/technology

Dubbo + SpringMVC + Mybatis + Ehcache + Redis J2ee distributed architecture, restful, Kafka, Shiro

Dubbo + SpringMVC + Mybatis + EhCache + Redis J2ee distributed architecture, restful, Shiro