目录
1.启动zookeeper❤️❤️❤️
2.创建8004模块 ❤️❤️❤️
3.临时节点还是永久节点❤️❤️❤️
4.创建zk80消费模块❤️❤️❤️
./zkServer.sh start 和 ./zkCli.sh -server 主机名
在父工程下创建模块
注意:jdk版本和,maven版本
1.springboot依赖
2.mybatis依赖
3.druid依赖
4.mysql依赖
5.zookeeper依赖
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.mybatis.spring.boot
mybatis-spring-boot-starter
com.alibaba
druid-spring-boot-starter
1.2.16
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.example
cloud-api-commons
${project.version}
org.springframework.cloud
spring-cloud-starter-zookeeper-discovery
1.服务端口
2.服务名称
3.数据库连接池
4.zookeeper连接地址
5.mybatis配置
server:
port: 8004
spring:
application:
#服务名称
name: cloud-provider-payment
datasource:
#druid连接池
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springcloud
username: root
password: 123456
#zookeeper服务注册中心
cloud:
zookeeper:
connect-string: 192.168.20.129:2181,192.168.20.130:2181,192.168.20.131:2181
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.xz.springcloud.entity
@EnableDiscoveryClient:将服务注册到服务注册中心(zookeeper)
@SpringBootApplication
@EnableDiscoveryClient//作为服务中心
public class PaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8004.class);
}
}
@RestController
public class PaymentController {
@Autowired
private PaymentService paymentService;
@Value("${server.port}")
private String serverPort;
@GetMapping("/payment/getById/{id}")
public CommonResult getById(@PathVariable("id") Integer id){
Payment payment = paymentService.getPaymentById(id);
if (payment!=null){
return new CommonResult(200,"查询成功,serverPort:"+serverPort,payment);
}else {
return new CommonResult(404,"查询无果,serverPort:"+serverPort,null);
}
}
@RequestMapping("/payment/zk")
public String paymentZk(){
return "springCloud with zookeeper:"+serverPort+"\t"+ UUID.randomUUID().toString();
}
}
当关闭8004服务时,在查询zookeeper,发现节点剔除
所以:zookeeper存储的是临时节点,当服务消失时,会马上剔除
在父工程下创建模块
注意jdk版本和maven版本
1.springboot依赖
2.通用依赖
3.zookeeper依赖
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.example
cloud-api-commons
${project.version}
org.springframework.cloud
spring-cloud-starter-zookeeper-discovery
1.服务端口
2.服务名
3.zookeeper连接ip(我这里使用集群)
server:
port: 80
spring:
application:
name: cloud-consumer-order
cloud:
zookeeper:
connect-string: 192.168.20.129:2181,192.168.20.130:2181,192.168.20.131:2181
@EnableDiscoveryClient添加上
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZkMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderZkMain80.class);
}
}
注意:因为访问的是服务名,而不是具体的ip,所以加上@LoadBalance注解
@Configuration
public class ApplicationConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
1.声明常量,表示zookeeper中8004的服务名
2.注入RestTemplate
3.调用restTemplate访问
@RestController
public class OrderController {
public static final String INVOKE_URL = "http://cloud-provider-payment";
@Autowired
private RestTemplate restTemplate;
@GetMapping("/payment/getById/{id}")
public CommonResult getPayment(@PathVariable("id") Integer id){
return restTemplate.getForObject(INVOKE_URL+"/payment/getById/"+id,CommonResult.class);
}
@GetMapping("/payment/zk")
public String getDiscovery(){
return restTemplate.getForObject(INVOKE_URL+"/payment/zk",String.class);
}
}