七、SpringCloud 搭建Zookeeper註冊中心
1、本地啓動zookeeper(進入zk的bin目錄下 執行 ./zkServer.sh start命令)
2、打開zookeeper的圖形化視圖工具
zookeeper圖像化客戶端工具的下載地址:https://issues.apache.org/jira/secure/attachment/12436620/ZooInspector.zip;
打開方式:進入build目錄下,找到下載出的文件執行 java -jar zookeeper-dev-zooinspector.jar 命令,效果如下:
3、建立maven工程,pom.xml中導入相關jar包
<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.0</modelVersion>
<groupId>com.gonghua</groupId>
<artifactId>springcloud-zookeeper-member</artifactId>
<version>0.0.1-SNAPSHOT</version>
<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整合zookeeper客戶端 -->
<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>
</project>
4、配置application.properties或yml
##服務端口號
server.port=8003
##服務別名--服務註冊到註冊中心名稱
spring.application.name=zk-member
##註冊到zookeeper服務地址
spring.cloud.zookeeper.connect-string=127.0.0.1:2181
5、編寫啓動類
package com.gonghua.api.controller;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
@EnableDiscoveryClient
public class ZookeeperMemberController {
@Value("${server.port}")
private String serverPort;
@RequestMapping("/getMember")
public String getMember(){
return "hello zookeeper member!!! the port is "+serverPort;
}
//@EnableDiscoveryClient作用:如果服務使用consul、zookeeper則使用該註解。目的是向註冊中心上註冊服務
public static void main(String[] args) {
SpringApplication.run(ZookeeperMemberController.class, args);
}
}
6、啓動成功,zk客戶端顯示如下
使用zookeeper做負載均衡
1、服務提供者如上已經建立好,接下來建立一個服務消費者,代碼如下:
1、pom.xml文件通服務提供者的配置一致,見上述配置。
2、application.yml 或application.properties配置如下:
##服務端口號
server.port=8004
##服務別名--服務註冊到註冊中心名稱
spring.application.name=zk-order
##註冊到zookeeper服務地址
spring.cloud.zookeeper.connect-string=127.0.0.1:2181
3、啓動類代碼:
package com.gonghua;
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.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@SpringBootApplication
@EnableDiscoveryClient
public class ZookeeperOrderController {
//RestTemplate 是由springboot web組件提供,默認整合ribbon負載均衡器,rest方式底層是採用httpclient技術
@Autowired
private RestTemplate restTemplate;
/**
* 在springcloud中有兩種方式調用
* 1、rest調用
* 2、fegin(springcloud)
* @return
*/
@RequestMapping("/getOrder")
public String getOrder(){
//別名調用,別名見服務提供者的yml配置
String url = "http://zk-member/getMember";
String result1 = restTemplate.getForObject(url, String.class);
System.out.println("order 別名調用 member 服務:"+result1);
return result1;
}
public static void main(String[] args) {
SpringApplication.run(ZookeeperOrderController.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
2、啓動服務提供者,然後不停服務的情況下,更改提供者的端口,再重啓(相當於啓動了兩臺服務提供者,以便做負載均衡),再啓動服務消費者,此時zookeeper註冊中心如下圖: