六、Zookeeper服务注册与发现
1)搭建zookeeper服务并启动
这里就不再赘述了,直接去我的博客下看zookeeper在linux上的搭建
2) 支付服务注册进zookeeper
步骤如下:
项目结构:
① 添加依赖
【注意】spring-cloud-starter-zookeeper-discovery中自带了 zookeeper3.5.3
如果自己服务器上zookeeper的版本不是zookeeper3.5.3,则需要排除该依赖
<dependencies> <!-- SpringBoot整合Web组件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency><!-- 引入自己定义的api通用包,可以使用Payment支付Entity --> <groupId>com.atguigu.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>
② 配置文件
#8004 表示注册到zookeeper的支付服务提供者的端口号 server: port: 8004 spring: application: name: cloud-provider-payment cloud: zookeeper: connect-string: 172.20.10.14:2181
③ 主启动类
@SpringBootApplication @EnableDiscoveryClient //使用consul或者zookeeper注册中心时,向其注册服务 public class PaymentMain8004 { public static void main(String[] args) { SpringApplication.run(PaymentMain8004.class,args); } }
④ controller
@RestController @Slf4j public class PaymentController { @Value("${server.port}") private String serverPort; @RequestMapping(value = "/payment/zk") public String paymentzk(){ return "springcloud with zookeeper:"+ serverPort+ " "+ UUID.randomUUID().toString(); } }
⑤ 启动 该服务提供方,测试 : http://localhost:8004/payment/zk
连上zookeeper客户端
./zkCli.sh --> ls / ---> ls /services
查看注册服务的相关信息,获得一个流水号
查看zookeeperj节点的相关信息
当cloud-provider-payment服务停掉时,zookeeper会剔除相应的服务节点
3) 订单服务注册进zookeeper
项目结构
①、添加依赖
<dependencies> <!-- SpringBoot整合Web组件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- SpringBoot整合zookeeper客户端 --> <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> </dependencies>
②、编写配置文件 application.xml
#8004 表示注册到zookeeper的支付服务提供者的端口号 server: port: 80 spring: application: name: cloud-consumer-order cloud: zookeeper: connect-string: 172.20.10.14:2181
③、编写主配置类
@SpringBootApplication @EnableDiscoveryClient public class OrderZkMain80 { public static void main(String[] args) { SpringApplication.run(OrderZkMain80.class,args); } }
④、注入restTemplate
@Configuration public class ApplicationContextConfig { @Bean //一定要加这个注解,因为远程调用的地址写的是 服务名 // http://cloud-provider-payment ,不加会报错呀!!!! @LoadBalanced public RestTemplate getRestTemplate(){ return new RestTemplate(); } }
⑤、controller
@RestController @Slf4j public class OrderZkController { private static final String INVOKE_URL = "http://cloud-provider-payment" ; @Autowired private RestTemplate restTemplate; @GetMapping(value = "/consumer/payment/zk") public String paymentInfo(){ String result = restTemplate.getForObject(INVOKE_URL+"/payment/zk", String.class); return result; } }
⑥ 启动服务后:
进入zookeeper客户端查看: 发现cloud-consumer-order 已经注册进zookeeper.
访问:http://localhost/consumer/payment/zk
访问成功,说明zookeeper确实能够代替eureka称为注册中心