李林超博客
首页
归档
留言
友链
动态
关于
归档
留言
友链
动态
关于
首页
Java
正文
02.Nacos作为服务注册中心演示
Leefs
2023-01-04 PM
1782℃
0条
[TOC] ### 前言 **版本关系** + **Spring Cloud Alibaba Version**:`2.2.9.RELEASE` + **Spring Cloud Version**:`Spring Cloud Hoxton.SR12` + **Spring Boot Version**:`2.3.12.RELEASE` **组件版本关系** + **Sentinel Version**:`1.8.5` + **Nacos Version**:`2.1.0` + **RocketMQ Version**:`4.9.4` + **Seata Version**:`1.5.2` 这个版本组合是目前找到的最新的版本组合。 本人使用的为IDEA 2022.3版本,有的地方和之前版本的有所不同。 ### 一、Nacos注册中心示意图  **注册中心主要有三部分组成**: + **Nacos-Server**:注册中心,提供服务的注册和发现。 + **Nacos-Provider**:服务提供方,把自身的服务实例注册到Nacos Server中。 + **Nacos-Consumer**:服务调用方,通过Nacos Server获取服务列表,消费服务。 ### 二、基于Nacos的服务提供者 #### 2.1 父POM依赖 ```xml
1.8
UTF-8
8
8
4.12
1.2.17
1.18.24
8.0.22
1.1.22
2.1.3
org.springframework.boot
spring-boot-dependencies
2.3.12.RELEASE
pom
import
org.springframework.cloud
spring-cloud-dependencies
Hoxton.SR12
pom
import
com.alibaba.cloud
spring-cloud-alibaba-dependencies
2.2.9.RELEASE
pom
import
mysql
mysql-connector-java
${mysql.version}
com.alibaba
druid
${druid.version}
org.mybatis.spring.boot
mybatis-spring-boot-starter
${mybatis.spring.boot.version}
junit
junit
${junit.version}
log4j
log4j
${log4j.version}
org.projectlombok
lombok
${lombok.version}
true
``` #### 2.2 建moudle `provider-nacos-demo01` #### 2.3 本模块pom ```xml
4.0.0
com.lilinchao
SpringCloudAlibabaDemo
0.0.1-SNAPSHOT
provider-nacos-demo01
8
8
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
``` #### 2.4 修改配置文件 ```yaml server: port: 9001 spring: application: name: provider-nacos-demo01 cloud: nacos: discovery: server-addr: localhost:8848 #配置Nacos地址,注册到Nacos # 做监控需要把这个全部暴露出来 management: endpoints: web: exposure: include: '*' ``` #### 2.5 主启动类 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class ProviderNacosDemo01Application { public static void main(String[] args) { SpringApplication.run(ProviderNacosDemo01Application.class, args); } } ``` #### 2.6 业务类 ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class NacosDemoController { @Value("${server.port}") private String serverPort; @GetMapping(value = "/provider/nacos/{id}") public String getPayment(@PathVariable("id") Integer id) { return "nacos registry, serverPort: "+ serverPort+"\t id"+id; } } ``` #### 2.7 测试 + 启动`provider-nacos-demo01`服务 + 在nacos服务列表中查看是否注册成功  + 访问如下地址 ``` http://localhost:9001/provider/nacos/1 ```  nacos服务注册中心+服务提供者9001完成部署。 #### 2.8 相同配置再新建一个provider-nacos-demo02服务 步骤与`provider-nacos-demo01`一致,就是端口号改成9002 这里有一个简单的方法,虚拟映射一个和9001配置相同的9002微服务模块,端口号是9002 + **选择启动项进行复制**  + 修改名称和添加【Modify options】  + 选择【Add VM options】  + 修改端口为9002 ``` -DServer.port=9002 ```  + 启动项目  + 查看Nacos服务列表  ### 三、基于Nacos的服务消费者 #### 3.1 新建module `customer-nacos-demo01` #### 3.2 引入pom依赖 ```xml
4.0.0
com.lilinchao
SpringCloudAlibabaDemo
0.0.1-SNAPSHOT
customer-nacos-demo01
8
8
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
``` nacos中集成了Ribbon,支持负载均衡,自带`RestTemplate` #### 3.3 配置文件 ```yaml server: port: 9010 spring: application: name: customer-nacos-demo01 cloud: nacos: discovery: server-addr: localhost:8848 #消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者) service-url: nacos-user-service: http://provider-nacos-demo01 ``` #### 3.4 主启动类 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class CustomerNacosDemo01Application { public static void main(String[] args) { SpringApplication.run(CustomerNacosDemo01Application.class, args); } } ``` #### 3.5 负载均衡配置 ```java package com.lilinchao.springcloud.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class ApplicationContextBean { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } } ``` #### 3.6 业务类 ```java import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; @RestController @Slf4j public class NacosDemoController { @Resource private RestTemplate restTemplate; @Value("${service-url.nacos-user-service}") private String serverURL; @GetMapping("/consumer/nacos/{id}") public String paymentInfo(@PathVariable("id") Long id) { String result = restTemplate.getForObject(serverURL + "/provider/nacos/" + id, String.class); return result; } } ``` #### 3.7 测试 + 同时启动9001、9002、9010 + 查看Nacos控制台  + 访问链接 ``` http://localhost:9010/consumer/nacos/1 ``` 经过测试,9001与9002交替出现,即轮询负载均衡。
标签:
SpringCloudAlibaba
,
Nacos
非特殊说明,本博所有文章均为博主原创。
如若转载,请注明出处:
https://lilinchao.com/archives/2740.html
上一篇
MySQL迁移到达梦数据库
下一篇
03.Nacos作为服务配置中心基础配置演示
评论已关闭
栏目分类
随笔
2
Java
326
大数据
229
工具
31
其它
25
GO
47
NLP
6
标签云
并发编程
Spark Streaming
Scala
SpringBoot
Hbase
持有对象
CentOS
LeetCode刷题
Nacos
Spark SQL
Git
JavaWEB项目搭建
Hive
JavaSE
随笔
BurpSuite
队列
Java工具类
Netty
JavaScript
SQL练习题
Golang基础
Livy
Http
Linux
Azkaban
线程池
MySQL
Java编程思想
Python
友情链接
申请
范明明
庄严博客
Mx
陶小桃Blog
虫洞
评论已关闭