zookeeper是一个分布式协调工具,可以实现注册中心功能
关闭Linux服务器防火墙后启动zookeeper服务器
zookeeper服务器取代Eureka服务器,zk作为服务注册中心
安装具体可以参考笔者之前写的这篇文章
一、Linux下zookeeper环境搭建
编写yml文件mscloud com.zsy.springcloud 1.0-SNAPSHOT 4.0.0 com.zsy.springcloud cloud-provider-payment80041.0-SNAPSHOT org.springframework.boot spring-boot-starter-webcom.zsy.springcloud cloud-api-commins${project.version} org.springframework.cloud spring-cloud-starter-zookeeper-discoveryorg.springframework.boot spring-boot-devtoolsruntime true org.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest
在resource目录下新建一个application.yml的文件
#8004表示注册到zookeeper服务器的支付服务提供者端口号
server:
port: 8004
#服务别名----注册zookeeper到注册中心名称
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: 192.168.111.144:2181
编写主启动类
@SpringBootApplication
@EnableDiscoveryClient//该注解用于向使用consul或者zookeeper作为注册中心时注册服务
public class PaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8004.class,args);
}
}
服务提供者controller
package com.zsy.springCloud.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
@RestController
public class PaymentController
{
@Value("${server.port}")
private String serverPort;
@RequestMapping(value = "/payment/zk")
public String paymentzk()
{
return "springcloud with zookeeper: "+serverPort+"t"+ UUID.randomUUID().toString();
}
}
启动8004注册进zookeeper
修改pom解决zk依赖冲突的问题
org.springframework.cloud
spring-cloud-starter-zookeeper-discovery
org.apache.zookeeper
zookeeper
org.apache.zookeeper
zookeeper
3.4.9
浏览器输入验证并测试
http://localhost:8004/payment/zkzk操作节点查看znode信息
注意zk这个节点是临时节点,我们可以让服务上下线验证问题
get /services/cloud-provider-payment/fd3cae3a-61d6-4c14-ad57-ed471ccc97d3编写服务消费者 新建cloud-consumerzk-order80 编写pom
编写yml文件mscloud com.zsy.springcloud 1.0-SNAPSHOT 4.0.0 com.zsy.springcloud cloud-consumerzk-order801.0-SNAPSHOT org.springframework.boot spring-boot-starter-weborg.springframework.cloud spring-cloud-starter-zookeeper-discoveryorg.apache.zookeeper zookeeperorg.apache.zookeeper zookeeper3.4.9 org.springframework.boot spring-boot-devtoolsruntime true org.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest
server:
port: 80
spring:
application:
name: cloud-consumer-order
cloud:
#注册到zookeeper地址
zookeeper:
connect-string: 192.168.111.144:2181
编写主启动类
package com.zsy.springCloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class OrderZK80 {
public static void main(String[] args) {
SpringApplication.run(OrderZK80.class, args);
}
}
配置一个服务调用的测试bean
public class ApplicationContextBean {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate()
{
return new RestTemplate();
}
}
编写消费者controller
@RestController
public class OrderZKController {
private static final String INVOKE_URL="http://cloud-provider-payment";
@Autowired
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/zk")
public String paymentInfo(){
String result=restTemplate.getForObject(INVOKE_URL+"/payment/zk",String.class);
result="从生产者得到的数据:"+result;
return result;
}
}
地址访问测试
http://localhost/consumer/payment/zk源码地址
https://gitee.com/fugongliudehua/mscloud



