springcloud微服务学习笔记目录:
springcloud微服务学习笔记
zookeeper代替eureka
1.要求:在centos上已安装zookeeper
安装教程:https://www.cnblogs.com/huangjianping/p/8012580.html
2.环境准备
1.关闭防火墙
systemctl stop firewalld
2.查看linux的ip地址
ifconfig
3.linux端和windows端互相ping通
3.编码实现
3.1 新建cloud-provider-payment8004
3.2 pom文件
<dependencies>
<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.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<!-- 排除自带的的zookeeper-->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 添加zookeeper3.4.9版本-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</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>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
3.3 yml文件
server:
port: 8004
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: 192.168.153.129
3.4 主启动类
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
//该注解用于向使用consul或者zookeeper作为注册中心时注册服务
@EnableDiscoveryClient
public class PaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8004.class,args);
}
}
3.5 controller
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@RequestMapping("/payment/zk")
public String paymentZk(){
return serverPort+"\t"+ UUID.randomUUID().toString();
}
}
3.6 启动zookeeper
zkServer.sh start
3.7连接服务端
./zkCli.sh
如果出现-bash: ./zkCli.sh: 没有那个文件或目录错误
运行zkCli.sh -server localhost:2181则可成功连接
3.6 启动8004注册进zookeeper
如果没有做jar包排除,会出现jar冲突问题,在pom文件中排除自带的jar包,引入3.4.9的zookeeper版本即可解决问题
3.7查看服务
默认只有zookeeper服务
查看注册进去的服务
ls /services
地址:http://localhost:8004/payment/zk
测试成功