Getting started with dubbo (zookeeper is the registry)

1, To build a zookeeper in the registry, first download the zookeeper package ...

1, To build a zookeeper in the registry, first download the zookeeper package

After downloading the compression package, the next steps are as follows: decompress = = > enter the conf directory of the decompressed file = = > the file name of zoo_sample.cfg is changed to zoo.cfg = = > dataDir = XXX is changed to dataDir=../data and saved = = > create a new folder of the same level as conf data = = > Open CMD in the directory of zookeeper-3.4.11, execute zkServer.cmd, and zookeeper will start (Port default 2181, to modify the end You need to modify the zoo.cfg file.

Download link: https://archive.apache.org/dist/zookeeper/zookeeper-3.4.11/

2, Download dobbo management console

Download link: https://github.com/apache/dubbo-admin/tree/master

After downloading the compressed package, the next steps are as follows: after decompression, enter Dubbo admin = = > Open CMD = = > MVN clean package = = > start command: java -jar xxx.jar start the jar package in the target directory (make sure to execute the command in the directory of the jar package, where the jar package is in the target directory, so switch to the target directory) = > Enter localhost:7001 in the browser to visit = = > enter the account after opening the page Password, as follows

After successful login, the interface is as follows

3, Three party tool code (reusable service or bean is put under this package and introduced to producer Dubbo provider and consumer Dubbo consumer as dependency)

H

3.1 pom.xml

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>dubbo-api</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <java.version>1.8</java.version> </properties> </project>

3.2 HelloService

package com.example.dubboapi.service; public interface HelloService { String sayHello(String name); }

4, Producer or service provider

4.1 project structure: focus on the red frame

4.2 pom.xml (introduce dubbo dependency, client-side dependency to operate zookeeper, tripartite toolkit dependency). Pay attention to the one-to-one correspondence between dubbo version and zookeeper client. The version before dubbo 2.6.0 corresponds to zkclient, and the version after dubbo 2.6.0 corresponds to cursor.

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.demo</groupId> <artifactId>dubbo-provider</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!--dubbo rely on--> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <!--Three party tools--> <dependency> <groupId>com.example</groupId> <artifactId>dubbo-api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>

4.3 provider.xml (under the resources directory), four elements: application, registry, protocol, service

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- Provider application information for dependency calculation --> <dubbo:application name="hello-world-app" /> <!-- Use multicast Broadcast registry exposure service address --> <dubbo:registry address="zookeeper://localhost:2181" /> <!-- use dubbo Protocol exposes service on port 20880 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- Declare the service interface to be exposed --> <dubbo:service interface="com.example.dubboapi.service.HelloService" ref="helloService" /> <!-- And local bean Same implementation services --> <bean id="helloService" /> </beans>

4.4 HelloServiceImpl

package com.example.dubboprovider.rpc; import com.example.dubboapi.service.HelloService; public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { return "hello "+name; } }

4.5 Provider

The "provider. XML" in ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml") is because the provider.xml is in the resources directory.

package com.example.dubboprovider; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Provider { public static void main(String[] args) throws Exception{ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml"); context.start(); System.in.read(); } }

5, Consumer

Project structure: focus on the red frame.

5.1 pom.xml is the same as the producer

5.2 consumer.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- The consumer application name is used to calculate the dependency. It is not a matching condition. It should not be the same as the provider --> <dubbo:application name="consumer-of-helloworld-app" /> <!-- Use multicast Broadcast registry exposure discovery service address --> <!--<dubbo:registry address="multicast://224.5.6.7:1234" />--> <dubbo:registry address="zookeeper://localhost:2181" /> <dubbo:protocol name="dubbo" port="20880" /> <!-- Generate remote service agent, which can be connected with local bean Same use demoService --> <dubbo:reference id="helloService" interface="com.example.dubboapi.service.HelloService" /> </beans>

5.3 Consumer

package com.example.dubboconsumer; import com.example.dubboapi.service.HelloService; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Consumer { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml"); context.start(); HelloService helloService = (HelloService) context.getBean("helloService"); // Get remote service agent String hello = helloService.sayHello("world"); // Execute remote method System.out.println( hello ); // Show call results System.in.read(); } }

Six, result

You can also view the dubbo administration console

But look at the way 196 original articles published, 68 praised, 140000 visitors+ Private letter follow

2 February 2020, 04:47 | Views: 2703

Add new comment

For adding a comment, please log in
or create account

0 comments