SpringCloud之使用Zookeeper作为注册中心
linux安装zookeeper
安装zookeeper
- 关闭linux防火墙
- 启动zookeeper
1 创建项目导入依赖和配置文件
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">
mscloud03
com.atguigu.springcloud
1.0-SNAPSHOT
4.0.0
cloud-provider-payment8004
org.springframework.boot
spring-boot-starter-web
com.atguigu.springcloud
cloud-api-commons
${project.version}
org.springframework.cloud
spring-cloud-starter-zookeeper-discovery
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
2 编写yaml
#8004表示注册到zookeeper服务器的支付服务提供者端口号
server:
port: 8004
#服务别名----注册zookeeper到注册中心名称
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: 192.168.111.144:2181 # 您的linux服务器地址 ifconfig查看
3 编写启动类
@SpringBootApplication
@EnableDiscoveryClient //该注解用于向使用consul或者zookeeper作为注册中心时注册服务
public class PaymentMain8004
{
public static void main(String[] args)
{
SpringApplication.run(PaymentMain8004.class,args);
}
}
4 controller
@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();
}
}
出现的问题:
解决zookeeper版本jar包冲突问题
- zk 版本和springcloud的zk版本不一致
- 一般采用排除自带的zk版本 使用自己的zk
修改pom
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">
cloud2022
com.atguigu.springcloud
1.0-SNAPSHOT
4.0.0
cloud-provider-payment8004
org.springframework.boot
spring-boot-starter-web
com.atguigu.springcloud
cloud-api-commons
${project.version}
org.springframework.cloud
spring-cloud-starter-zookeeper-discovery
org.apache.zookeeper
zookeeper
org.apache.zookeeper
zookeeper
3.4.9
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
5 测试
http://localhost:8004/payment/zk
查看服务的所有信息