通过本文,学会如何确定项目组件版本(减少可能出现的jar包冲突),nacos的注册与发现及其源码执行流程。
重点如下:
**总结:**spring cloud 2021.0.5 、spring-cloud-alibaba 2021.0.5.0、spring-boot 2.6.13、nacos 2.2.0(因为本地mac系统版本低报错)、sentinel 、rocket mq 、seata 会根据约束自动加载。
nacos源码搭建将使用
nacos 2.0.0
版本,
pom.xml 重要配置如下:
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.6.13version>
parent>
<properties>
<maven.compiler.source>11maven.compiler.source>
<maven.compiler.target>11maven.compiler.target>
<spring-cloud.version>2021.0.5spring-cloud.version>
<spring-cloud-alibaba.version>2021.0.5.0spring-cloud-alibaba.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>${spring-cloud-alibaba.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
main 方法
@SpringBootApplication
@EnableDiscoveryClient
public class NacosRegistryProviderApplication {
public static void main(String[] args) {
SpringApplication.run(NacosRegistryProviderApplication.class);
}
}
controller
@RestController
public class NacosRegistryProviderController {
@GetMapping("/hello/{id}")
public String echo(@PathVariable String id) {
return "NacosProvider receive args: id=" + id + ",date:"
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
}
}
配置文件
spring:
application:
name: nacos-provider
cloud:
nacos:
discovery:
server-addr: 10.xx.xx.142
server:
port: 9010
pom.xml 配置
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-bootstrapartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
服务订阅 与 服务注册案例基本上配置相同
main 方法
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApplication.class);
}
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
controller
@RestController
public class NacosDiscoveryConsumerController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/hello/{id}")
public String echo(@PathVariable String id) {
return restTemplate.getForObject("http://nacos-provider/hello/" + id, String.class);
}
}
pom.xml
注意加上一个jar包,否则不能识别
微服务
名
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-loadbalancerartifactId>
dependency>
注意: 为了解决其它流程影响正常 debug
源码,参考以下断点。通过下面关键断点,可以避开其它流程的影响,然后查看堆栈,基本可以理清 nacos
源码注册流程。
org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration#onApplicationEvent
com.alibaba.cloud.nacos.registry.NacosServiceRegistry#register
源码阅读是有一定的技巧的,在以后的文章中会慢慢讲述,今天讲述一个报错查看 nacos
源码注册流程。
org.springframework.boot.SpringApplication.run(SpringApplication.java:1317) ~[spring-boot-2.6.13.jar:2.6.13]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-2.6.13.jar:2.6.13]
at com.fun.ms.NacosRegistryProviderApplication.main(NacosRegistryProviderApplication.java:11) ~[classes/:na]
Caused by: java.lang.reflect.UndeclaredThrowableException: null
at org.springframework.util.ReflectionUtils.rethrowRuntimeException(ReflectionUtils.java:147) ~[spring-core-5.3.23.jar:5.3.23]
at com.alibaba.cloud.nacos.registry.NacosServiceRegistry.register(NacosServiceRegistry.java:83) ~[spring-cloud-starter-alibaba-nacos-discovery-2021.0.5.0.jar:2021.0.5.0]
at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.registerService(NamingGrpcClientProxy.java:123) ~[nacos-client-2.2.0.jar:na]
at com.alibaba.nacos.client.naming.remote.NamingClientProxyDelegate.registerService(NamingClientProxyDelegate.java:98) ~[nacos-client-2.2.0.jar:na]
at com.alibaba.nacos.client.naming.NacosNamingService.registerInstance(NacosNamingService.java:152) ~[nacos-client-2.2.0.jar:na]
at com.alibaba.cloud.nacos.registry.NacosServiceRegistry.register(NacosServiceRegistry.java:75) ~[spring-cloud-starter-alibaba-nacos-discovery-2021.0.5.0.jar:2021.0.5.0]
... 27 common frames omitted
nacos
注册与发现及源码注册流程至此就结束了,如有疑问,欢迎评论区留言。