springCould中的zookeeper-从小白开始【3】

目录

1.启动zookeeper❤️❤️❤️

2.创建8004模块❤️❤️❤️

3.临时节点还是永久节点❤️❤️❤️

4.创建zk80消费模块❤️❤️❤️


1.启动zookeeper❤️❤️❤️

  • 进入自己zookeeper的bin目录下 分别使用命令:
php 复制代码
./zkServer.sh start   和 ./zkCli.sh -server 主机名

2.创建8004模块 ❤️❤️❤️

1.建模块

在父工程下创建模块

注意:jdk版本和,maven版本

2.添加pom

1.springboot依赖

2.mybatis依赖

3.druid依赖

4.mysql依赖

5.zookeeper依赖

XML 复制代码
 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.16</version>
        </dependency>
        <!--mysql-connector-java-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--jdbc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--引入自己的api通用包-->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--springboot整合zookeeper客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
    </dependencies>

3.添加yml

1.服务端口

2.服务名称

3.数据库连接池

4.zookeeper连接地址

5.mybatis配置

Groovy 复制代码
server:
  port: 8004


spring:
  application:
    #服务名称
    name: cloud-provider-payment

  datasource:
    #druid连接池
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springcloud
    username: root
    password: 123456
    #zookeeper服务注册中心
  cloud:
    zookeeper:
      connect-string: 192.168.20.129:2181,192.168.20.130:2181,192.168.20.131:2181

mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.xz.springcloud.entity

4.添加主启动类

@EnableDiscoveryClient:将服务注册到服务注册中心(zookeeper)

java 复制代码
@SpringBootApplication
@EnableDiscoveryClient//作为服务中心
public class PaymentMain8004 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8004.class);
    }
}

5.编写业务类

java 复制代码
@RestController
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

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

    @GetMapping("/payment/getById/{id}")
    public CommonResult getById(@PathVariable("id") Integer id){
        Payment payment = paymentService.getPaymentById(id);
        if (payment!=null){
            return new CommonResult(200,"查询成功,serverPort:"+serverPort,payment);
        }else {
            return new CommonResult(404,"查询无果,serverPort:"+serverPort,null);
        }
    }

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

6.测试

  • 1.启动主启动类,刷新浏览器
  • 2.访问zookeeper

说明zookeeper配置成功

3.临时节点还是永久节点❤️❤️❤️

当关闭8004服务时,在查询zookeeper,发现节点剔除

所以:zookeeper存储的是临时节点,当服务消失时,会马上剔除

4.创建zk80消费模块❤️❤️❤️

1.建模块

在父工程下创建模块

注意jdk版本和maven版本

2.写pom

1.springboot依赖

2.通用依赖

3.zookeeper依赖

XML 复制代码
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--spring boot整合zookeeper-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>

    </dependencies>

3.改yml

1.服务端口

2.服务名

3.zookeeper连接ip(我这里使用集群)

java 复制代码
server:
  port: 80
  
spring:
  application:
    name: cloud-consumer-order
    
  cloud:
    zookeeper:
      connect-string: 192.168.20.129:2181,192.168.20.130:2181,192.168.20.131:2181

4.主启动类

@EnableDiscoveryClient添加上

java 复制代码
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZkMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderZkMain80.class);
    }
}

5.业务代码

  • 1.先将Rest Template注入到spring中

注意:因为访问的是服务名,而不是具体的ip,所以加上@LoadBalance注解

java 复制代码
@Configuration
public class ApplicationConfig {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}
  • 2.controller

1.声明常量,表示zookeeper中8004的服务名

2.注入RestTemplate

3.调用restTemplate访问

java 复制代码
@RestController
public class OrderController {

    public static final String INVOKE_URL = "http://cloud-provider-payment";

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/payment/getById/{id}")
    public CommonResult<Payment>  getPayment(@PathVariable("id") Integer id){
        return restTemplate.getForObject(INVOKE_URL+"/payment/getById/"+id,CommonResult.class);
    }

    @GetMapping("/payment/zk")
    public String getDiscovery(){
        return restTemplate.getForObject(INVOKE_URL+"/payment/zk",String.class);
    }
}

6.测试

  • 1.使用80访问,刷新浏览器,依然成功
  • 2.查看zookeeper节点 ,节点创建成功

相关推荐
watermelonoops5 分钟前
Deepin和Windows传文件(Xftp,WinSCP)
linux·ssh·deepin·winscp·xftp
疯狂飙车的蜗牛1 小时前
从零玩转CanMV-K230(4)-小核Linux驱动开发参考
linux·运维·驱动开发
向前看-2 小时前
验证码机制
前端·后端
xlsw_2 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
神仙别闹3 小时前
基于java的改良版超级玛丽小游戏
java
远游客07133 小时前
centos stream 8下载安装遇到的坑
linux·服务器·centos
马甲是掉不了一点的<.<3 小时前
本地电脑使用命令行上传文件至远程服务器
linux·scp·cmd·远程文件上传
jingyu飞鸟3 小时前
centos-stream9系统安装docker
linux·docker·centos
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭3 小时前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
Data跳动4 小时前
Spark内存都消耗在哪里了?
大数据·分布式·spark