当前位置:网站首页>Nacos installation configuration and single-machine deployment tutorial
Nacos installation configuration and single-machine deployment tutorial
2022-08-02 06:44:00 【m0_67402564】
首先说明:This tutorial is for thatnacos版本:1.3.2,不同的版本,可能略有不同.
Springcloudalibaba的版本与Springboot的版本与nacos版本需要对应
Nacos支持三种部署模式
- 单机模式 - 用于测试和单机试用.
- 集群模式 - 用于生产环境,确保高可用.
- 多集群模式 - 用于多数据中心场景.
单机部署:
1、首先去官网下载nacos
Releases · alibaba/nacos · GitHub
The default is to start the cluster,Can be modified to stand-alone startup mode:
Windows启动脚本命令:
startup.cmd -sstandalone
nacos初体验
父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>order-nacos</module>
<module>stock-nacos</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.springcloudalibaba</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloudalibabaDemo</name>
<description>Demo project for Spring springcloudalibaba</description>
<properties>
<java.version>1.8</java.version>
<spring.cloud.alibaba.version>2.2.5.RELEASE</spring.cloud.alibaba.version>
<sping.boot.version>2.3.2.RELEASE</sping.boot.version>
<spring.cloud.version>Hoxton.SR8</spring.cloud.version>
</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>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<!--springcloudalibaba的版本管理,通过dependency继承-->
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--springboot的版本管理器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${sping.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--springcloud的版本管理器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</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>
Imported in every submodulenacos依赖
<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>
configured in each submodulenacos,端口不同
server:
port: 8021
#nacos配置
# 应用名称,nacos会将该名称当做服务名称
spring:
application:
name: stock-service
cloud:
nacos:
server-addr: 127.0.0.1:8848
discovery:
username: nacos
password: nacos #登录nacosUsername and password for the visual interface
namespace: public #命名空间
配置好nacosThe service will then be automatically registered
nacosThe service calls depend on the load balancer,默认是ribbon.在消费者端配置restTemplateThe service interface used to call the provider side
package per;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
}
@Bean
@LoadBalanced // 负载均衡器注解,nacosThe service calls rely on load balancing(nacosFailed to translate service name to service address,需要使用负载均衡器,The polling method is used by default)
public RestTemplate restTemplate(RestTemplateBuilder builder){
RestTemplate RestTemplate = builder.build();
return RestTemplate;
}
}
编写服务提供者
package per.pz.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("stock")
public class stockController {
//读取配置文件中的端口
@Value("${server.port}")
String port;
@RequestMapping("/reduct")
public String reduct(){
System.out.println("扣减库存");
return "扣减库存111"+port;
}
}
编写服务消费者
package per.pz.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("order")
public class oderController {
@Autowired
RestTemplate RestTemplate;
@RequestMapping("/add")
public String add(){
System.out.println("下单成功");
// 服务间的通讯,There is no way to use microservices
// The disadvantage of the method is that the service address is hard-coded in the code,So use the registry to solve it
// String msg = RestTemplate.getForObject("http://localhost:8021/stock/reduct",String.class);
// nacos服务调用,No need to use an address,使用服务名称即可
String msg = RestTemplate.getForObject("http://stock-service/stock/reduct",String.class);
return "下单成功111"+msg;
}
}
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在.深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小.自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前.因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担.添加下方名片,即可获取全套学习资料哦
边栏推荐
- Cyber Security Learning - Intranet Penetration 4
- leetcode每天5题-Day04
- pytorch basic operations: classification tasks using neural networks
- 虚拟现实房产展示系统提前预见未来装修效果
- DNS的解析流程
- HCIP第十七天
- BGP experiment (route reflector, federation, route optimization)
- Nacos注册中心的部署与用法详细介绍
- The virtual reality real estate display system foresees the future decoration effect in advance
- Analysis of the source code of the JS UI framework of Hongmeng system
猜你喜欢
随机推荐
从入门到精通的MySQL数据库视频教程
腾讯大咖分享 | 腾讯Alluxio(DOP)在金融场景的落地与优化实践
6W+字记录实验全过程 | 探索Alluxio经济化数据存储策略
The advantages of making web3d dynamic product display
分布式文件存储服务器之Minio对象存储技术参考指南
卸载redis
洛谷小游戏大全(用洛谷的人都得知道)
MySQL数据表的基本操作和基于 MySQL数据表的基本操作的综合实例项目
pl/sql之神奇的嵌套与变量生命周期
51 MCU Peripherals: Infrared Communication
Node的安装与环境变量的配置
Automated operation and maintenance tools - ansible, overview, installation, module introduction
leetcode-318.最大单词长度乘积
【解决】RESP.app 连接不上redis
BGP experiment (route reflector, federation, route optimization)
BGP实验(路由反射器,联邦,路由优化)
Detailed explanation of interface in Go language
Practice on optimizing startup performance of VS Code
C语言入门实战(13):十进制数转二进制
软件测试在职2年跳槽4次,你还在怪老板不给你涨薪?