5-4 基于Zookeeper案例实现微服务动态注册和发现案例

发布于:2022-12-22 ⋅ 阅读:(589) ⋅ 点赞:(0)

前言

ZooKeeper主要服务于分布式系统,可以用ZooKeeper来做:统一配置管理、统一命名服务、分布式锁、集群管理。在Dubbo框架中做服务的注册中心。

Dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。



zk注册中心

注册中心的部署可参考 4-6 k8s构建zookeeper集群

在dubbo provider和dubbo consumer两个目录修改配置文件,注册中心填3个。

vim conf/dubbo.properties
dubbo.registry.address=zookeeper://zookeeper1.myserver.svc.cluster.local:2181 | zookeeper://zookeeper2.myserver.svc.cluster.local:2181 | zookeeper://zookeeper3.myserver.svc.cluster.local:2181

准备镜像

provider镜像

# Dubbo provider
FROM easzlab.io.local:5000/myhub/jdk-base:v8.212 

RUN yum install file nc -y
RUN mkdir -p /apps/dubbo/provider
ADD dubbo-demo-provider-2.1.5/  /apps/dubbo/provider
ADD run_java.sh /apps/dubbo/provider/bin 
RUN useradd nginx && chown -R nginx.nginx /apps
RUN chmod a+x /apps/dubbo/provider/bin/*.sh

CMD ["/apps/dubbo/provider/bin/run_java.sh"]
nerdctl build -t easzlab.io.local:5000/myhub/dubbo-demo-provider:v1  .
nerdctl push --insecure-registry easzlab.io.local:5000/myhub/dubbo-demo-provider:v1

consumer镜像

# Dubbo consumer
FROM easzlab.io.local:5000/myhub/jdk-base:v8.212 

RUN yum install file -y
RUN mkdir -p /apps/dubbo/consumer 
ADD dubbo-demo-consumer-2.1.5  /apps/dubbo/consumer
ADD run_java.sh /apps/dubbo/consumer/bin 
RUN useradd nginx && chown -R nginx.nginx /apps
RUN chmod a+x /apps/dubbo/consumer/bin/*.sh

CMD ["/apps/dubbo/consumer/bin/run_java.sh"]
nerdctl build -t easzlab.io.local:5000/myhub/dubbo-demo-consumer:v1  .
nerdctl push --insecure-registry easzlab.io.local:5000/myhub/dubbo-demo-consumer:v1

dubbo-admin镜像

先修改dubbo-admin配置文件,包括zk地址,账号密码。

vim dubboadmin/WEB-INF/dubbo.properties

dubbo.registry.address=zookeeper://zookeeper1.myserver.svc.cluster.local:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
# Dubbo dubboadmin
FROM easzlab.io.local:5000/myhub/tomcat-base:v8.5.43

RUN yum install unzip -y  
ADD server.xml /apps/tomcat/conf/server.xml
ADD logging.properties /apps/tomcat/conf/logging.properties
ADD catalina.sh /apps/tomcat/bin/catalina.sh
ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
ADD dubboadmin.war  /data/tomcat/webapps/dubboadmin.war
RUN cd /data/tomcat/webapps && unzip dubboadmin.war && rm -rf dubboadmin.war && useradd nginx && chown -R nginx.nginx /data /apps
RUN chmod +x /apps/tomcat/bin/catalina.sh
RUN chmod +x /apps/tomcat/bin/run_tomcat.sh

EXPOSE 8080 8443

CMD ["/apps/tomcat/bin/run_tomcat.sh"]
nerdctl build -t easzlab.io.local:5000/myhub/dubboadmin:v1  .
nerdctl push --insecure-registry easzlab.io.local:5000/myhub/dubboadmin:v1

yaml部署

provider部署

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    app: myserver-provider
  name: myserver-provider-deployment
  namespace: myserver
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myserver-provider
  template:
    metadata:
      labels:
        app: myserver-provider
    spec:
      containers:
      - name: myserver-provider-container
        image: easzlab.io.local:5000/myhub/dubbo-demo-provider:v1
        imagePullPolicy: Always
        ports:
        - containerPort: 20880
          protocol: TCP
          name: http

---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: myserver-provider
  name: myserver-provider-spec
  namespace: myserver
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 20880
  selector:
    app: myserver-provider

consumer部署

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    app: myserver-consumer
  name: myserver-consumer-deployment
  namespace: myserver
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myserver-consumer
  template:
    metadata:
      labels:
        app: myserver-consumer
    spec:
      containers:
      - name: myserver-consumer-container
        image: easzlab.io.local:5000/myhub/dubbo-demo-consumer:v1 
        imagePullPolicy: Always
        ports:
        - containerPort: 80
          protocol: TCP
          name: http

---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: myserver-consumer
  name: myserver-consumer-server
  namespace: myserver
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: myserver-consumer

dubbo-admin部署

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    app: myserver-dubboadmin
  name: myserver-dubboadmin-deployment
  namespace: myserver
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myserver-dubboadmin
  template:
    metadata:
      labels:
        app: myserver-dubboadmin
    spec:
      containers:
      - name: myserver-dubboadmin-container
        image: easzlab.io.local:5000/myhub/dubboadmin:v1
        imagePullPolicy: Always
        ports:
        - containerPort: 8080
          protocol: TCP
          name: http

---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: myserver-dubboadmin
  name: myserver-dubboadmin-service
  namespace: myserver
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 8080
    nodePort: 30080
  selector:
    app: myserver-dubboadmin

验证服务


在ZooInspector查看注册中心IP个数


在dubboadmin查看生产者与消费者使用情况


本文含有隐藏内容,请 开通VIP 后查看