当前位置:网站首页>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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担.添加下方名片,即可获取全套学习资料哦
边栏推荐
猜你喜欢

Nacos注册中心的部署与用法详细介绍

51 MCU peripherals: ADC

Practice on optimizing startup performance of VS Code

leetcode solves the linked list merge problem in one step

国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐

使用TinkerPop框架对GDB增删改查

Features and installation of non-relational database MongoDB

Polar Parametrization for Vision-based Surround-View 3D Detection Paper Notes

51 MCU peripherals: DS18B20

面试官:设计“抖音”直播功能测试用例吧
随机推荐
leetcode每天5题-Day04
排雷小游戏(C语言)
Meta公司新探索 | 利用Alluxio数据缓存降低Presto延迟
Thread Basics (1)
Automated operation and maintenance tools - ansible, overview, installation, module introduction
MySQL联合查询(多表查询)
Introduction to coredns
Contents of encoding-indexes.js file printed with Bluetooth:
rhce作业
Guarantee WIFI security in home and enterprise-with AC and AP networking experiment
聪明人的游戏提高篇:第三章第二课:“桐桐数”(number)
Redis集群模式
selenium + robotframework的运行原理
eggjs controller层调用controller层解决方案
Nacos客户端启动出现9848端口错误分析(非版本升级问题)
人工神经网络
Redis-cluster mode (master-slave replication mode, sentinel mode, clustering mode)
Nacos数据库配置
About the directory structure of the web application
NPM 安装指定版本包的方法及版本号查看