SpringCloud微服务(三)——Zookeeper服务注册中心

Zookeeper服务注册中心

SpringCloud

zookeeper中的节点是持久还是临时的?

:临时的。

服务挂了,服务节点就没了。

安装Zookeeper

有win版

docker pull zookeeper3.5.2

docker run --privileged=true -d --name zookeeper --publish 2181:2181 -d zookeeper:latest

# 进入容器
docker exec -it id /bin/bash

# 进到services
cd services
ls
#可查看服务的名字

不需要server端:

服务提供者

依赖:zookeeper-discovery是基于SpringCloud依赖的

<!-- zookeeper -->
<zookeeper.version>3.4.14</zookeeper.version>
<!--添加zookeeper3.4.9版本-->
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>${zookeeper.version}</version>
</dependency>

<!--SpringBoot整合Zookeeper客户端-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
    <exclusions>
        <!--先排除自带的zookeeper3.5.3,如果zookeeper版本是3.4.x的话会jar冲突-->
        <exclusion>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<!--添加zookeeper3.4.9版本-->
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <exclusions>
        <!--排除zookeeper自带的slf4j,不然有引用slf4j会jar冲突-->
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
    </exclusions>
</dependency>

配置yml文件

server:
  # 8004表示注册到zookeeper服务器的支付服务提供者端口号
  port: 8004

spring:
  application:
    # 服务别名---注册zookeeper到注册中心的名称
    name: cloud-provider-payment
  cloud:
    zookeeper:
      # 默认localhost:2181
      connect-string: localhost:2181

启动类添加**@EnableDiscoveryClient**,该注解用于向使用consul或者zookeeper作为注册中心时注册服务

服务消费者

依赖配置一样,消费者使用RestTemplate调用服务提供者接口,@LoadBalanced实现集群负载均衡,轮询。

/**
 * @author zzyy
 * @date 2020-02-18 17:27
 **/
@Configuration
public class ApplicationContextConfig {
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}
/**
 * @author zzyy
 * @create 2020-02-18 17:23
 **/
@RestController
@Slf4j
public class OrderZkController {

    public static final String INVOKE_URL = "http://cloud-provider-payment"; //提供者服务名字,前提是消费者和提供者都要注册到注册中心

    @Resource
    private RestTemplate restTemplate;


    /**
     * http://localhost/consumer/payment/zk
     *
     * @return
     */
    @GetMapping("/consumer/payment/zk")
    public String paymentInfo() {
        return restTemplate.getForObject(INVOKE_URL + "/payment/zk", String.class);
    }
}

集群搭建

注册中心集群:connect-string: localhost:2181,ip1:2181,ip2:2181

服务集群保证服务名一致即可。

server:
  # 8004表示注册到zookeeper服务器的支付服务提供者端口号
  port: 8004

spring:
  application:
    # 服务别名---注册zookeeper到注册中心的名称
    name: cloud-provider-payment
  cloud:
    zookeeper:
      # 默认localhost:2181
      connect-string: localhost:2181,ip1:2181,ip2:2181
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wzq_55552

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值