第五节:cloud-provider-payment8004(Zookeeper整合)

  • Zookeeper基础知识:【待补充】

 

  • 注册中心Zookeeper:

分布式协调工具,可以实现注册中心功能。

关闭Linux服务器防火墙后启动zookeeper服务器,Zookeeper服务器取代Eureka服务器,zk作为服务注册中心。

Linux命令:
[~]# pwd  --当前所在位置:.../bin
[~]# systemctl stop firewalld    --关闭防火墙
[~]# systemcctl status firewalld    --查看防火墙状态
[~]# ifconfig    --Linux的ip地址(要联网)
[~]# ping [Windows的ip地址]    --必须要ping通

[~]# ll    --列出当前目录文件夹的所有内容
[~]# ./zkServer.sh start    --启动Zookeeper服务器
[~]# ./zkClient.sh    -- 连接客户端【第一次很慢】
[localhost:8081 ~] ls /    --[zookeeper]节点
[localhost:8081 ~] get /zookeeper
[localhost:8081 ~] ls /zookeeper    --[quota]

[localhost:8081 ~] ls /services    --[cloud-provider-payment]
[localhost:8081 ~] ls /services/cloud-provider-payment    --[一串UUID的流水号]
[localhost:8081 ~] ls /services/cloud-provider-payment/一串UUID的流水号    --[]
[localhost:8081 ~] get /services/cloud-provider-payment/一串UUID的流水号    --[返回json串{name:"",id:"",address:"",...}]
  • 服务提供者:【cloud-provider-payment8004】

pom.xml(在8001基础上去掉Eureka依赖,添加Zookeeper依赖)

<!-- SpringBoot整合zookeeper客户端 -->
<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>

application.yml

server:
  port: 8004

spring:
  application:
    name: cloud-provider-payment
  cloud:
    zookeeper:
      connect-string: #此处的IP地址+端口号2181,为Linux的Zookeeper服务器所在地址

PaymentMain8004.java

package com.jiao.springcloud;

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

/**
 * @author jyl
 * @create 2021-01-25
 */

@SpringBootApplication
@EnableDiscoveryClient  //该注解用于向使用consul或者Zookeeper作为注册中心时注册服务
public class PaymentMain8004 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8004.class,args);
    }
}

PaymentController.java

package com.jiao.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
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
@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());
    }
}

                        注意:要补好dao,service,mapper!!!!

8004注册进Zookeeper出现错误:

启动Zookeeper服务器,再启动8004端口报错:【解决Zookeeper版本jar包冲突问题】

排除Zookeeper冲突后的新POM

<!-- SpringBoot整合zookeeper客户端 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
    <!-- 先排除自带的Zookeeper3.5.3 -->
    <exclusions>
         <exclusion>
              <groupId>org.apache.zookeeper</groupId>
              <artifactId>zookeeper</artifactId>
         </exclusion>
     </exclusions>
</dependency>
<!-- 添加Zookeeper3.4.9版本 -->
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.9</version>
</dependency>

<!-- 假如slf4j报错:Zookeeper自带有slf4j,排除即可 -->

测试:http://localhost:8004/payment/zk

                   服务节点:临时节点、持久节点

  • 服务消费者:【cloud-consumerzk-order80】

pom.xml:与8004相似。

application.yml:与8004相似【改端口80,name:cloud-consumer-order】

server:
  port: 80

spring:
  application:
    name: cloud-consumer-order
  cloud:
    zookeeper:
      connect-string: #此处的IP地址+端口号2181,为Linux的Zookeeper服务器所在地址

OrderZKMain80.java:

package com.jiao.springcloud;

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

/**
 * @author jyl
 * @create 2021-1-26
 */

@SpringBootApplication
@EnableDiscoveryClient
public class OrderZKMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderZKMain80.class,args);
    }
}

业务类:

(1)config.ApplicationContextConfig.java

package com.jiao.springcloud.config;

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 ApplicationContextConfig {
    @LoadBalanced
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

(2)OrderZKController.java

package com.jiao.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

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

    @Resource
    private RestTemplate restTemplate;

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

测试:http://localhost/consumer/payment/zk

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值