zoukankan      html  css  js  c++  java
  • idea+springcloud+zookeeper做的服务发现和远程连接zookeeper

    一,我们新建一个springboot项目

     二,添加项目micro-provider,结构如下

    三,代码如下

    package com.nl.provider.microprovider.controller;
    
    import com.nl.provider.microprovider.domain.ChatMsg;
    import com.nl.provider.microprovider.service.ChatMsgService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class ChatMsgController {
    
        @Autowired
        private ChatMsgService chatMsgService;
    
        @GetMapping("/Chat/GetModel/{id}")
        public ChatMsg GetModel(@PathVariable("id") Integer cId) {
            return chatMsgService.GetChatMsg(cId);
        }
    }
    package com.nl.provider.microprovider.domain;
    
    import lombok.Getter;
    import lombok.Setter;
    
    @Getter
    @Setter
    public class ChatMsg {
        private Integer Id;
        private String Content;
    }
    package com.nl.provider.microprovider.service.imp;
    
    import com.nl.provider.microprovider.domain.ChatMsg;
    import com.nl.provider.microprovider.service.ChatMsgService;
    import org.springframework.stereotype.Service;
    
    @Service
    public class ChatMsgServiceImp implements ChatMsgService {
    
        @Override
        public ChatMsg GetChatMsg(Integer Id) {
            ChatMsg chatMsg=new ChatMsg();
            chatMsg.setId(1);
            chatMsg.setContent("111");
            return  chatMsg;
        }
    }
    package com.nl.provider.microprovider.service;
    
    import com.nl.provider.microprovider.domain.ChatMsg;
    
    public interface ChatMsgService {
        ChatMsg GetChatMsg(Integer Id);
    }
    package com.nl.provider.microprovider;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @SpringBootApplication
    @EnableDiscoveryClient
    public class MicroProviderApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MicroProviderApplication.class, args);
        }
    
    }

    四,配置文件如下

    application.yml

    spring:
      application:
        name: provider
      cloud:
        zookeeper:
          connect-string: localhost:2181
    
    logging:
      level:
        com.cloud.demo.microprovider.mapper:  debug

    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.6.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.nl.provider</groupId>
        <artifactId>micro-provider</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>micro-provider</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <!--spring cloud依赖管理-->
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Finchley.SR2</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--<dependency>-->
            <!--<groupId>org.mybatis.spring.boot</groupId>-->
            <!--<artifactId>mybatis-spring-boot-starter</artifactId>-->
            <!--<version>1.3.2</version>-->
            <!--</dependency>-->
    
            <!--引入zookeeper-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-zookeeper-all</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.apache.zookeeper</groupId>
                        <artifactId>zookeeper</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.4.12</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-log4j12</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
            </dependency>
            <!--<dependency>-->
            <!--<groupId>mysql</groupId>-->
            <!--<artifactId>mysql-connector-java</artifactId>-->
            <!--</dependency>-->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <fork>true</fork>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

    五,运行zookeeper,如下两个程序

    在 zkCli.cmd窗口输入

     敲一下命令,《provider》是自己在配置文件配置的名字

    ls /
    ls /services
    ls /services/provider

    得到如上结构,证明zk发现成功

    六,发现失败原因,在我们新建springboot项目的时候这个springboot版本是2.2.2,而现在springcloud支持的版本必须跟spring boot对应,要不然你会发现一直发现不了服务,如下2.2.2是对应不了我新增的

     本文的配置是如下,

    2》查看对应版本地址https://spring.io/projects/spring-cloud

    七,添加多台微服务,如下,-Dserver.port=8082,原理是设置多个端口

     启动客户端可以访问发现,看客户端的控制台输出,发现三台,不同端口的服务

    PS:经过测试,用springcloud做的负载,如果有一个服务端挂掉了,可是如果用户接着访问,就会出现第一次错误提示,如果用户接着访问,服务端就会自动切换到没有挂掉的服务器

    八,可是经过测试上文的pom.xml文件的zookeeper配置并不能连接远程的zookeeper,那这个问题怎么解决呢?我们来替换掉远程连接的zookeeper包

    如下:

      <!--仅仅适用于本地连接zookeeper-->
            <!--<dependency>-->
                <!--<groupId>org.springframework.cloud</groupId>-->
                <!--<artifactId>spring-cloud-starter-zookeeper-all</artifactId>-->
                <!--<exclusions>-->
                    <!--<exclusion>-->
                        <!--<groupId>org.apache.zookeeper</groupId>-->
                        <!--<artifactId>zookeeper</artifactId>-->
                    <!--</exclusion>-->
                <!--</exclusions>-->
            <!--</dependency>-->
            <!--<dependency>-->
                <!--<groupId>org.apache.zookeeper</groupId>-->
                <!--<artifactId>zookeeper</artifactId>-->
                <!--<version>3.4.12</version>-->
                <!--<exclusions>-->
                    <!--<exclusion>-->
                        <!--<groupId>org.slf4j</groupId>-->
                        <!--<artifactId>slf4j-log4j12</artifactId>-->
                    <!--</exclusion>-->
                <!--</exclusions>-->
            <!--</dependency>-->
            <!--仅仅适用于本地连接zookeeper-->
            <!--远程连接zookeeper-->
            <!--Service Discovery with Zookeeper-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
            </dependency>
            <!--远程连接zookeeper-->

    这个时候我们就可以更改application.yml文件中的连接配置,如下

    spring:
      cloud:
        zookeeper:
          connect-string: 192.168.30.145:2181
      application:
        name: provider

    九,运行看结果,连接成功

  • 相关阅读:
    mysql数据库给别人访问权限
    linux 5-sort,uniq,tar,split
    linux 4 -awk
    Linux 3 -grep
    linux 基础2-null,cut,wc,head,tail
    linux 基础-变量,shell基本语法
    [ActionScript 3.0] 利用InteractivePNG.as类精确选择识别png图片有像素的区域
    [ActionScript 3.0] 嵌入字体
    [ActionScript 3.0] AS利用ByteArray向PHP发送二进制数据生成图片
    [ActionScript 3.0] AS向php发送二进制数据方法之——在URLRequest中构造HTTP协议发送数据
  • 原文地址:https://www.cnblogs.com/May-day/p/12215835.html
Copyright © 2011-2022 走看看