前言
当我们使用@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里已经缓存的有服务注册的值了:
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。