1. 下載 zookeeper
2. 進入apache-zookeeper-3.5.5-bin\apache-zookeeper-3.5.5-bin\bin\zkServer.cmd
啓動zookeeper
3.ZooInspector\build 啓動
java -jar zookeeper-dev-ZooInspector.jar
以上3步驟完成後,註冊中心服務端已經ok
4. java服務註冊到註冊中心
4.1 啓動類
package com.springcloud.zookeeper.server; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class ServerApplication { public static void main(String[] args) { SpringApplication.run(ServerApplication.class, args); } }
4.2 配置類
server.port=8000 # 註冊到zookeeper的服務名 spring.application.name=zk-server #zookeeper的地址和port spring.cloud.zookeeper.connect-string=127.0.0.1:2181
4.3 提供的服務
package com.springcloud.zookeeper.server; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ServerController { @Value("${server.port}") private String serverPort; @RequestMapping("/getServer") public String getServerPort(){ return "zookeeper 服務端,端口號:" +serverPort; } }
4.4 依賴
<?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>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.springcloud.zookeeper</groupId> <artifactId>server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>server</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR2</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> </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>
5. 服務客戶端
5.1 啓動類
package com.springcloud.zookeeper.client; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class ClientApplication { public static void main(String[] args) { SpringApplication.run(ClientApplication.class, args); } }
5.2 配置類
server.port=8001 spring.application.name=zk-client spring.cloud.zookeeper.connect-string=127.0.0.1:2181
5.3 調用服務類
package com.springcloud.zookeeper.client; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; import java.util.List; @RestController public class ClientController { @Resource private RestTemplate restTemplate; @Resource private DiscoveryClient discoveryClient; @RequestMapping("/getServer") public String getSever(){ String serverUrl = "http://zk-server/getServer"; return restTemplate.getForObject(serverUrl,String.class); } @RequestMapping("/discoveryClients") public List<ServiceInstance> discoveryClients(){ List<ServiceInstance> instances = discoveryClient.getInstances("zk-server"); for(ServiceInstance serviceInstance : instances){ System.out.println("url:" + serviceInstance.getUri()); } return instances; } @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } }
依賴與服務一樣