Docker+Dubbo+Zookeeper实现RPC远程调用

Docker+Dubbo+Zookeeper

1.安装Docker

1.1卸载旧版本的Docker

//如果Docker处于与运行状态  未运行可跳过
[root@MrADiao ~]# systemctl stop docker

//如果Docker设置了开机自启 需先删除 没有设置可自行跳过
[root@MrADiao ~]# systemctl diable docker
//卸载docker-ce
[root@MrADiao ~]# yum remove docker-ce

//删除注册表 文件夹
[root@MrADiao ~]# rm -rf /var/lib/docker

1.2.安装新版本的Docker

1、检查内核版本,必须是3.10及以上
[root@MrADiao ~]# uname -r

2.安装依赖包
[root@MrADiao ~]# yum install -y yum-utils device-mapper-persistent-data lvm2

3.添加docker软件包源
[root@MrADiao ~]#yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

4、安装docker
[root@MrADiao ~]# yum install docker-ce

5、输入y确认安装

6.查看是否安装成功
[root@MrADiao ~]# docker version
或者
[root@MrADiao ~]# docker -v

7、启动docker
[root@MrADiao ~]# systemctl start docker 

8.设置docker开机自启
[root@MrADiao ~]#systemctl enable docker

可参考Docker官方文档:https://docs.docker.com/install/linux/docker-ce/centos/

1.3.docker常用命令

  • 镜像操作命令

操作 命令 说明
检索 docker search 关键字 eg:docker search redis 我们经常去docker hub上检索镜像的详细信息,如镜像的TAG。
拉取 docker pull 镜像名:tag :tag是可选的,tag表示标签,多为软件的版本,默认是latest
列表 docker images 查看所有本地镜像
删除 docker rmi image-id 删除指定的本地镜像
  • 容器操作命令

操作 命令 说明
运行 docker run --name container-name -d image-nameeg:docker run –name myredis –d redis –name:自定义容器名
-d:后台运行
image-name:指定镜像模板
列表 docker ps 查看运行中的容; 加上-a;可以查看所有容器
停止 docker stop 容器Id 删除指定的容器
启动 docker start 容器id 启动容器
删除 docker rm 容器id 删除一个容器
端口映射 docker run -d -p 8888:8080 tomcat -d:后台运行
-p: 将主机的端口映射到容器的一个端口 主机端口:容器内部的端口
容器日志 docker logs container-name/container-id 查看容器的日志

2.Docker中安装Zookeeper

2.1.从Docker Hub中获取zookeeper镜像

[root@MrADiao ~]# docker pull zookeeper

2.2.启动zookeeper

[root@MrADiao ~]# docker run --name myzookeeper -p 2181:2181  --restart always -d zookeeper  //-p指定端口启动   :前的端口是linux虚拟机的端口  :后的端口是指docker容器的端口  2181:2181意思是指  把linux系统的2181端口映射到docker容器的2181端口

3.zookeeper+dubbo简单代码实现

3.1创建服务端项目(provider-ticket)

首先创建一个SpringBoot项目

3.1.1Pom.xml

<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.0modelVersion>

	<groupId>com.adiaogroupId>
	<artifactId>provider-ticketartifactId>
	<version>0.0.1-SNAPSHOTversion>
	<packaging>jarpackaging>

	<name>provider-ticketname>
	<description>Demo project for Spring Bootdescription>

	<parent>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-parentartifactId>
		<version>1.5.12.RELEASEversion>
		<relativePath/> 
	parent>

	<properties>
		<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
		<java.version>1.8java.version>
	properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-webartifactId>
		dependency>
		<dependency>
			<groupId>com.alibaba.bootgroupId>
			<artifactId>dubbo-spring-boot-starterartifactId>
			<version>0.1.0version>
		dependency>

		
		
		<dependency>
			<groupId>com.github.sgroschupfgroupId>
			<artifactId>zkclientartifactId>
			<version>0.1version>
		dependency>


		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-testartifactId>
			<scope>testscope>
		dependency>
	dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.bootgroupId>
				<artifactId>spring-boot-maven-pluginartifactId>
			plugin>
		plugins>
	build>
project>

3.1.2 application.properties
dubbo.application.name=provider-ticket

dubbo.registry.address=zookeeper://部署zookeeper服务器IP:2181

dubbo.scan.base-packages=com.adiao.ticket.service

server.port=8081
3.1.3创建一个TicketService
package com.adiao.ticket.service;

public interface TicketService {
     

    public String getTicket();
}

3.1.4实现TicketService
package com.adiao.ticket.service;

import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;


@Component
@Service 
public class TicketServiceImpl implements TicketService {
     
    @Override
    public String getTicket() {
     
        return "《厉害了,我的国》";
    }
}

注意:@Service注解是dubbo下的,作用是将服务发布出去

3.2创建调用方

3.2.1创建调用方项目(consumer-user)

首先创建一个SpringBoot项目

3.2.2 pom.xml

<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.0modelVersion>

	<groupId>com.adiaogroupId>
	<artifactId>consumer-userartifactId>
	<version>0.0.1-SNAPSHOTversion>
	<packaging>jarpackaging>

	<name>consumer-username>
	<description>Demo project for Spring Bootdescription>

	<parent>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-parentartifactId>
		<version>1.5.12.RELEASEversion>
		<relativePath/> 
	parent>

	<properties>
		<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
		<java.version>1.8java.version>
	properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-webartifactId>
		dependency>

		<dependency>
			<groupId>com.alibaba.bootgroupId>
			<artifactId>dubbo-spring-boot-starterartifactId>
			<version>0.1.0version>
		dependency>

		
		
		<dependency>
			<groupId>com.github.sgroschupfgroupId>
			<artifactId>zkclientartifactId>
			<version>0.1version>
		dependency>


		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-testartifactId>
			<scope>testscope>
		dependency>
	dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.bootgroupId>
				<artifactId>spring-boot-maven-pluginartifactId>
			plugin>
		plugins>
	build>


project>

3.2.3application.properties
dubbo.application.name=consumer-user

dubbo.registry.address=zookeeper://部署zookeeper服务器IP:2181

server.port=8082


3.2.3 创建与被调用放一样的Service
package com.adiao.ticket.service;

public interface TicketService {
     

    public String getTicket();
}
3.2.4创建一个调用方自己的Service
package com.adiao.user.service;


import com.alibaba.dubbo.config.annotation.Reference;
import com.adiao.ticket.service.TicketService;
import org.springframework.stereotype.Service;

@Service
public class UserService{
     

    @Reference//注入的是分布式中的远程服务对象
    TicketService ticketService;

    public void hello(){
     
        String ticket = ticketService.getTicket();
        System.out.println("买到票了:"+ticket);
    }
}

3.3测试

在main/test/java/下创建测试类,一定要确保被调用方 也就是provider-ticket处于运行状态

package com.adiao.user;

import com.adiao.user.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ConsumerUserApplicationTests {
     

	@Autowired
	UserService userService;

	@Test
	public void contextLoads() {
     

		userService.hello();
	}

}

你可能感兴趣的:(dubbo,Docker,java)