Zookeeper服務註冊與發現
阿新 • • 發佈:2020-10-07
學習地址:https://www.bilibili.com/video/BV18E411x7eT?p=28
環境準備
zookeeper版本:3.4.9
下載地址:http://archive.apache.org/dist/zookeeper/zookeeper-3.4.9/
- 下載安裝zookeeper
[root@localhost ~]# cd /usr/local # 解壓 [root@localhost local]# tar -zxvf zookeeper-3.4.9.tar.gz # 複製一份配置檔案 [root@localhost zookeeper-3.4.9]# cp conf/zoo_sample.cfg conf/zoo.cfg [root@localhost zookeeper-3.4.9]# cd bin [root@localhost bin]# pwd /usr/local/zookeeper-3.4.9/bin
- 關閉防火牆
[root@localhost bin]# systemctl stop firewalld
[root@localhost bin]# systemctl status firewalld
- 檢視IP
[root@localhost bin]# ifconfig
# 192.168.110.128
註冊中心Zookeeper
zookeeper是一個分散式協調工具,可以實現註冊中心功能
關閉Linux伺服器防火牆後啟動zookeeper伺服器
zookeeper伺服器取代Eureka伺服器,zk作為服務註冊中心
服務提供者
cloud-provider-payment8004
-
建module
-
寫pom
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springcloud2020</artifactId> <groupId>com.nuc.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-provider-payment8004</artifactId> <dependencies> <dependency> <groupId>com.nuc.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</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-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
- 寫YML
#8004表示註冊到zookeeper伺服器的支付服務提供的埠號
server:
port: 8004
#服務別名-註冊zookeeper到註冊中心名稱
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: 192.168.110.128
- 主啟動類
@SpringBootApplication
//該註解用於向使用consul或zookeeper作為註冊中心時註冊服務
@EnableDiscoveryClient
public class PaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8004.class,args);
}
}
- Controller
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@GetMapping(value = "/payment/zk")
public String paymentzk(){
return "springcloud with zookeeper:"+serverPort+"\t"+ UUID.randomUUID().toString();
}
}
-
啟動8004
jar包衝突
maven
- 修改POM
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<!--排除zk3.5.3-->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--新增zk 3.4,9版本-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
- 啟動zookeeper
[root@localhost bin]# ./zkServer.sh start
[root@localhost bin]# ./zkCli.sh
- 測試
http://localhost:8004/payment/zk
思考:服務節點是臨時節點還是持久節點
是臨時的
關閉--重啟,檢視
服務消費者
cloud-consumerzk-order80
參考cloud-provider-payment8004
-
建module
-
寫POM(參考cloud-provider-payment8004)
-
寫YML
server:
port: 80
spring:
application:
name: cloud-consumer-order
cloud:
# 註冊到zookeeper地址
zookeeper:
connect-string: 192.168.110.128
- 主啟動
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZKMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderZKMain80.class,args);
}
}
- 業務類
- bean
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
- controller
@RestController
@Slf4j
public class OrderZKController {
public static final String INVOME_URL = "http://cloud-provider-payment";
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/zk")
public String payment() {
String result = restTemplate.getForObject(INVOME_URL + "/payment/zk", String.class);
return result;
}
}
- 測試
http://localhost:8004/payment/zk
http://localhost/consumer/payment/zk
zookeeper