SpringCloud之服务注册发现(Spring Cloud Zookeeper) |第十五章 -yellowcong

我们刚刚把服务注册弄到了Consul上了,当然springcloud也设计了,将服务弄到zookeeper上,让大家一起玩啊。配置Zookeeper注册发现的步骤:1、配置pom.xml,添加spring-cloud-starter-zookeeper-all依赖包,让springcloud 支持zookeeper,2、配置bootstrap.yml,设定zookeeper的服务信息,3、配置application.yml,设定服务信息,4、配置启动类,添加@EnableDiscoveryClient注解,让系统支持注册服务。5、添加测试类,6、启动服务并测试。

代码地址

https://gitee.com/yellowcong/springcloud/tree/master/chapter15

目录结构

这里写图片描述

搭建Zookeeper服务注册发现客户端

基本的步骤1、配置pom.xml,添加spring-cloud-starter-zookeeper-all依赖包,让springcloud 支持zookeeper,2、配置bootstrap.yml,设定zookeeper的服务信息,3、配置application.yml 4、配置启动类,添加@EnableDiscoveryClient注解,让系统支持注册服务。5、添加测试类,6、启动服务并测试。

1、配置pom.xml

添加spring-cloud-starter-zookeeper-all依赖包,让springcloud 支持zookeeper,其中,这个spring-cloud-starter-zookeeper-all 是包含了两个依赖包,一个是注册中心的,一个是配置服务的。

这里写图片描述

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zookeeper-all</artifactId>
</dependency>

<dependencyManagement>  
  <dependencies>  
       <dependency>  
           <groupId>org.springframework.cloud</groupId>  
           <artifactId>spring-cloud-zookeeper-dependencies</artifactId>  
           <version>1.0.1.RELEASE</version>  
           <type>pom</type>  
           <scope>import</scope>  
       </dependency>  
   </dependencies>  
</dependencyManagement> 

下面是完整的pom.xml配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>yellowcong.com</groupId>
    <artifactId>cas-client-springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>cas-client-springboot</name>
    <url>http://maven.apache.org</url>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <!-- 引用父类依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <!--zookeeper server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-all</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <dependencyManagement>  
        <dependencies>  
            <dependency>  
                <groupId>org.springframework.cloud</groupId>  
                <artifactId>spring-cloud-zookeeper-dependencies</artifactId>  
                <version>1.0.1.RELEASE</version>  
                <type>pom</type>  
                <scope>import</scope>  
            </dependency>  
        </dependencies>  
    </dependencyManagement>  


    <build>
        <plugins>
            <!-- 添加spring的插件, 就可以直接通过 mvn spring-boot:run 运行了 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>springloaded</artifactId>
                        <version>1.2.4.RELEASE</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

2、配置bootstrap.yml

这个配置zookeeper注册服务,需要在bootstrap.yml 文件中,配置zookeeper服务信息,在application.yml 配置无效,连接不上

#配置zookeeper注册服务器地址
spring:
  cloud:
    zookeeper:
      discovery:
        instancePort: ${server.port} #端口号
        instanceHost: yellowcong.com  #当前服务的初始化地址(可以不填)
        enabled: true
        register: true
      connectString: 192.168.66.100:2182 # 多节点配置,通过逗号分割192.168.253.31:2181,192.168.253.32:2181

3、配置application.yml

添加zookeeper的依赖

# 注册服务的端口
server:
  port: 8761

## 关闭安全控制
management:
  security:
    enabled: false
#配置服务的名称
spring:
  application:
    name: zk-client

4、配置启动类

添加@EnableDiscoveryClient注解,让系统支持注册服务。

package com.yellowcong;

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

@SpringBootApplication
@EnableDiscoveryClient
public class ConfigMain {

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

5、配置测试类

说实话,这些东西都很类似,所以直接复制粘贴过来即可,查看zookeeper服务注册上的信息。

package com.yellowcong.main;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @Autowired
    DiscoveryClient discoveryClient;

    @GetMapping("/test")
    public String test() {
        //获取实例化的注册节点
        List<ServiceInstance> list = discoveryClient.getInstances("CONSUL-CLIENT");

        //获取实例化的服务
        StringBuffer sb = new StringBuffer();
        if (list != null && list.size() > 0 ) {
            sb.append(list.get(0).getUri()+",");
        }
        return "hello world  "+sb.toString();
    }
}

6、测试服务

可以看到创建了一个节点。
这里写图片描述

直接在命令行获取zk的节点信息
这里写图片描述

访问 http://yellowcong.com:8761/test 查看服务
这里写图片描述

常见错误

1、java.net.ConnectException: Connection refused: no further information

这个错误,多半是客户端配置的zookeeper地址有毛病

java.net.ConnectException: Connection refused: no further information
    at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[na:1.8.0_102]
    at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) ~[na:1.8.0_102]
    at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361) ~[zookeeper-3.4.6.jar:3.4.6-1569965]
    at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1081) ~[zookeeper-3.4.6.jar:3.4.6-1569965]

参考文章

1、Java笔记(3) - 使用Spring Cloud Zookeeper + Feign实现服务发现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狂飙的yellowcong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值