Dubbo is integrated with Zookeeper and Spring
Dubbo uses a full Spring configuration mode to transparently access applications without any API intrusion. You only need to load Dubbo’s configuration with Spring. Dubbo is loaded based on Spring’s Schema extension.
1: Single-machine deployment mode ZooKeeper is installed
1, download the zookeeper registry, download address: www.apache.org/dyn/closer…. E:\zookeeper-3.3.6\zookeeper-3.3.6\bin,
Double-click zkServer.cmd to start the registry service.
Zkserver. sh [Linux] or zkserver. CMD [Windows]
The Zookeeper configuration file is in the conf directory. This directory contains zoo_sample. CFG and log4j.properties. All you need to do is rename zoo_sample. CFG to zoo.cfg because Zookeeper looks for this file as the default configuration file when it starts. The following details the meaning of each configuration item in this configuration file.
•tickTime: Indicates the heartbeat interval between Zookeeper servers or between clients and servers. That is, each tickTime sends a heartbeat.
•dataDir: Indicates the directory in which Zookeeper saves data. By default, Zookeeper saves log files that write data in this directory.
•dataLogDir: Indicates the directory where Zookeeper saves log files
•clientPort: This port is used by the client to connect to the Zookeeper server. Zookeeper listens to this port and receives access requests from the client
After the configuration, ZooKeeper listens on port 2181 of the local host.
After these configuration items are configured, you can now start Zookeeper. To check whether Zookeeper is already in service, run the netstat -ano command to check whether the clientPort number you configured is listening for the service.
Two: service providers
Define the service interface :(this interface needs to be packaged separately and shared between service providers and consumers)
view plain
copy
print
?
- package com.unj.dubbotest.provider;
- import java.util.List;
- public interface DemoService {
- String sayHello(String name);
- public List getUsers();
- }
Implement interface at service provider :(hide implementation from service consumer)
view plain
copy
- package com.unj.dubbotest.provider.impl;
- import java.util.ArrayList;
- import java.util.List;
- import com.unj.dubbotest.provider.DemoService;
- public class DemoServiceImpl implements DemoService {
- public String sayHello(String name) {
- return “Hello ” + name;
- }
- public List getUsers() {
- List list = new ArrayList();
- User u1 = new User();
- u1.setName(“hejingyuan”);
- u1.setAge(20);
- u1.setSex(“f”);
- User u2 = new User();
- u2.setName(“xvshu”);
- u2.setAge(21);
- u2.setSex(“m”);
- list.add(u1);
- list.add(u2);
- return list;
- }
- }
Exposing services with Spring configuration declarations:
view plain
copy
print
?
- <beans xmlns=”http://www.springframework.org/schema/beans”
- xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:dubbo=”http://code.alibabatech.com/schema/dubbo”
- xsi:schemaLocation=”http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd
- “>
- <! — Specific implementation bean –>
- <bean id=”demoService” class=”com.unj.dubbotest.provider.impl.DemoServiceImpl” />
- <! — Provider application information for calculating dependencies –>
- <dubbo:application name=”xs_provider” />
- <! Use multicast broadcast registries to expose service addresses
- <! – “dubbo: registry address =” multicast: / / 224.5.6.7:1234 “/ > — >
- <! Use the ZooKeeper registry to expose the service address — that is, the IP address and port number of the zooKeeper server –>
- “Dubbo: registry address =” zookeeper: / / 192.168.24.213:2181 “/ >
- <! Expose service on port 20880 with dubbo
- <dubbo:protocol name=”dubbo” port=”20880″ />
- <! Declare the service interface to be exposed –>
- <dubbo:service interface=”com.unj.dubbotest.provider.DemoService”
- ref=”demoService” />
- </beans>
Load the Spring configuration and start the service (or build the project as a Web project, configure the Spring startup in web.xml, and throw it into Tomcat to provide the service) :
view plain
copy
- package com.unj.dubbotest.provider.impl;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Provider {
- public static void main(String[] args) throws Exception {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
- new String[] { “applicationContext.xml” });
- context.start();
- System.in.read(); // To keep the service on, use the blocking of the input stream to simulate
- }
- }
Three: service consumers
Referencing remote services via Spring configuration:
view plain
copy
- <beans xmlns=”http://www.springframework.org/schema/beans”
- xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:dubbo=”http://code.alibabatech.com/schema/dubbo”
- xsi:schemaLocation=”http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd
- “>
- <! — Consumer application name, used to calculate dependencies, not matching conditions, not the same as provider –>
- <dubbo:application name=”hjy_consumer” />
- <! — Use zooKeeper registry to expose service address –>
- <! – “dubbo: registry address =” multicast: / / 224.5.6.7:1234 “/ > — >
- “Dubbo: registry address =” zookeeper: / / 192.168.24.213:2181 “/ >
- <! Create a remote service proxy that can be used as a local bean –>
- <dubbo:reference id=”demoService”
- interface=”com.unj.dubbotest.provider.DemoService” />
- </beans>
Calling the service test:
view plain
copy
- package com.alibaba.dubbo.demo.pp;
- import java.util.List;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.unj.dubbotest.provider.DemoService;
- public class Consumer {
- public static void main(String[] args) throws Exception {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
- new String[] { “applicationContext.xml” });
- context.start();
- DemoService demoService = (DemoService) context.getBean(“demoService”);
- String hello = demoService.sayHello(“hejingyuan”);
- System.out.println(hello);
- List list = demoService.getUsers();
- if (list ! = null && list.size() > 0) {
- for (int i = 0; i < list.size(); i++) {
- System.out.println(list.get(i));
- }
- }
- System.in.read();
- }
- }
Test results:
Complete project source code
Willing to understand the framework technology or source code of friends directly ask to exchange technology: 2042849237
J2ee distributed architecture, restful, kafka, shiro, Spring MVC, mybatis, redis, dubbo, zookeeper