前提准备:IDEA2021.3 ,nacos1.4.2
本文nacos基于Windows环境
nacos1.4.2下载
下载地址:https://github.com/alibaba/nacos/releases?q=1.4.2&expanded=true
下载1.4.2zip 不要下错了。
下载完成后打开文件夹,进入bin目录,点击startup.cmd用文本打开
由于nacos默认是集群模式,所以我们要修改为单击模式。
将默认cluster改为standalone 退出保存即可。
然后就可以启动nacos了
nacos启动
双击startup.cmd启动 , 复制蓝色划线的进行访问
默认的账号和密码都是nacos,登陆即可
1,创建一个maven项目,引入相关依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yss</groupId>
<artifactId>springcloud-Alibaba</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>order-nacos</module>
<module>stock-nacos</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<java.version>1.8</java.version>
<spring.cloud.alibaba>2.2.6.RELEASE</spring.cloud.alibaba>
<spring.boot.starter>2.3.2.RELEASE</spring.boot.starter>
<spring.cloud.dependency>Hoxton.SR9</spring.cloud.dependency>
</properties>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--springboot版本管理-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring.boot.starter}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--SpringCloud的版本管理-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.dependency}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2,order-nacos服务模块
2.1,pom所需要的依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--nacos 服务 注册与发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
2.2 yaml文件配置
server:
port: 9002
# 服务名称
spring:
application:
name: order-server
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
username: nacos
password: nacos
namespace: public
2.3,编写controller层
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
RestTemplate restTemplate;
@GetMapping("/orderList")
public String getOrder() {
System.out.println("订单");
String message = restTemplate.getForObject("http://stock-server/stock/stockList", String.class);
return "order" + message;
}
}
2.4新建配置包config,将其用注解标记为配置类。
@Configuration
public class ConfigTemplate {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(RestTemplateBuilder builder){
return builder.build();
}
}
3,stock-nacos服务创建
3.1,添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--nacos 服务 注册与发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
3.2 yaml文件编写
server:
port: 9003
# 服务名称
spring:
application:
name: stock-server
cloud:
nacos:
discovery:
namespace: public
username: nacos
password: nacos
server-addr: 127.0.0.1:8848
3.3 controller层编写映射路径
@RestController
@RequestMapping("/stock")
public class StockController {
@Value("${server.port}")
String value;
@RequestMapping("/stockList")
public String getStock(){
System.out.println("库存");
return "扣减库存"+value;
}
}
4,启动order-nacos和stock-nacos即可
项目大体目录结构如下:
只有order-nacos和stock-nacos模块
然后查看可视化界面平台
发现服务成功注册进来。
最后访问接口
远程调用stock服务成功。到此初步的一个nacos服务注册与发现完成了!