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整合成功