Java学习手册+面试指南:https://javaxiaobear.cn
Spring Cloud Zookeeper通过自动配置和绑定到 Spring 环境和其他 Spring 编程模型习惯用法,为 Spring Boot 应用程序提供Apache Zookeeper集成。通过一些简单的注释,您可以快速启用和配置应用程序内的常见模式,并使用 Zookeeper 构建大型分布式系统。提供的模式包括服务发现和分布式配置。
特征
官方地址:https://spring.io/projects/spring-cloud-zookeeper#lea
创建名为: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>
服务提供者名字为:javaxiaobear-provider
<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>
server:
port: 8080 #端口
spring:
application:
name: product-service # 服务名称
# ZooKeeper 配置
cloud:
zookeeper:
discovery:
enabled: true # ?????? ZooKeeper ????????????? false ??
connect-string: ip:2181
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product {
private Integer id;
private String productName;
private Integer productNum;
private Double productPrice;
}
public interface ProviderService {
/**
* 查询商品列表
*
* @return
*/
List<Product> selectProductList();
}
@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)
);
}
}
@RestController
public class ProviderController {
@Autowired
private ProviderService providerService;
@GetMapping("/list")
public List<Product> selectList(){
return providerService.selectProductList();
}
}
服务提供者名字为: javaxiaobear-consumer
依赖和提供者一样
server:
port: 9090 # ??
spring:
application:
name: consumer-service # ????
# ?? ZooKeeper ????
cloud:
zookeeper:
discovery:
enabled: true # ?????? ZooKeeper ????????????? false ??
connect-string: ip:2181
@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);
}
}
访问路径:http://localhost:9090/consumer/go