zookeeper + dubbo 部署项目

  • Ali云docker部署zookeeper

  • 启动zookeeper 容器
    docker run -dit --name zookeeper --hostname zookeeper-host -v /usr/local/zookeeper
    /data:/data -v /usr/local/zookeeper/datalog:/datalog -p 2181:2181 --restart always zookeeper:latest

  • 本地dubbo服务

  • 工程结构
    在这里插入图片描述
    pom文件

<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.aotian</groupId>
	<artifactId>dubbo-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	
	<modules>
		<module>dubbo-provider</module>
		<module>dubbo-consumer</module>
		<module>dubbo-api</module>
	</modules>
	
	<dependencies>
		<!-- dubbo当前最新版 -->
		<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.0</version>
        </dependency>
        <!-- zookeeper客户端 -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
	</dependencies>
	
	<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
  • dubbo API在这里插入图片描述
    pom文件
<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>
  <parent>
    <groupId>com.aotian</groupId>
    <artifactId>dubbo-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>dubbo-api</artifactId>
</project>

DemoService接口

package com.aotian.demo.dubbo.api;

/**
 * 测试接口类
 * @author aotian
 *
 */
public interface DemoService {

	/**
	 * 测试方法,在用户名后添加一个字符串
	 * @param username	用户名
	 * @return	修改后的字符串
	 */
	String changeUsername(String username);
	
}

  • dubbo提供者
    在这里插入图片描述
    pom文件
<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>
	<parent>
		<groupId>com.aotian</groupId>
		<artifactId>dubbo-demo</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>dubbo-provider</artifactId>
	
	<dependencies>
		<dependency>
			<groupId>com.aotian</groupId>
			<artifactId>dubbo-api</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
</project>

DemoServiceImpl实现

package com.aotian.demo.dubbo.provider;

import com.aotian.demo.dubbo.api.DemoService;

/**
 * 测试服务端接口实现类
 * @author aotian
 *
 */
public class DemoServiceImpl implements DemoService{

	/**
	 * 测试方法,在用户名后添加一个字符串
	 * @param username	用户名
	 * @return	修改后的字符串
	 */
	@Override
	public String changeUsername(String username) {
		return username + " is changed";
	}
}

Provider服务提供者

package com.aotian.demo.dubbo.provider;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {

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

dubbo-provider.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://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">

    <!--dubbo应用程序命名-->
    <dubbo:application name="dubbo-demo-provider"/>

    <!--dubbo注册地址-->
    <dubbo:registry address="zookeeper://xxxx:2181"/>

    <!--dubbo协议地址-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--接口声明-->
    <dubbo:service interface="com.aotian.demo.dubbo.api.DemoService" ref="demoService"/>
    <bean id="demoService" class="com.aotian.demo.dubbo.provider.DemoServiceImpl"/>
</beans>
  • dubbo消费者
    在这里插入图片描述
    pom文件
<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>
	<parent>
		<groupId>com.aotian</groupId>
		<artifactId>dubbo-demo</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>dubbo-consumer</artifactId>
	
	<dependencies>
		<dependency>
			<groupId>com.aotian</groupId>
			<artifactId>dubbo-api</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
</project>

Consumer服务消费者

package com.aotian.demo.dubbo.consumer;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.aotian.demo.dubbo.api.DemoService;

public class Consumer {
	
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context 
			= new ClassPathXmlApplicationContext("classpath:dubbo-consumer.xml");
		context.start();
		
		String username = "sui";
		DemoService demoService = (DemoService) context.getBean("demoService");
		System.out.println(demoService.changeUsername(username));
	}
}

dubbo-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://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">

    <!--dubbo应用程序命名-->
    <dubbo:application name="dubbo-demo-provider"/>

    <!--dubbo注册地址-->
    <dubbo:registry address="zookeeper://xxxx:2181"/>

    <!--接口引用-->
    <dubbo:reference interface="com.aotian.demo.dubbo.api.DemoService" id="demoService"/>
</beans>
  • 链接调试

启动提供者服务
在这里插入图片描述
启动消费者服务
在这里插入图片描述

参考博客
https://www.jianshu.com/p/b492ef5d4b98

https://www.cnblogs.com/ASPNET2008/p/5622005.html

https://blog.csdn.net/qq_35241080/article/details/84963216

https://www.jianshu.com/p/5e3cbac93689

https://blog.csdn.net/xinluqishi123/article/details/64124503

47.106.234.142

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值