Build Dubbo framework service in Linux environment

An introduction to building Dubbo framework services in Linux
environment The WAR package
dubbo-admin of JDK, zookeeper, and dubbo-admin needs to be prepared in advance, and dubbo-admin is not introduced here. It was introduced in my previous article. The
article address: http://kuailenanhaier .iteye.com/blog/2329575
Install zookeeper
1. Download from zookeeper official website, download address: http://zookeeper.apache.org/releases.html
2. Unzip and execute the command: tar -zxvf zookeeper-3.4.8.tar. gz zookeeper-3.4.8
3. Go to the conf directory, copy zoo_sample.cfg, and rename it to zoo.cfg, execute the command cp zoo_sample.cfg zoo.cfg
4. Edit zoo.cfg, and modify dataDir to
dataDir =/home/test/zookeeper/data   
dataLogDir=/home/test/zookeeper/logs

Start zookeeper
1. Enter the bin directory of zookeeper and execute the command ./zkServer.sh start
2. In the bin directory, execute the command ./zkCli .sh -server server IP: 2181, this is to test whether to start zookeeper, if zkClient is already connected, it means that zookeeper has been started to


close zookeeper
1. Enter the bin directory of zookeeper and execute the command ./zkServer.sh stop to

start dubbo-admin under tomcat to

create a dubbo-demo-provider producer
1. Create a maven project, the project name is dubbo-demo-provider
2. Modify the pom file and add the JAR package as follows:
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency >
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
<build>
<finalName>dubbo-demo-provider</finalName>
</build>

3. Create the interface class IDemoService, the code is as follows:
package com.cn.dubbo.provider;

public interface IDemoService {
public String sayHello(String name);
}

4. Create an interface implementation class DemoServiceImpl to implement the methods in the interface. The code is as follows:
package com.cn.dubbo.provider;

public class DemoServiceImpl implements IDemoService{

public String sayHello(String name)
{
String result = "Hello "+ name+" provider sayHello method success";
System.out.println("provider sayHello method invoke");
return result;
}
}


5. Create the dubbo-demo-provider.xml configuration file, configure the producer to expose the service address and registry, as follows
<!-- provider application information for dependencies -->
<dubbo:application name="dubbo-demo-provider"/>

<!-- Use zookeeper registry to expose provider service address -->
<dubbo:registry address="zookeeper://192.168.0.29:2181"/ >

<!-- Expose service on port 8001 using dubbo protocol -->
<dubbo:protocol name="dubbo" port="8001" />

<!-- Provider service implementation -->
<bean id="demoService" class="com.cn.dubbo.provider.DemoServiceImpl" />

<!-- Declare the service interface to be exposed-->
<dubbo:service interface="com.cn.dubbo.provider.IDemoService" ref="demoService" />

6. Create a TEST class to start the producer and load the configuration file. The code is as follows:
package com.cn.dubbo.test;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ProviderTest {

public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:dubbo-demo-provider.xml");
context.start();
System.in.read();
}
}

create dubbo -demo-consumer consumer
1. Create a maven project, the project name is dubbo-demo-consumer
2. Modify the pom file, here you need to rely on dubbo-demo-provider, the code is as follows:
<dependencies>
<dependency>
<groupId>com.cn .dubbo</groupId>
<artifactId>dubbo-demo-provider</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
<dependency>
<groupId>com.github .sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
<build>
<finalName>dubbo-demo-consumer</finalName>
</build>

3 , Create dubbo-demo-consumer.xml to configure the services exposed by consumers and producers, the code is as follows:
<!-- provider application information, used for dependencies-->
<dubbo:application name="dubbo- demo-consumer"/>

<!-- Use the zookeeper registry to expose the provider service address -->
<dubbo:registry address="zookeeper://192.168.0.29:2181"/>

<dubbo:reference id="demoService" interface="com.cn.dubbo.provider.IDemoService" />

4、创建TEST类,用于调用生产者服务,代码如下:
package com.cn.dubbo.test;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cn.dubbo.provider.IDemoService;

public class ConsumerTest {

public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:dubbo-demo-consumer.xml");
context.start();
IDemoService providerImpl = (IDemoService)context.getBean("demoService");
String result = providerImpl.sayHello("bb");
System.out.println(result);
System.in.read();
}
}


Finally, log in to the dubbo-admin console to check the producer and consumer, and whether the producer and consumer are successfully released

Note : The startup steps here are described below:
1. Start zookeeper first
2. Start dubbo-admin
3. Start production
4. Start the consumer, which is dubbo-demo-consumer The

above briefly introduces how to build the dubbo service framework in the Linux environment, and the installation, startup, and deployment of zookeeper hope that it will be helpful to everyone

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326962535&siteId=291194637
Recommended