当前位置:网站首页>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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担.添加下方名片,即可获取全套学习资料哦
边栏推荐
- 排雷小游戏(C语言)
- Leetcode parentheses matching problem -- 32. The longest parentheses effectively
- Nacos安装详细过程
- The virtual reality real estate display system foresees the future decoration effect in advance
- 51 microcontroller peripherals article: dot-matrix LCD
- C语言入门实战(13):十进制数转二进制
- APP Bluetooth connection test of test technology
- leetcode一步解决链表合并问题
- 人工神经网络
- 虚拟现实房产展示系统提前预见未来装修效果
猜你喜欢
随机推荐
Nacos注册中心的部署与用法详细介绍
TikTok平台的两种账户有什么区别?
leetcode一步解决链表合并问题
从入门到精通的MySQL数据库视频教程
NPM 安装指定版本包的方法及版本号查看
C语言操作符详解(2)
DNS的解析流程
selenium + robotframework的运行原理
kubernetes affinity, anti-affinity, taint, tolerance
APT + Transform 实现多模块应用Application生命周期分发
Guarantee WIFI security in home and enterprise-with AC and AP networking experiment
Linux CentOS8安装Redis6
leetcode每天5题-Day04
Redis database
Redis(十二) - Redis消息队列
为什么4个字节的float要比8个字节的long大呢?
nacos registry
秒杀系统小demo
聪明人的游戏提高篇:第三章第二课:“桐桐数”(number)
点云旋转到参考坐标系方向(最小方向包围盒方法)