SpringCloud从入门到放弃02 —— Zookeeper服务注册与发现

文章目录

    • Zookeeper服务注册与发现
      • 一、zookeeper简介
        • 1.什么是zookeeper
      • 二、SpringCloud 整合 Zookeeper
        • 1.关闭Linux服务器防火墙后启动zookeeper服务器
        • 2.新建 module cloud-provider-payment8004

Zookeeper服务注册与发现

一、zookeeper简介

1.什么是zookeeper

ZooKeeper 是一个集中式服务,用于维护配置信息、命名、提供分布式同步和提供组服务。所有这些类型的服务都以某种形式被分布式应用程序使用。每次实施它们时,都会进行大量工作来修复不可避免的错误和竞争条件。由于实现这些服务的难度,应用程序最初通常会忽略它们,这使得它们在发生变化时变得脆弱并且难以管理。即使正确完成,这些服务的不同实现也会在部署应用程序时导致管理复杂性。

二、SpringCloud 整合 Zookeeper

1.关闭Linux服务器防火墙后启动zookeeper服务器

查看linux防火墙是否关闭,如果没有关闭则用 systemctl stop firewalld 来关闭防火墙,本处linux防火墙是已经关闭了的

[root@root ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

Aug 30 11:38:38 root systemd[1]: Starting firewalld - dynamic firewall daemon...
Aug 30 11:38:41 root systemd[1]: Started firewalld - dynamic firewall daemon.
Aug 30 14:51:29 root systemd[1]: Stopping firewalld - dynamic firewall daemon...
Aug 30 14:51:31 root systemd[1]: Stopped firewalld - dynamic firewall daemon.
2.新建 module cloud-provider-payment8004

1.写 pom.xml

<dependencies>
        <dependency>
            <groupId>com.avgrado.springcloudgroupId>
            <artifactId>cloud-api-commonsartifactId>
            <version>${project.version}version>
        dependency>
       <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-zookeeper-discoveryartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency
        >
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

2.写application.yml文件

server:
  port: 8004 #8004表示注册到zookeeper服务器的支付服务提供者端口号
spring:
  application:
    #服务别名----注册zookeeper到注册中心名称
    name: payment8004
  cloud:
    zookeeper:
      connect-string: 192.168.199.128:2181

3.写主启动类

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

/**
 * @ClassName PaymentApplication8004
 * @Description PaymentApplication8004
 * @Author avgrado
 * @Date 2022-01-25 15:36
 */
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentApplication8004 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentApplication8004.class,args);
    }
}

4.验证测试 :
服务代码如下

package com.avgrado.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @ClassName PaymentController
 * @Description Controller
 * @Author avgrado
 * @Date 2022-01-25 15:44
 */
@RestController
@Slf4j
public class PaymentController {

    @Value("${server.port}")
    private String serverPort;

    @RequestMapping("/payment/zk")
    public String paymentZk(){
        return "springcloud with zookeeper "+serverPort+"\t"+ UUID.randomUUID().toString();
    }
}

访问 http://localhost:8004/payment/zk
SpringCloud从入门到放弃02 —— Zookeeper服务注册与发现_第1张图片

你可能感兴趣的:(springcloud,Spring,Boot,java-zookeeper,spring,cloud,zookeeper)