1. 学会使用才是研究工具的第一步
最近一直苦恼该如何学习,感觉看了很多的资料和视频去学习zookeeper,了解分析原理。但是过了一段时间,当我再想起zookeeper是啥的时候,我发现脑子里面已经很空白,空白到我甚至都不知道如何在现在的springboot中如何去使用它。索性自己尝试去使用zookeeper作为服务注册中心在实际中使用。
2. Spring Cloud让事情变得异常简单
- 只需要简单的依赖包引入,加上某些注解和配置即可完成整合
- 一定要注意zookeeper和spring cloud start的版本要对应好,否则服务启动会报错(这里使用的zookeeper版本是3.4.14)。
<?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>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xiong</groupId>
<artifactId>zk-discovery</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>zk-discovery</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR5</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
application.properties
server.port=8089
spring.application.name=zk-mem
spring.cloud.zookeeper.connect-string=192.168.182.128:2181
注解开启服务注册与发现
@EnableDiscoveryClient
@SpringBootApplication
public class ZkDiscoveryApplication {
public static void main(String[] args) {
SpringApplication.run(ZkDiscoveryApplication.class, args);
}
}
测试controller
@RestController
@RequestMapping("/api/demo")
public class TestController {
@GetMapping("/test")
public String hello() {
return UUID.randomUUID().toString();
}
}
消费者
pom.xml
<?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>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xiong</groupId>
<artifactId>zk-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>zk-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR5</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-zookeeper-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
application.properties
server.port=8081
spring.application.name=zk-fegin
spring.cloud.zookeeper.connect-string=192.168.182.128:2181
spring.cloud.discovery.enabled=true
spring.cloud.zookeeper.discovery.register=true
注解开启服务注册与发现,开启feign
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ZkConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ZkConsumerApplication.class, args);
}
}
fegin client声明定义
@FeignClient(value = "zk-mem", path = "/api/demo")
public interface DemoService {
@GetMapping("/test")
String testHello();
}
test controller
@RestController
@RequestMapping("/fegin")
public class TestFeignController {
@Autowired
DemoService demoService;
@GetMapping("remote")
public String feginHello() {
return demoService.testHello();
}
@GetMapping("local")
public String localHello() {
return "hello fegin";
}
}
调用结果截图: