云社区 博客 博客详情

springboot集成zookeeper注册中心

刀法第一人 发表于 2020-04-16 19:39:58 2020-04-16
0
0

【摘要】 【推荐阅读】微服务还能火多久?>>>ZooKeeper是一个分布式服务框架,是ApacheHadoop的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务、状态同步服务、集群管理、分布式应用配置项的管理等ZooKeeper是一个树形结构的目录服务,支持变更推送在ZooKeeper中,节点分为两类:  机器节点:    指构成集群的机器  数据节...

【推荐阅读】微服务还能火多久?>>>

ZooKeeper是一个分布式服务框架,是Apache Hadoop 的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务、状态同步服务、集群管理、分布式应用配置项的管理等

ZooKeeper是一个树形结构的目录服务,支持变更推送

在ZooKeeper中,节点分为两类:

  机器节点:

    指构成集群的机器

  数据节点ZNode:

    指数据模型中的数据单元  

    ZooKeeper将所有数据存储在内存中,数据模型是一棵树(ZNode Tree),由斜杠(/)进行分割的路径,就是一个ZNode,例如/services/customer

    每个ZNode上都会保存自己的数据内容,同时还会保存一系列属性信息

    Znode可分为:

      持久节点:指一旦这个ZNode被创建了,除非主动进行ZNode的移除操作,否则这个ZNode将一直保存在ZooKeeper上

      临时节点:它的生命周期和客户端会话绑定,一旦客户端会话失效,那么这个客户端创建的所有临时节点都会被移除

1.docker安装

docker pull zookeeper:3.5

2.启动

docker run --name zookeeper -p 2181:2181 -d zookeeper:3.5

3.使用

provider

依赖

<properties>
        <java.version>1.8java.version>
        <spring-cloud.version>Greenwich.SR3spring-cloud.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-zookeeper-discoveryartifactId>
        dependency>
    dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

配置

server.port=8010
spring.application.name=service-provider
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
spring.cloud.zookeeper.connect-string=192.168.99.100:2181

启动类

package com.xyz.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class ProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }

}

控制器

package com.xyz.provider.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class demoController {
    @RequestMapping("/hello")
    public String Hello() {
        return "hello,provider";
    }

}

comsumer

添加依赖

<properties>
        <java.version>1.8java.version>
        <spring-cloud.version>Hoxton.SR1spring-cloud.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-zookeeper-discoveryartifactId>
        dependency>
    dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

配置

server.port=8015
spring.application.name=service-comsumer
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
feign.client.config.default.connect-timeout=500
feign.client.config.default.read-timeout=500
spring.cloud.zookeeper.connect-string=192.168.99.100:2181

启动类

package com.xyz.comsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableFeignClients
@SpringBootApplication
public class ComsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ComsumerApplication.class, args);
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

控制器

package com.xyz.comsumer.controller;

import com.xyz.comsumer.feign.RemoteHelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FeignController {
    @Autowired
    RemoteHelloService remoteHelloService;

    @RequestMapping("feignTest")
    public String feignTest() {
        String result = remoteHelloService.hello();
        if (result == null) {
            result = "出错";
        }
        return result;
    }
    
}

启动provider

启动comsumer

测试 GET http://127.0.0.1:8015/feignTest

输出

  hello,provider

4.查看

在docker中进入zookeeper命令行

docker exec -it zookeeper bash

进入zookeeper的bin目录

cd bin

使用zookeeper client访问zookeeper服务

./zkCli.sh

查看当前 ZooKeeper 中所包含的内容

[zk: localhost:2181(CONNECTED) 1] ls /
[services, zookeeper]

查看所有services

[zk: localhost:2181(CONNECTED) 2] ls /services
[service-comsumer, service-provider]

查看service-comsumer的信息

[zk: localhost:2181(CONNECTED) 13] ls /services/service-comsumer
[efac86a0-1d0c-47e8-8f29-998661f50e04]

根据[id]查看相关服务的详细信息

[zk: localhost:2181(CONNECTED) 23] ls /services/service-comsumer
[f8c462b5-a6e6-4861-b20d-e48cdcdda207]
[zk: localhost:2181(CONNECTED) 24] get /services/service-comsumer/f8c462b5-a6e6-4861-b20d-e48cdcdda207
{"name":"service-comsumer","id":"f8c462b5-a6e6-4861-b20d-e48cdcdda207","address":"hkgi-PC","port":8015,"sslPort":null,"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"service-comsumer-1","name":"service-comsumer","metadata":{}},"registrationTimeUTC":1586917597776,"serviceType"
:"DYNAMIC","uriSpec":{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}}

 

文章来源: www.oschina.net,作者:osc_g91p39eg,版权归原作者所有,如需转载,请联系作者。

原文链接:https://my.oschina.net/u/4373790/blog/3236874

登录后可下载附件,请登录或者注册

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:huaweicloud.bbs@huawei.com进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
评论文章 //点赞 收藏 0
点赞
分享文章到微博
分享文章到朋友圈

上一篇:面试官求你了,别再问我TCP的三次握手和四次挥手

下一篇:Unity3D资源异步加载(二)——AssetBundle资源打包、加载

评论 (0)


登录后可评论,请 登录注册

评论