第九节--Zookeeper服务注册与发现

1 .Eureka停止更新之后你怎么办?
    从2.0版本之后如何还继续用, 官方文档说明:后果自负
 
   SpringCloud整合Zookeeper替代Eureka
 
第一步:新建工程cloud-provider-payment8004,新加依赖,添加之后要排除自带的zookeeper,重新引入对应自己安装的zookeeper版本,引入的zookeeper要排除自身带的log日志包
<!--整合zookeeper整合zookeeper客户端-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
    <!--排除自带的的版本zookeeper,否则启动报错版本冲突-->
    <exclusions>
        <exclusion>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!--引入本地zookeeper一样的版本包:我这里本地的是3.4.13-->
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.13</version>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
        <exclusion>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </exclusion>
    </exclusions>
</dependency>

第二步:yml文件配置

server:
  port: 8004
spring:
  application:
    name: cloud-provider-payment
  cloud:
    zookeeper:
      connect-string: 192.168.1.104:2181

第三步:主配置类

@EnableDiscoveryClient //向使用consol或zookeeper作为注册中心时注册服务
@SpringBootApplication
public class PaymentApplication8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentApplication8004.class,args);
  }
}
 

zookeeper每注册一个服务,就会创建一个节点,创建的节点是临时节点还是持久节点?
  创建的肯定是临时节点: 它不会像eureka一样提供者或消费者服务关闭了还会保存服务基本信息
  如果服务关闭,直接剔除在zookeeper上的基本信息
 

 
客户端搭建
 
第一步:新建工程cloud-consumer-zkorder80,pom.xml文件
<dependencies>
    <!--整合zookeeper整合zookeeper客户端-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        <!--排除自带的的版本zookeeper:3.5.3,否则启动报错版本冲突-->
        <exclusions>
            <exclusion>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!--引入本地zookeeper一样的版本包:我这里本地的是3.6.0-->
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.13</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
            <exclusion>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
            </exclusion>
        </exclusions>
    </dependency>




    <dependency>
        <groupId>com.hx.springcloud</groupId>
        <artifactId>cloud-api-commos</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>




    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--热部署  -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
</dependencies>

第二步:yml文件

server:
  port: 80


spring:
  application:
    name: cloud-consumer-order #zookeeper对外暴露的微服务名字
  cloud:
    zookeeper:
      connect-string: 192.168.1.104:2181

第三步:config提供的RestTemplate配置类

@Configuration
public class ApplicationConfig {

@Bean
@LoadBalanced //这个注解必须标上,否则无法访问zookeeper上的微服务
public RestTemplate getTemplate(){
return new RestTemplate();
}
}

第四步:controller类

@RestController
@Slf4j
public class OrderController {


public static final String PAYMENT_URL="http://cloud-payment-service";

@Resource private RestTemplate restTemplate;
@PostMapping("/consumer/addPayment")
public CommonResult addPayment(Payment payment){
  System.out.println(payment.getSerial());
  return restTemplate.postForObject(PAYMENT_URL+"/admin/addPayment",payment,CommonResult.class);
}

@GetMapping("/consumer/findPaymentById/{id}")
public CommonResult findPaymentById(@PathVariable("id")Long id){
    log.info("消费端查找数据");
    return restTemplate.getForObject(PAYMENT_URL+"/admin/findPaymentById/"+id,CommonResult.class);
  }
}
 

最后启动服务测试即可;先启动zookeeper 进入zookeeper目录/bin zkServer.cmd
posted @ 2020-09-16 22:19  hexu_blog  阅读(269)  评论(0编辑  收藏  举报