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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <modules> <module>stock-nacos</module> <module>order-nacos</module> </modules> <packaging>pom</packaging> <groupId>com.lili</groupId> <artifactId>springcloud-alibaba</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springcloud-alibaba</name> <description>springcloud-alibaba</description> <properties> <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> <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> <!--SpringCloudAlibaba的版本管理--> <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、yml文件
server: port: 9001 # 服务名称 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、启动类加上以下内容
@Bean @LoadBalanced public RestTemplate getRestTemplate(RestTemplateBuilder builder){ return builder.build(); }
3、stock-nacos服务创建
3.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>
3.2、yml文件
server: port: 9002 # 服务名称 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、下载nacos
下载地址:https://github.com/alibaba/nacos/releases?q=1.4.2&expanded=true
下载完成后,右击文本打开下列文件,
由于默认nacos是集群模式,我们需要修改为单机模式
如上图,将默认得cluster
修改为standalone
即可、保存退出
5、启动nacos
启动方式:双击startup.cmd
即可,如下图所示。
访问后默认的账号和密码都是nacos
,登陆即可
6、启动order-nacos和stock-nacos
正常启动:
观察nacos客户端的情况:
发现成功注册进来了
7、服务发现
访问写好的接口即可
成功访问到stock-nacos
服务里面的内容。