栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

SpringCloud+ZooKeeper(注册与发现)

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringCloud+ZooKeeper(注册与发现)

目录
  • 一、Linux配置ZooKeeper服务
    • ZooKeeper单点配置
      • 1.下载ZooKeeper:
      • 2.配置ZooKeeper:
      • 3.配置环境变量
      • 4.启动Zookeeper
    • ZooKeeper集群配置
      • 1.修改zoo.cfg配置文件
      • 2.新建myid文件
      • 3.重启三个zookeeper服务
  • 二、创建ZooKeepre客户端应用
      • 1.提供者
      • 2.消费者
  • 三、测试
      • 1.启动项目
      • 2.访问接口

一、Linux配置ZooKeeper服务 ZooKeeper单点配置 1.下载ZooKeeper:

ZooKeeper官网下载:http://mirror.bit.edu.cn/apache/zookeeper/

$cd /usr/local
$mkdir zookeeper
$cd zookeeper
$wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.5.5/apache-zookeeper-3.5.5-bin.tar.gz
$tar -zxvf apache-zookeeper-3.5.5-bin.tar.gz
2.配置ZooKeeper:
$cd apache-zookeeper-3.5.5-bin/conf
$ls


复制一份zoo_sample.cfg文件命名为zoo.cfg并修改内容如下。

cp zoo_sample.cfg zoo.cfg

3.配置环境变量
$vi /etc/profile

在文件最后加上如下配置

export ZOOKEEPER=/usr/local/zookeeper/apache-zookeeper-3.5.5-bin
export PATH=$PATH:$ZOOKEEPER/bin


然后执行如下命令使环境生效

$source /etc/profile
4.启动Zookeeper

启动服务器端

zkServer.sh start //启动
zkServer.sh status  //查看状态

启动客户端

zkCli.sh   //启动客户端

ZooKeeper集群配置 1.修改zoo.cfg配置文件

在原来的服务器和新增的两台服务器的zoo.cfg配置文件后面加上如下配置(记住本机的ip用0.0.0.0代替如图)

server.1=123.207.231.159:2888:3888
server.2=120.77.222.219:2888:3888
server.3=47.106.128.4:2888:3888

123.207.231.159服务器配置:

120.77.222.219服务器配置:

最后一台服务器同理。
2181端口:服务连接zookeeper时使用
2888端口:在zookeeper服务器之间及和leader连接时使用,
3888端口:在进行leader选举时使用

2.新建myid文件

在/tmp/zookeeper/下新建myid文件

$cd /tmp/zookeeper/
$vi myid

如第二台服务120.77.222.219直接输入的 2。

在123.207.231.159的myid下输入1,120.77.222.219输入2,47.106.128.4输入3。与zoo.cfg配置文件是对应的。

3.重启三个zookeeper服务
zkServer.sh restart //重启

我第一个启动的第二台120.77.222.219服务器。


二、创建ZooKeepre客户端应用

GitHub项目地址:https://github.com/ZiXinZhu/zookeeper

1.提供者
  • 添加maven依赖

       
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.0
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
        
            org.springframework.cloud
            spring-cloud-starter-zookeeper-discovery
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            org.apache.zookeeper
            zookeeper
            3.5.3-beta
            
                
                    org.slf4j
                    slf4j-log4j12
                
                
                    log4j
                    log4j
                
            
        
    
  • 配置application.properties配置文件

    server.port=8762
    spring.application.name=zk-provider
    spring.cloud.zookeeper.connect-string=120.77.222.219:2181
    spring.cloud.zookeeper.discovery.instance-id=1
    spring.cloud.zookeeper.discovery.enabled=true
    spring.cloud.zookeeper.discovery.register=true
    spring.cloud.zookeeper.discovery.root=/server
    
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://123.207.123.159:3306/hibernate?characterEncoding=utf8&useSSL=false&zeroDateTimeBehavior=convertToNull&autoReconnect=true
    spring.datasource.username=root
    spring.datasource.password=password
    mybatis.configuration.map-underscore-to-camel-case=true
    
    spring.datasource.hikari.minimum-idle=5
    spring.datasource.hikari.maximum-pool-size=15
    spring.datasource.hikari.auto-commit=true
    spring.datasource.hikari.idle-timeout=30000
    spring.datasource.hikari.pool-name=DatebookHikariCP
    spring.datasource.hikari.max-lifetime=1800000
    spring.datasource.hikari.connection-timeout=30000
    spring.datasource.hikari.connection-test-query=SELECt 1
    

    参数的简单说明:
    connect-string:连接zk服务的套接字
    register: 是否启动服务注册
    instance-id: zk唯一id的标识
    root: zk根节点名称,默认/services

  • 启动类添加注解

    @SpringBootApplication
    @EnableDiscoveryClient
    public class ProviderApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ProviderApplication.class, args);
        }
    }
    
  • DAO层UserDao

     import com.zzx.provider.po.UserPO;
     import org.apache.ibatis.annotations.Mapper;
     import org.apache.ibatis.annotations.Param;
     import org.apache.ibatis.annotations.Select;
     import org.springframework.stereotype.Repository;
     
     @Mapper
     @Repository
     public interface UserDao {
     
         @Select("SELECT * from girl where id=#{id}")
         UserPO findById(@Param("id")int id);
     }
    
  • 服务层UserServer

     import com.zzx.provider.dao.UserDao;
     import com.zzx.provider.po.UserPO;
     import org.springframework.beans.factory.annotation.Autowired;
     import org.springframework.stereotype.Service;
     
     @Service
     public class UserServer {
     
         @Autowired
         UserDao userDao;
         public UserPO findById(){
             return userDao.findById(1);
         }
     }
    
  • 控制层UserController

     import com.zzx.provider.po.UserPO;
     import com.zzx.provider.server.UserServer;
     import org.springframework.beans.factory.annotation.Autowired;
     import org.springframework.web.bind.annotation.GetMapping;
     import org.springframework.web.bind.annotation.RestController;
     import java.util.ArrayList;
     import java.util.List;
     
     @RestController
     public class UserController {
     
         @Autowired
         UserServer userServer;
     
         @GetMapping("/find")
         public UserPO findById(){
             return userServer.findById();
         }
         
         @GetMapping("/list")
         public List getList() {
             return new ArrayList() {
                 {
                     add("add");
                 }
             };
         }
     }
    
2.消费者
  • 添加maven依赖
     	
            
            org.springframework.boot
            spring-boot-starter-web
        
    
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.cloud
            spring-cloud-starter-zookeeper-discovery
        
        
            org.springframework.cloud
            spring-cloud-starter-feign
            1.4.6.RELEASE
        
        
            org.apache.zookeeper
            zookeeper
            3.5.3-beta
            
                
                    org.slf4j
                    slf4j-log4j12
                
                
                    log4j
                    log4j
                
            
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
  • 配置application.properties配置文件
    server.port=8763
    spring.application.name=zk-consumer
    
    spring.cloud.zookeeper.connect-string=120.77.222.219:2181
    spring.cloud.zookeeper.discovery.instance-id=1
    spring.cloud.zookeeper.discovery.enabled=true
    spring.cloud.zookeeper.discovery.register=true
    spring.cloud.zookeeper.discovery.root=/server
    
  • 启动类添加注解
     import org.springframework.boot.SpringApplication;
     import org.springframework.boot.autoconfigure.SpringBootApplication;
     import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
     
     @SpringBootApplication
     @EnableDiscoveryClient
     public class ProviderApplication {
     
         public static void main(String[] args) {
             SpringApplication.run(ProviderApplication.class, args);
         }
     
     }
    
  • 服务器层IUserServer
     import org.springframework.cloud.openfeign.FeignClient;
     import org.springframework.stereotype.Service;
     import org.springframework.web.bind.annotation.GetMapping;
     import java.util.List;
     
     @Service
     @FeignClient("zk-provider")
     public interface IUserServer {
     
     
         @GetMapping("/find")
         String findById() ;
     
         @GetMapping("/list")
         List getList();
     }
    
  • 控制层UserController
     import com.zzx.consumer.server.IUserServer;
     import org.springframework.beans.factory.annotation.Autowired;
     import org.springframework.web.bind.annotation.GetMapping;
     import org.springframework.web.bind.annotation.RestController;
     import java.util.List;
     
     @RestController
     public class UserController {
     
     
         @Autowired
         protected IUserServer server;
     
         @GetMapping("/get")
         public String getUser() {
             return server.findById();
         }
     
         @GetMapping("/add")
         public List getList() {
             return server.getList();
         }
     }
    
三、测试 1.启动项目

分别启动提供者和消费者,然后打开ZooInspector界面管理工具如下

2.访问接口



GitHub项目地址:https://github.com/ZiXinZhu/zookeeper

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/268644.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号