当前位置:网站首页>Nacos做注册中心使用
Nacos做注册中心使用
2022-07-27 09:21:00 【Charge8】
上一篇了解了 Nacos并安装了 nacos-server服务。接下来简单搭建一个微服务项目,使用 Nacos做注册中心。
一、项目搭建
项目版本选型:
Spring Cloud Alibaba版本选型:https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
这里选择:

1、创建父模块
在 pom.xml中管理版本。
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
<spring-cloud.version>Hoxton.SR12</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.8.RELEASE</spring-cloud-alibaba.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>
<dependencies>
<!-- spring-cloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring-cloud-alibaba-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>
</dependencies>
</dependencyManagement>
2、创建order服务
配置服务提供者 order服务,服务提供者可以通过 Nacos 的服务注册发现功能将其服务注册到 Nacos server 上。
2.1 引入依赖
当前项目pom中引入nacos依赖
<!-- nacos服务注册与发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
2.2 配置文件
配置nacos注册中心
server:
port: 18082
servlet:
context-path: /
spring:
application:
name: app-order #微服务名称
#配置nacos注册中心地址
cloud:
nacos:
discovery:
server-addr: 192.168.xxx.xxx:8848
#namespace: f16bcf35-810d-4b5b-a936-bd2abb8582a3
#cluster-name: SH

更多配置:https://github.com/alibaba/spring-cloud-alibaba/wiki/Nacos-discovery
3、创建user服务
配置服务消费者user服务,服务消费者可以通过 Nacos 的服务注册发现功能从 Nacos server 上获取到它要调用的服务。
3.1 引入依赖
当前项目pom中引入依赖
<!-- nacos服务注册与发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
3.2 配置文件
配置nacos注册中心
server:
port: 18081
servlet:
context-path: /app-user
spring:
application:
name: app-user #微服务名称
#配置nacos注册中心地址
cloud:
nacos:
discovery:
server-addr: 192.168.xxx.xxx:8848
#namespace: f16bcf35-810d-4b5b-a936-bd2abb8582a3
#cluster-name: SH
3.3 使用RestTemplate进行服务调用
1)开启 @LoadBalanced 与 Ribbon 的集成。
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
2)使用 RestTemplate进行服务调用
写一个 controller调用。
@RequestMapping(value = "/findOrderByUserId/{id}")
public R findOrderByUserId(@PathVariable("id") Integer id) {
log.info("根据userId:" + id + "查询订单信息");
String url = "http://app-order/order/findOrderByUserId/" + id;
R result = restTemplate.getForObject(url, R.class);
return result;
}
4、启动user和order服务
在 nacos管理端界面查看是否成功注册。

在 nacos管理端,也可以在指定一些配置,比如,权重,分组,集群等。

5、测试访问user服务
浏览器访问测试controller,测试服务调用ok。
到此,Nacos做注册中心使用就OK了。Namespace 和 Group配置项使用场景也是蛮多的,自行了解。
– 求知若饥,虚心若愚。
边栏推荐
- The difference between computed and watch
- Music experience ceiling! Emotional design details of 14 Netease cloud music
- 【CTF】ciscn_ 2019_ es_ two
- [C language - zero foundation lesson 11] rotate the pointer of the big turntable
- [daily algorithm day 96] Tencent interview question: merge two ordered arrays
- Community attribute of BGP
- 工程材料知识点总结(全)
- ES6 new - function part
- The fifth day of learning C language
- 【CTF】ciscn_2019_es_2
猜你喜欢

C# 窗体应用常用基础控件讲解(适合萌新)

As a VC, the auction house invested Web3 for the first time

NCCL (NVIDIA Collective Communications Library)

ArcGIS pro2.8 deep learning environment configuration based on rtx30 graphics card

JS call and apply

Cross domain and processing cross domain

You haven't heard of such 6 question brushing websites, have you? Are you out?

Analog library function

NCCL 集合通信--Collective Operations

The whole process of principle, simulation and verification of breath lamp controlled by FPGA keys
随机推荐
《工程测量学》考试复习总结
SQL exercise set
ES6 new - string part
网易笔试之解救小易——曼哈顿距离的典型应用
命令提示符启动不了mysql,提示发生系统错误 5。拒绝访问。解决办法
Day 6 of learning C language
[acl2020] a novel method of component syntax tree serialization
七月集训(第15天) —— 深度优先搜索
【云驻共创】华为云:全栈技术创新,深耕数字化,引领云原生
Easy language programming: allow the screen reading software to obtain the text of the label control
C language takes you to tear up the address book
七月集训(第13天) —— 双向链表
Pymongo fuzzy query
ES6 new - deconstruction assignment of array / object
【无标题】
JS call and apply
IDL 6S lookup table
C language exercise 2
DNS domain name space
【云原生】我怎么会和这个数据库杠上了?