栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Zookeeper注册中心

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Zookeeper注册中心

Zookeeper注册中心

eureka已经不再更新,代替方案有zookeeper或consoul

zookeeper是一个分布式的协调工具,可以实现服务注册中心

linux 安装zookeeper(https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/)

https://blog.51cto.com/u_4837471/2466606

https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz

1. cloud-provider-payment8004(服务提供者)

    
        com.eiletxie.springcloud
        cloud-api-commons
        ${project.version}
    
    
        org.springframework.boot
        spring-boot-starter-web
    
    
    
        org.springframework.boot
        spring-boot-starter-actuator
    
    
    
        org.springframework.cloud
        spring-cloud-starter-zookeeper-discovery
        
            
            
                org.apache.zookeeper
                zookeeper
            
        
    
    
    
        org.apache.zookeeper
        zookeeper
        3.4.14
    
    
    
        org.springframework.boot
        spring-boot-devtools
        runtime
        true
    
    
        org.projectlombok
        lombok
        true
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    

 
server:
  port: 8004


#服务别名 --- 注册zookeeper到注册中心名称
spring:
  application:
    name: cloud-provider-payment
  cloud:
    zookeeper:
      connect-string: 192.168.111.144:2181
 
 
@SpringBootApplication
@EnableDiscoveryClient //该注解用于向使用consul或者zookeeper作为注册中心时注册服务
public class Payment8004 {

    public static void main(String[] args) {
        SpringApplication.run(Payment8004.class,args);
    }
}
@RestController
@Slf4j
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();
    }
}
 
如果不排除自带的3.5.3-zookeeper-beat会报jar包冲突
 
    
        org.springframework.cloud
        spring-cloud-starter-zookeeper-discovery
        
            
            
                org.apache.zookeeper
                zookeeper
            
        
    
验证

http://localhost:8004/payment/zk
2.cloud-consumerzk-order80(服务的消费者)
   
        
            org.example
            cloud-api-commons
            1.0-SNAPSHOT
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
        
            org.springframework.cloud
            spring-cloud-starter-zookeeper-discovery
            
                
                
                    org.apache.zookeeper
                    zookeeper
                
            
        
        
        
            org.apache.zookeeper
            zookeeper
            3.4.14
        
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

server:
  port: 80

#服务别名 --- 注册zookeeper到注册中心名称
spring:
  application:
    name: cloud-consumer-order
  cloud:
    zookeeper:
      connect-string: 127.0.0.1:2181
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZK80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderZK80.class,args);
    }
}

@RestController
public class OrderZkController {
    private static  final String INVOKE_URL = "http://cloud-provider-payment";

    @Autowired
    private RestTemplate restTemplate;
    @GetMapping(value = "/consumer/payment/zk")
    public String consumer() {
        String result = restTemplate.getForObject(INVOKE_URL+"/payment/zk",String.class);
        return result;
    }

}
@Configuration
public class ApplicationContextConfig {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return  new RestTemplate();
    }
}
验证

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

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

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