目录
4.4、application.properties配置文件?
5.4、application.properties配置文件?
Nacos源码地址:https://github.com/alibaba/nacos
使用git clone https://github.com/alibaba/nacos命令将源码clone下来,或者先fork到自己的GitHub,然后clone到本地,这样阅读的时候,可以添加一些注释,提交到自己的GitHub上面,方便后续再次回来查看时快速回想起代码的含义。
截止到编写这篇文章,Nacos的最新版本为2.3.0,为了学习,我们也是拿最新的源码来学习。
直接通过IDEA提供的Maven可视化工具进行编译:
当然也可以进入到Nacos源码目录,执行下述的命令进行编译:
mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U
Nacos服务端的启动类:com.alibaba.nacos.Nacos
@SpringBootApplication
@ComponentScan(basePackages = "com.alibaba.nacos", excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = {NacosTypeExcludeFilter.class}),
@Filter(type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class}),
@Filter(type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class})})
@ServletComponentScan
@EnableScheduling
public class Nacos {
public static void main(String[] args) {
SpringApplication.run(Nacos.class, args);
}
}
我们需要设置Nacos为单机节点启动,通过修改运行时的JVM参数:-Dnacos.standalone=true
直接运行Nacos启动类的main方法:
通过控制台,我们可以看到Nacos已经成功启动,控制台界面地址:http://172.110.0.138:8848/nacos/index.html,默认端口号是8848,默认账号密码都是nacos,直接访问控制台页面:
为了更好的调试Nacos的服务注册、服务发现功能,我们需要搭建一个服务提供者和服务消费者。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>discovery-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>discovery-provider</name>
<description>discovery-provider</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-discovery -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.9.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@SpringBootApplication
@EnableDiscoveryClient
public class DiscoveryProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DiscoveryProviderApplication.class, args);
}
}
@RestController
@RequestMapping("/provider")
public class DiscoveryProviderController {
@RequestMapping(value = "/hello/{text}", method = RequestMethod.GET)
public String hello(@PathVariable(value = "text") String text) {
return "Nacos is coming :" + text;
}
}
server.port=1001
spring.application.name=discovery-provider
spring.cloud.nacos.discovery.server-addr=localhost:8848
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
spring.cloud.nacos.discovery.namespace=public
可以看到,服务提供者discovery-provider已经成功注册到Nacos中。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>discovery-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>discovery-consumer</name>
<description>discovery-consumer</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-discovery -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.9.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@SpringBootApplication
// 开启服务注册功能
@EnableDiscoveryClient
public class DiscoveryConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DiscoveryConsumerApplication.class, args);
}
}
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
server.port=1002
spring.application.name=discovery-consumer
spring.cloud.nacos.discovery.server-addr=localhost:8848
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
spring.cloud.nacos.discovery.namespace=public
@RestController
@RequestMapping("/consumer")
public class DiscoveryConsumerController {
@Autowired
private RestTemplate restTemplate;
@GetMapping(value = "/hello/{text}")
public String hello(@PathVariable(value = "text") String text) {
return restTemplate.getForObject("http://discovery-provider/provider/hello/" + text, String.class);
}
}
可以看到,服务提供者discovery-consumer已经成功注册到Nacos中。
浏览器访问:http://localhost:1002/consumer/hello/helloword
至此,Nacos源码阅读的环境就搭建好了,后续我们会进行服务注册、服务发现、以及服务心跳等功能进行分析。