1.这里我以windows版本的zookeeper为案例进行整合
2.启动zookeeper
D:\zookpeer\zookeeper-3.3.6\bin zkserver.cmd
D:\zookpeer\ZooInspector\build 双击zookeeper-dev-ZooInspector.jar,这个时候如下图所示点击启动
以上是我本地存储的位置,zookeeper客户端服务端下载地址:
3.我们这里订单服务调用会员服务,首先搭建会员服务member,
首先创建一个maven项目,把下面依赖粘贴进去
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<!-- 管理依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot整合eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
</dependencies>
<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
4.新建一个application.yml文件,把下面配置文件粘贴进去
###订单服务的端口号
server:
port: 8002
###服务别名----服务注册到注册中心名称
spring:
application:
name: zk-member
cloud:
zookeeper:
connect-string: 127.0.0.1:2181
5.新建一个app启动类,代码如下:
package zk_member;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
@EnableDiscoveryClient
public class App {
@Value("${server.port}")
private String serverPort;
@RequestMapping("/getMember")
public String getMember() {
return "订单服务调用会员服务,端口:"+serverPort;
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
注意:当使用Eureka作为注册中心时我们使用的注解是@EnableEurekaClient,这里使用zookeeper作为注册中心要使用注解:@EnableDiscoveryClient
EnableDiscoveryClient作用是如果注册中心使用consul、zookeeper就使用注解@EnableDiscoveryClient向注册中心注册服务
6.然后搭建order服务
首先将上面member的依赖copy过来,然后新建application.yml,将下面配置文件粘贴过来
###订单服务的端口号
server:
port: 8003
###服务别名----服务注册到注册中心名称
spring:
application:
name: zk-order
cloud:
zookeeper:
connect-string: 127.0.0.1:2181
7.然后新建启动类app
package zk_order;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class App {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/order")
public String getList(){
String memberUrl="http://zk-member/getMember";
return restTemplate.getForObject(memberUrl, String.class);
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
然后分别启动member和order
此时通过order访问member成功就代表zookeeper整合成功
感兴趣的小伙伴可以扫码关注下公众号哦,公众号会分享高质量的技术文章哦