東川印記

一本東川,笑看爭龍斗虎;寰茫兦者,度橫佰昧人生。

简单学习微服务之dubbo

2022年6月15日星期三



简单学习一下微服务

集群:堆服务器解决同一个问题,物理层面。

分布式:一个复杂问题拆分成若干小问题,小问题结果整合实现大需求,软件设计层面。

现在流行的微服务框架有 dubbo、spring cloud、grpc。

简单学习一下dubbo。

官网 dubbo.apache.org/zh/

核心为 RPC通信 和 微服务治理。

RPC:远程过程调用,Remote Process Call。

服务治理:服务提供者、服务消费者、注册中心。

微服务将自己信息存储到注册中心叫服务注册,服务消费者从注册中心获取服务提供者信息,叫服务发现。

配图 dobbo_architecture.png

特么的梯子挂了,连GitHub都打不开了。。。。

新建一个SpringBoot Web项目。

配图1

特么的网络有问题建个项目都各种莫名其妙的问题。。。。。

https://start.spring.io connect time out,用 https://start.aliyun.com 替换

哎,真是特色bug了。。。。还是改回start.spring.io吧,阿里的跟spring.io不太一样。。。。

写代码1个小时,解决网络问题五个小时。。。。

代码结构与TestRMI基本一致,区别就是 TestRMI需要自定义RMIConfig,Dubbo使用现成的服务中心。

api: 与TestRMI一致。

provider:服务提供者;

consumer:消费者。

修改provider的Service impl @Service注解为dubbo。

provider application.yml

server:
  port: 8081

# dubbo config
dubbo:
  application:
    name: provider
  registry:
    #注册中心地址
    #    address: multicast://224.5.6.7:1234
    # zookeeper 注册中心地址
    address: zookeeper://172.16.5.131:2181
    timeout: 6000
  #元数据中心地址
  metadata-report:
    address: zookeeper://172.16.5.131:2181

  protocol:
    #协议名称
    name: dubbo
    port: 20880
  scan:
    base-packages: dc.test.provider.service

修改consumer的IUserService注入为dubbo的 @Reference

consumer的application.yml

server:
  port: 8082

#dubbo config
dubbo:
  application:
    name: consumer
  registry:
    #消费者从哪个地址找服务
#    address: multicast://224.5.6.7:1234
    address: zookeeper://172.16.5.131:2181


启动运行即可,dubbo代码端的简单集成OK。


zookeeper single 部署

[senrsl@localhost ~]$ wget https://dlcdn.apache.org/zookeeper/zookeeper-3.8.0/apache-zookeeper-3.8.0-bin.tar.gz

[senrsl@localhost ~]$ mkdir zookeep_single

[senrsl@localhost ~]$ tar zxvf apache-zookeeper-3.8.0-bin.tar.gz -C zookeep_single/

[senrsl@localhost ~]$ cp zoo_sample.cfg  zoo.cfg

[senrsl@localhost ~]$ cat zookeep_single/apache-zookeeper-3.8.0-bin/conf/zoo.cfg
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
#dataDir=/tmp/zookeeper
dataDir=/home/senrsl/zookeep_single/data
logDir=/home/senrsl/zookeep_single/log
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# https://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpHost=0.0.0.0
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true

[senrsl@localhost ~]$

安装java

[senrsl@localhost ~]$ yum search jdk

[senrsl@localhost ~]$ yum install -y java-1.8.0-openjdk.x86_64

启动zookeeper

[senrsl@localhost bin]$ ./zkServer.sh start
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /home/senrsl/zookeep_single/apache-zookeeper-3.8.0-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[senrsl@localhost bin]$ ./zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /home/senrsl/zookeep_single/apache-zookeeper-3.8.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: standalone
[senrsl@localhost bin]$

关闭centos 防火墙

[senrsl@localhost ~]$ systemctl stop firewalld.service

[senrsl@localhost ~]$ systemctl status firewalld.service


然后将provider和consumer服务中心地址注册到zookeeper地址即可。


zookeeper集群

#解压复制三份,123,然后配置data和log配置123

[senrsl@localhost ~]$ ls zookeep_multi/
data1  data2  data3  zookeeper1  zookeeper2  zookeeper3

#每份在data下配置myid,为123

[senrsl@localhost ~]$ cat zookeep_multi/data1/myid
1
[senrsl@localhost ~]$ cat zookeep_multi/zookeeper1/conf/zoo.cfg
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
#dataDir=/tmp/zookeeper
dataDir=/home/senrsl/zookeep_multi/data1
logDir=/home/senrsl/zookeep_multi/log1
# the port at which the clients will connect
clientPort=2181 //分别为2181、2182、2183
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# https://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpHost=0.0.0.0
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true

# server.1中1是data/myid中内容,2888 2889用于集群内部通信,3888,3889用于选择leader
server.1=172.16.5.131:2888:3888
server.2=172.16.5.131:2889:3889
server.3=172.16.5.131:2890:3890
[senrsl@localhost ~]$

依次启动123

检查状态

senrsl@localhost ~]$ ./zookeep_multi/zookeeper1/bin/zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /home/senrsl/zookeep_multi/zookeeper1/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
[senrsl@localhost ~]$ ./zookeep_multi/zookeeper2/bin/zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /home/senrsl/zookeep_multi/zookeeper2/bin/../conf/zoo.cfg
Client port found: 2182. Client address: localhost. Client SSL: false.
Mode: leader
[senrsl@localhost ~]$ ./zookeep_multi/zookeeper3/bin/zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /home/senrsl/zookeep_multi/zookeeper3/bin/../conf/zoo.cfg
Client port found: 2183. Client address: localhost. Client SSL: false.
Mode: follower
[senrsl@localhost ~]$

然后修改代码中注册中心地址即可。


监控中心 dubbo-admin

直接走docker,省事

SENRSL:docker senrsl$ docker pull apache/dubbo-admin

docker run -d \
--name dubbo-admin \
-v /Users/senrsl/test/docker/dubbo:/data \
-p 8100:8080 \
-e admin.registry.address=zookeeper://172.16.5.131:2181 \
-e admin.config-center=zookeeper://172.16.5.131:2181 \
-e admin.metadata-report.address=zookeeper://172.16.5.131:2181 \
--restart=always \
docker.io/apache/dubbo-admin;

启动 http://localhost:8100/ 即可。


--
senRsl
2022年06月09日15:23:43

没有评论 :

发表评论