栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

zookeeper搭建springcloud注册中心

zookeeper搭建springcloud注册中心

安装并启动zookeeper

我的其他博客有安装细节

搭建生产者 修改pom

    
    
        org.springframework.boot
        spring-boot-starter-web
    
    
    
        org.springframework.cloud
        spring-cloud-starter-zookeeper-discovery
    
    
        org.springframework.boot
        spring-boot-devtools
        runtime
        true
    
    
        org.projectlombok
        lombok
        true
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    

application.yml
#8004表示注册到zookeeper服务器的支付服务提供者端口号
server:
  port: 8004
#服务别名----注册zookeeper到注册中心名称
spring:
  application:
    name: producer
  cloud:
    zookeeper:
    	# zk的地址
      connect-string: 192.168.1.105:2181
启动类
package com.xiong;

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

@SpringBootApplication
@EnableDiscoveryClient //该注解用于向使用consul或者zookeeper作为注册中心时注册服务
public class Zk8004 {
    public static void main(String[] args) {
        SpringApplication.run(Zk8004.class,args);
    }
}
业务类 Controller
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;

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

    @RequestMapping(value = "/payment/zk")
    public String paymentzk()
    {
        return "springcloud with zookeeper: "+serverPort+"t"+ UUID.randomUUID().toString();
    }
}
搭建消费者 修改pom

    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.cloud
            spring-cloud-starter-zookeeper-discovery
            
            
                
                    org.apache.zookeeper
                    zookeeper
                
            
        
        
        
            org.apache.zookeeper
            zookeeper
            3.4.9
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
修改application.yml
server:
  port: 8080

spring:
  application:
    name: consumer
  cloud:
    #注册到zookeeper地址
    zookeeper:
      connect-string: 192.168.1.105:2181
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class OrderZK80
{
    public static void main(String[] args)
    {
        SpringApplication.run(OrderZK80.class,args);
    }
}
配置类
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ApplicationContextBean
{
    @Bean
    @LoadBalanced // 让RestTemplate具有负载均衡的能力
    public RestTemplate getRestTemplate()
    {
        return new RestTemplate();
    }
}
业务类(controller)
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderZKController
{
    // 生产者服务名
    public static final String INVOKE_URL = "http://producer";

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/consumer/payment/zk")
    public String paymentInfo()
    {
        String result = restTemplate.getForObject(INVOKE_URL+"/payment/zk", String.class);
        System.out.println("消费者调用支付服务(zookeeper)--->result:" + result);
        return result;
    }
}
启动并测试 启动

启动zookeeper

启动生产者

启动消费者

测试

在zookeep中已经出现两个节点:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tvpAyC6K-1643101082740)(D:study教程资料git下载tutorial-summary5-博客springcloudzookeeper搭建微服务集群.assetsimage-20220125164750672.png)]

访问

http://localhost:8080/consumer/payment/zk

访问结果:

springcloud with zookeeper: 8004 9cb4a83f-9c95-47e4-a8df-49fd9f3182ec

如此则配置成功

常见面试题

zookeeper与eureka的区别

zookeeper牺牲高可用而拥有一致性 CP

eureka牺牲一致性而拥有高可用 AP

eureka拥有健康检查机制,当某服务挂掉时,不会立刻删除该服务,而是等待服务的重启
consumer/payment/zk

访问结果:

```txt
springcloud with zookeeper: 8004 9cb4a83f-9c95-47e4-a8df-49fd9f3182ec

如此则配置成功

常见面试题

zookeeper与eureka的区别

zookeeper牺牲高可用而拥有一致性 CP

eureka牺牲一致性而拥有高可用 AP

eureka拥有健康检查机制,当某服务挂掉时,不会立刻删除该服务,而是等待服务的重启

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/721177.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号