Spring Cloud学习教程之DiscoveryClient的深入探究

时间:2021-05-20

前言

当我们使用@DiscoveryClient注解的时候,会不会有如下疑问:它为什么会进行注册服务的操作,它不是应该用作服务发现的吗?下面我们就来深入的探究一下其源码。

一、Springframework的LifeCycle接口

要搞明白这个问题我们需要了解一下这个重要的接口:

/* * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://positeDiscoveryClientAutoConfiguration.class})public class MemberApplication { public static void main(String[] args) { ConfigurableApplicationContext applicationContext = SpringApplication.run(MemberApplication.class, args); DiscoveryClient discoveryClient = applicationContext.getBean(DiscoveryClient.class); discoveryClient.getServices().forEach(action -> { System.out.println(action); }); }}

这里在SpringbootApplication注解里排除DiscoveryClient的默认装配。

当我们启动成功后可以发现,控制台已经输出对应的服务名称与地址:

我们再次通过gradle打包生成jar文件并运行:

java -jar member-server-0.0.1-SNAPSHOT.jar --server.port=8800

我们可以看到redis里已经缓存的有服务注册的值了:

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章