前言
SpringCloud使用ZooKeeper作为服务注册,主要用于纪录部署过程。
一、VMware安装和ZooKeeper环境搭建
-
环境搭建
- VMware Workstation15虚拟机安装教程
- CentOS 7镜像下载 (我安装的是精简版的)
- VMware安装Centos7超详细过程(图文)
- centos7 安装jdk1.8环境
-
# centos7上安装zookeeper
-
启动ZooKeeper
- 首先关闭CentOS的防火墙
//关闭防火墙
[root@localhost bin]# systemctl stop firewalld
//查看状态
[root@localhost bin]# systemctl status firewalld
- 进入ZooKeeper的bin目录
//启动ZooKeeper
[root@localhost bin]# ./zkServer.sh start
//查看状态
[root@localhost bin]# ./zkServer.sh status
//启动ZooKeeper客户端
[root@localhost bin]# ./zkCli.sh
//查看ZooKeeper服务
[zk: localhost:2181(CONNECTED) 0] ls /
//查看注册进ZooKeeper的服务
[zk: localhost:2181(CONNECTED) 1] ls /services
二、服务提供者
-
pom文件
<dependencies>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<groupId>com.huzh.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!-- SpringBoot整合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.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>
</dependencies>
-
yml文件
#8004表示注册到zookeeper服务器的服务提供者端口号
server:
port: 8004
#服务别名----注册zookeeper到注册中心名称
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: 192.168.118.128:2181
-
启动类
@SpringBootApplication
@EnableDiscoveryClient //该注解用于向使用consul或者zookeeper作为注册中心时注册服务
public class PaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8004.class, args);
}
}
-
controller文件
只提供controller供测试
@RestController
@Slf4j
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();
}
}
三、服务调用者
-
pom文件
通服务提供者
-
yml文件
server:
port: 80
spring:
application:
name: cloud-consumer-order
cloud:
#注册到zookeeper地址
zookeeper:
connect-string: 192.168.118.128:2181
-
启动类
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZKMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderZKMain80.class, args);
}
}
-
配置文件
配置Rest组件配置
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
-
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;
}
}