zoukankan      html  css  js  c++  java
  • Spring Cloud之整合ZK作为注册中心

    Eureka已经闭源了,用zk可以替代之

    Eureka 作为注册中心

    Dubbo也是zk作为注册中心的

     Zookeeper简介

    Zookeeper是一个分布式协调工具,可以实现服务注册与发现、注册中心、消息中间件、分布式配置中心等。

     

    公共pom:

    <parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>2.0.1.RELEASE</version>
    	</parent>
    	<!-- 管理依赖 -->
    	<dependencyManagement>
    		<dependencies>
    			<dependency>
    				<groupId>org.springframework.cloud</groupId>
    				<artifactId>spring-cloud-dependencies</artifactId>
    				<version>Finchley.M7</version>
    				<type>pom</type>
    				<scope>import</scope>
    			</dependency>
    		</dependencies>
    	</dependencyManagement>
    	<dependencies>
    		<!-- SpringBoot整合Web组件 -->
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    		<!-- SpringBoot整合eureka客户端 -->
    		<dependency>
    			<groupId>org.springframework.cloud</groupId>
    			<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
    		</dependency>
    
    	</dependencies>
    	<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
    	<repositories>
    		<repository>
    			<id>spring-milestones</id>
    			<name>Spring Milestones</name>
    			<url>https://repo.spring.io/libs-milestone</url>
    			<snapshots>
    				<enabled>false</enabled>
    			</snapshots>
    		</repository>
    	</repositories>
      
    

      

    package com.toov5.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @SpringBootApplication
    @EnableDiscoveryClient //如果服务使用consul或者zk使用这个注解 向注册中心注册服务
    public class zkMemberApiController {
        @Value("${server.port}")
        private String serverPort;
        
        
        @RequestMapping("/getMember")
      public String getMember() {
            return "会员服务prot:"+serverPort;
      }
        
        public static void main(String[] args) {
            SpringApplication.run(zkMemberApiController.class, args);
        }
    }

    ###订单服务的端口号
    server:
    port: 8003
    ###服务别名----服务注册到注册中心名称
    spring:
    application:
    name: zk-member
    cloud:
    zookeeper:
    connect-string: 192.168.91.7:2181

    启动后: yml配置文件的别名

    通过json解析工具:

    {
    	"name": "zk-member",
    	"id": "c01a3167-71c4-4d8a-8584-332c659e2d57",
    	"address": "localhost",
    	"port": 8002,
    	"sslPort": null,
    	"payload": {
    		"@class": "org.springframework.cloud.zookeeper.discovery.ZookeeperInstance",
    		"id": "application-1",
    		"name": "zk-member",
    		"metadata": {}
    	},
    	"registrationTimeUTC": 1542086637317,
    	"serviceType": "DYNAMIC",
    	"uriSpec": {
    		"parts": [{
    			"value": "scheme",
    			"variable": true
    		}, {
    			"value": "://",
    			"variable": false
    		}, {
    			"value": "address",
    			"variable": true
    		}, {
    			"value": ":",
    			"variable": false
    		}, {
    			"value": "port",
    			"variable": true
    		}]
    	}
    }
    

      

     yml

    ###订单服务的端口号
    server:
      port: 8002
    ###服务别名----服务注册到注册中心名称 
    spring:
      application:
        name: zk-order
      cloud:
        zookeeper:
          connect-string: 192.168.91.7:2181
    

      controller

    package com.toov5.api.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    @RestController
    @SpringBootApplication
    @EnableDiscoveryClient //如果服务使用consul或者zk使用这个注解 向注册中心注册服务
    public class zkOrderApiController {
        @Value("${server.port}")
        private String serverPort;
        @Autowired
        private RestTemplate restTemplate;
        
        @RequestMapping("/orderToMember")
      public String orderToMember() {
            String url ="http://zk-member/getMember"; 
            return restTemplate.getForObject(url, String.class);
      }
        
        public static void main(String[] args) {
            SpringApplication.run(zkOrderApiController.class, args);
        }
        @Bean
        @LoadBalanced
        RestTemplate restTemplate() {
            return new RestTemplate();
        }
    }

    可以实现轮询

  • 相关阅读:
    EventBus详解
    Java BigDecimal使用
    StringFormate使用
    Sourcetree拉取推送问题
    Android下拉刷新控件android-Ultra-Pull-To-Refresh 使用
    SourceTree跳过Atlassian账号,免登陆,跳过初始设置
    Android Studio3.2新建项目gradle read time out
    底部导航栏使用BottomNavigationBar
    PopupWindow封装
    电脑连接真机,但是androidstudio不显示手机,ADB Interface黄色感叹号
  • 原文地址:https://www.cnblogs.com/toov5/p/9952131.html
Copyright © 2011-2022 走看看