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
1、创建服务
创建名为:spring-cloud-zookeeper
项目,
依赖如下:
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion><parent><groupId>com.javaxiaobear</groupId><artifactId>javaxiaobear-zookeeper</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>spring-cloud-zookeeper</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><spring.cloud.version>2023.0.0</spring.cloud.version><spring.boot.version>3.2.1</spring.boot.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>${spring.boot.version}</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring.cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement></project>
2、服务提供者
服务提供者名字为:javaxiaobear-provider
1、添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.2.1</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.javaxiaobear</groupId><artifactId>javaxiaobear-provider</artifactId><version>0.0.1-SNAPSHOT</version><name>javaxiaobear-provider</name><description>javaxiaobear-provider</description><properties><java.version>21</java.version><spring-cloud.version>2023.0.0</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zookeeper-discovery</artifactId></dependency><!-- spring boot web 依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- lombok 依赖 --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>provided</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></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 {@Overridepublic 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 {@Autowiredprivate 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 {@Autowiredprivate 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