SpringCloud(二)zookeeper注册中心

SpringCloud支持三种注册方式Eureka, Consul(go语言编写),zookeeper
Dubbo支持两种注册方式Redis和zookeeper.

由于Eureka2.0闭源, 我们可以使用zookeeper作为注册中心.
首先要在自己的电脑上安装zookeeper, 安装完成之后切换到/zookeeper/bin目录

启动zk

./zkServer.sh start //启动

连接zk客户端

./zkCli.sh -server 127.0.0.1:2181
[zk: 127.0.0.1:2181(CONNECTED) 0] help //输入help命令

退出zk客户端连接

quit

退出zk

./zkServer.sh stop


ZooInspector客户端下载地址
https://issues.apache.org/jira/secure/attachment/12436620/ZooInspector.zip
这个是一个可视话的客户端

SpringCloud(二)zookeeper注册中心_第1张图片
屏幕快照 2019-04-19 下午4.34.13.png

新建服务者项目

添加依赖


        org.springframework.boot
        spring-boot-starter-parent
        2.0.1.RELEASE
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Finchley.M7
                pom
                import
            
        
    
    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.cloud
            spring-cloud-starter-zookeeper-discovery
        

    
    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/libs-milestone
            
                false
            
        
    

yml配置

###服务端口号
server:
  port: 8000
###服务名称
spring:
  application:
    name: zk-member
  cloud:
    zookeeper:
    ###注册到zookeeper地址
      connect-string: 127.0.0.1:2181

创建启动类

@RestController
@SpringBootApplication
@EnableDiscoveryClient
public class ZkMemberController {
    @Value("${server.port}")
    private String serverPort;

    @RequestMapping("/getMember")
    public String getMember() {
        return "订单服务调用接口,端口号为:" + serverPort;
    }

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

@EnableDiscoveryClient 作用是 如果服务使用connsul、zookeeper ,使用@EnableDiscoveryClient 向注册中心上注册服务

运行服务, 该服务会被注册到zk上. 以下为注册信息

{
    "name": "zk-member",
    "id": "c233c176-51b1-4046-8d36-065bd8c69673",
    "address": "10.10.15.24",
    "port": 8000,
    "sslPort": null,
    "payload": {
        "@class": "org.springframework.cloud.zookeeper.discovery.ZookeeperInstance",
        "id": "application-1",
        "name": "zk-member",
        "metadata": {}
    },
    "registrationTimeUTC": 1555662311685,
    "serviceType": "DYNAMIC",
    "uriSpec": {
        "parts": [{
            "value": "scheme",
            "variable": true
        }, {
            "value": "://",
            "variable": false
        }, {
            "value": "address",
            "variable": true
        }, {
            "value": ":",
            "variable": false
        }, {
            "value": "port",
            "variable": true
        }]
    }
}

创建消费者项目

添加依赖


        org.springframework.boot
        spring-boot-starter-parent
        2.0.1.RELEASE
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Finchley.M7
                pom
                import
            
        
    
    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.cloud
            spring-cloud-starter-zookeeper-discovery
        

    
    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/libs-milestone
            
                false
            
        
    

yml配置

###服务端口号
server:
  port: 8060
###服务名称
spring:
  application:
    name: zk-order
  cloud:
    zookeeper:
    ###注册到zookeeper地址
      connect-string: 127.0.0.1:2181

创建启动类

@SpringBootApplication
@RestController
@EnableDiscoveryClient
public class ZkOrderController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;

    // springcloud 中使用那些技术实现调用服务接口 feign 或者rest
    @RequestMapping("/orderToMember")
    public String orderToMember() {
        String memberUrl = "http://zk-member/getMember";
        return restTemplate.getForObject(memberUrl, String.class);
    }

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

    // 默认rest方式开启 负载均衡功能
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

如何获取到注册中心上服务列表信息?

private DiscoveryClient discoveryClient;
@RequestMapping("/discoveryClientMember")
    public List discoveryClientMember() {
        List instances = discoveryClient.getInstances("zk-member");
        for (ServiceInstance serviceInstance : instances) {
            System.out.println("url:" + serviceInstance.getUri());
        }
        return instances;
    }

通过该方法可以获取zk上的所有节点信息, 注册到zk上的节点全部为临时节点, 随着服务的断开, 节点也会从zk上移除.

你可能感兴趣的:(SpringCloud(二)zookeeper注册中心)