图片

到dubbo上场了,该迁移dubbo咯,开始。

图片

迁移流程

老规矩,老图上场。

图片

基础镜像

也是基于java环境的,那真是又巧了哈,我们看下迁移定时任务那一篇,就可以了,这里就不水文了。

服务本身

1、软件下载

我发现这个不需要下载,因为本身不需要了解这些,我在这里只是为了有个清晰的认知的。你们操作的时候肯定是根据自己的实际业务中的dubbo环境进行的,当然了,感兴趣的同学可以私我哈,就不放下载链接了。

2、服务认识

[root@node1 ~]# mkdir -pv ceshi/dubbo
mkdir: 已创建目录 "ceshi/dubbo"
[root@node1 ~]# cd ceshi/dubbo/

[root@node1 dubbo]# unzip dubbo-demo.zip
[root@node1 dubbo]# cd dubbo-demo
[root@node1 dubbo-demo]#  vim src/main/java/com/mooc/demo/service/DemoServiceImpl.java 
# 可以看到里面定义了一个DemoService,sayHello里面就简单的回复了一个内容
package com.mooc.demo.service;

import com.mooc.demo.api.DemoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Michael on 2018/9/25.
 */
public class DemoServiceImpl implements DemoService {

    private static final Logger log = LoggerFactory.getLogger(DemoServiceImpl.class);

    public String sayHello(String name) {

        log.debug("dubbo say hello to : {}", name);

        return "Hello "+name;
    }

}
[root@node1 dubbo-demo]#  
# 申明了接口的名字
    <!-- service implementation, as same as regular local bean -->
    <bean id="demoService" class="com.yunweijia.demo.service.DemoServiceImpl"/>

    <!-- declare the service interface to be exported -->
    <dubbo:service interface="com.yunweijia.demo.api.DemoService" ref="demoService"/>
[root@node1 dubbo-demo]# vim src/main/assembly/assembly.xml 
# 申明了打包的格式
    <formats>
        <format>tar.gz</format>
    </formats>
[root@node1 dubbo-demo]# vim src/main/resources/dubbo.properties 
# 申明了一些基础配置,在这里会使用一个zookeeper服务,随便找个服务器搭建下
# 这里就不写如何搭建了哈,这都不是重点,重点是如何迁移
dubbo.application.name=demo
dubbo.registry.address=zookeeper://10.1.2.31:2181
dubbo.spring.config=classpath*:spring/provider.xml
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
[root@node1 dubbo-demo]# 
[root@node1 dubbo-demo]# ls src/main/bin/
# 这里放了两个脚本,一个是启动脚本,一个是停止脚本
start.sh  stop.sh
[root@node1 dubbo-demo]#

3、构建镜像:

# 必须要走这一步才可以打包,但是这个原理咱们不用管
# 因为打包这个步骤是研发操作的,咱们这里只是做示例
[root@node1 dubbo-demo]# cd ../dubbo-demo-api/
[root@node1 dubbo-demo-api]# ls
pom.xml  src
[root@node1 dubbo-demo-api]# mvn install
# 出现如下字样就可以了
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.516 s
[INFO] Finished at: 2022-03-30T17:14:47+08:00
[INFO] ------------------------------------------------------------------------
[root@node1 dubbo-demo-api]#
[root@node1 dubbo-demo-api]# cd ../dubbo-demo
[root@node1 dubbo-demo]# mvn package
# 出现如下字样就可以了

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:10 min
[INFO] Finished at: 2022-03-30T17:16:34+08:00
[INFO] ------------------------------------------------------------------------
[root@node1 dubbo-demo]# 

4、测试下打包的程序是否可以运行:

[root@node1 dubbo-demo]# cd target/
[root@node1 target]# mkdir ROOT
[root@node1 target]# tar xf dubbo-demo-1.0-SNAPSHOT-assembly.tar.gz -C ROOT/
[root@node1 target]# cd ROOT/
[root@node1 ROOT]# ls
bin  conf  lib
[root@node1 ROOT]# ./bin/start.sh 
Starting the demo ...[2022-03-30 17:37:52] Dubbo service server started!
# 可以看到服务正常启动
# 简单测试下,在node2上进行以下操作
[root@node2 boot]# telnet 192.168.112.130 20880
Trying 192.168.112.130...
Connected to 192.168.112.130.
Escape character is '^]'.

dubbo>ls
com.mooc.demo.api.DemoService
dubbo>ls com.mooc.demo.api.DemoService
sayHello
dubbo>invoke com.mooc.demo.api.DemoService.sayHello("yunweijia")
"Hello yunweijia"
elapsed: 0 ms.
dubbo>exit
Connection closed by foreign host.
[root@node2 boot]#

经过以上操作表示,程序本身无异常。

构建镜像

[root@node1 dubbo-demo]# pwd
/root/mooc-k8s-demo/dubbo-demo
[root@node1 dubbo-demo]# vim Dockerfile
FROM registry.cn-beijing.aliyuncs.com/yunweijia0909/openjdk:8-jre
COPY target/ROOT /ROOT
ENTRYPOINT ['sh', '/ROOT/bin/start.sh']
[root@node1 dubbo-demo]#
[root@node1 dubbo-demo]# cd ..
[root@node1 mooc-k8s-demo]# tar zcf dubbo-demo.tar.gz dubbo-demo
# 把以上目录打个包,传输到一台有docker环境的服务器上,进行如下操作
[root@jier ~]# tar xf dubbo-demo.tar.gz 
[root@jier ~]# cd dubbo-demo/
[root@jier dubbo-demo]# docker build -t dubbo-demo:v1 .
Sending build context to Docker daemon  25.78MB
Step 1/3 : FROM registry.cn-beijing.aliyuncs.com/yunweijia0909/openjdk:8-jre
 ---> 71d97cb5644a
Step 2/3 : COPY target/ROOT /ROOT
 ---> 795d13d9d449
Step 3/3 : ENTRYPOINT ['sh', '/ROOT/bin/start.sh']
 ---> Running in 8c0fcbb46aff
Removing intermediate container 8c0fcbb46aff
 ---> 9f6a10e9dc3a
Successfully built 9f6a10e9dc3a
Successfully tagged dubbo-demo:v1
[root@jier dubbo-demo]# 
[root@jier dubbo-demo]# docker tag dubbo-demo:v1 registry.cn-beijing.aliyuncs.com/yunweijia0909/dubbo-demo:v1
[root@jier dubbo-demo]# docker push registry.cn-beijing.aliyuncs.com/yunweijia0909/dubbo-demo:v1
The push refers to repository [registry.cn-beijing.aliyuncs.com/yunweijia0909/dubbo-demo]
9bd8786c2b0a: Pushed 
6708e662f7d4: Mounted from yunweijia0909/springboot-web 
2543359fb19b: Mounted from yunweijia0909/springboot-web 
200bb8035fe4: Mounted from yunweijia0909/springboot-web 
6e632f416458: Mounted from yunweijia0909/springboot-web 
e019be289189: Mounted from yunweijia0909/springboot-web 
c9a63110150b: Mounted from yunweijia0909/springboot-web 
v1: digest: sha256:f8e8f275b113dcf8e36d953b6ff1985240647c7e95aba1beb23e99d6f2490157 size: 1794
[root@jier dubbo-demo]# 

服务发现策略
剩余内容请转至VX公众号 “运维家” ,回复 “132” 查看。

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐