zookeeper搭建springcloud注册中心

zookeeper搭建springcloud注册中心,第1张

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拥有健康检查机制,当某服务挂掉时,不会立刻删除该服务,而是等待服务的重启

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5717488.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存