SpringCloud~~~【Zookeeper】
搭建payment8004服务
application.yml
#8004表示注册到zookeeper服务器的支付服务提供者端口号
server:
port: 8004
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: 172.16.231.139:2181
启动类:
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8004.class,args);
}
}
搭建Zookeeper服务器
选用的是zookeeper3.4.11
ip地址:172.16.231.139
端口:2181
关闭服务器防火墙(CentOS7)
systemctl status firewalld 查看防火墙状态
systemctl stop firewalld 关闭防火墙
systemctl disable firewalld 设置开机不开启防火墙
开启zookeeper:
./ZkServer.sh start
解决zookeeper版本冲突问题
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper- discovery</artifactId>
<!--先排除自带的zookeeper3.5.3-->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加zookeeper3.4.11版本-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.11</version>
</dependency>
成功显示:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zoRX26sL-1607913536870)(en-resource://database/2123:1)]
搭建消费者注册进zookeeper
创建项目consumerzk-order80
application.yml
server:
port: 80
spring:
application:
name: cloud-consumer-order
cloud:
#注册到zookeeper地址
zookeeper:
connect-string: 172.16.231.139:2181
config:
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate()
{
return new RestTemplate();
}
}
基本与Eureka类似
看一下服务器端的情况
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Nfx2FC1b-1607913536873)(en-resource://database/2228:1)]
可以看出两个服务都注册进了zookeeper
但是是临时节点。
controller层:
@RestController
@Slf4j
public class OrderZKController {
public static final String INVOKE_URL = "http://cloud-provider-payment";
@Resource
private RestTemplate restTemplate;
@GetMapping(value = "/consumer/payment/zk")
public String paymentInfo(){
String result = restTemplate.getForObject(INVOKE_URL + "/payment/zk", String.class);
return result;
}
}