Zookeeper注册中心实战

Java学习手册+面试指南:https://javaxiaobear.cn

Spring Cloud Zookeeper通过自动配置和绑定到 Spring 环境和其他 Spring 编程模型习惯用法,为 Spring Boot 应用程序提供Apache Zookeeper集成。通过一些简单的注释,您可以快速启用和配置应用程序内的常见模式,并使用 Zookeeper 构建大型分布式系统。提供的模式包括服务发现和分布式配置。

特征

  • 服务发现:实例可以向 Zookeeper 注册,客户端可以使用 Spring 管理的 bean 发现实例
  • 支持 Spring Cloud LoadBalancer - 客户端负载均衡解决方案
  • 支持Spring Cloud OpenFeign
  • 分布式配置:使用Zookeeper作为数据存储

官方地址:https://spring.io/projects/spring-cloud-zookeeper#lea

Zookeeper注册中心实战_第1张图片

1、创建服务

创建名为:spring-cloud-zookeeper项目,

依赖如下:


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>com.javaxiaobeargroupId>
        <artifactId>javaxiaobear-zookeeperartifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>spring-cloud-zookeeperartifactId>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <spring.cloud.version>2023.0.0spring.cloud.version>
        <spring.boot.version>3.2.1spring.boot.version>
    properties>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-parentartifactId>
                <version>${spring.boot.version}version>
            dependency>

            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring.cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

project>

2、服务提供者

服务提供者名字为:javaxiaobear-provider

1、添加依赖


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0modelVersion>
	<parent>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-parentartifactId>
		<version>3.2.1version>
		<relativePath/> 
	parent>
	<groupId>com.javaxiaobeargroupId>
	<artifactId>javaxiaobear-providerartifactId>
	<version>0.0.1-SNAPSHOTversion>
	<name>javaxiaobear-providername>
	<description>javaxiaobear-providerdescription>
	<properties>
		<java.version>21java.version>
		<spring-cloud.version>2023.0.0spring-cloud.version>
	properties>
	<dependencies>
		<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.projectlombokgroupId>
			<artifactId>lombokartifactId>
			<scope>providedscope>
		dependency>
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-testartifactId>
			<scope>testscope>
		dependency>
	dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloudgroupId>
				<artifactId>spring-cloud-dependenciesartifactId>
				<version>${spring-cloud.version}version>
				<type>pomtype>
				<scope>importscope>
			dependency>
		dependencies>
	dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.bootgroupId>
				<artifactId>spring-boot-maven-pluginartifactId>
			plugin>
		plugins>
	build>

project>

2、配置文件application.yml

server:
  port: 8080 #端口

spring:
  application:
    name: product-service # 服务名称
  #  ZooKeeper 配置
  cloud:
    zookeeper:
      discovery:
        enabled: true # ?????? ZooKeeper ????????????? false ??
      connect-string: ip:2181

3、服务代码

1、实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product {

    private Integer id;
    private String productName;
    private Integer productNum;
    private Double productPrice;
}
2、服务接口
public interface ProviderService {
    /**
     * 查询商品列表
     *
     * @return
     */
    List<Product> selectProductList();
}
3、服务实现类
@Slf4j
@Service
public class ProviderServiceImpl implements ProviderService {
    @Override
    public List<Product> selectProductList() {

        log.info("商品服务查询商品信息...");
        return Arrays.asList(
                new Product(1, "小米手机", 1, 5800D),
                new Product(2, "小熊面试手册", 1, 68.88D),
                new Product(3, "小熊笔记", 5, 20.20D)
        );
    }
}
4、控制层
@RestController
public class ProviderController {

    @Autowired
    private ProviderService providerService;

    @GetMapping("/list")
    public List<Product> selectList(){
        return providerService.selectProductList();
    }
}

3、服务消费者

服务提供者名字为: javaxiaobear-consumer

1、添加依赖

依赖和提供者一样

2、添加配置文件

server:
  port: 9090 # ??

spring:
  application:
    name: consumer-service # ????
  # ?? ZooKeeper ????
  cloud:
    zookeeper:
      discovery:
        enabled: true # ?????? ZooKeeper ????????????? false ??
      connect-string: ip:2181

3、服务代码

1、控制层
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
    @Autowired
    private DiscoveryClient discoveryClient;
    @GetMapping("/go")
    public void order() {
        //调用提供者服务
        List<ServiceInstance> providerServer = discoveryClient.getInstances("product-service");

        if (0 == providerServer.size()) {
            return;
        }

        ServiceInstance serviceInstance = providerServer.get(0);
        System.out.print(serviceInstance.getUri() + " --- ");
        //获取接口路径
        String url = serviceInstance.getUri() + "/list";


        RestTemplate restTemplate = new RestTemplate();
        //调用接口
        String str = restTemplate.getForObject(url, String.class);
        System.out.println(str);
    }

}

4、测试

访问路径:http://localhost:9090/consumer/go

image-20231226160725386
Zookeeper注册中心实战_第2张图片

你可能感兴趣的:(Zookeeper,zookeeper,分布式,云原生,注册中心)