Spring Cloud+Nacos实现服务注册中心(Hoxton版本)

悬铃木pp 2021-04-08 09:51:36
spring Nacos 实现 Cloud cloud+nacos


前提条件
首先下载安装一个nacos server,然后启动nacos server。Nacos的下载和安装流程查看 https://www.cnblogs.com/ying-z/p/14630738.html
 
注册一个服务到nacos
(1)用SpringBoot新建一个服务,服务名称为nacos-discovery-server。
(2)在pom中加入必要的依赖  
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<!-- 声明Spring cloud的版本为Hoxton.SR3-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 声明Spring cloud alibaba版本为2.1.0.RELEASE -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- 声明nacos依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>

注意:版本 2.1.x.RELEASE 对应的是 Spring Boot 2.1.x 版本。版本 2.0.x.RELEASE 对应的是 Spring Boot 2.0.x 版本,版本 1.5.x.RELEASE 对应的是 Spring Boot 1.5.x 版本。

更多版本对应关系参考:版本说明 Wiki

(3)在application.properties中配置服务名称和nacos地址
server.port=9001
spring.application.name=nacos-discovery-server
spring.application.cloud.nacos.discovery.server-addr=127.0.0.1:8848

(4)新建一个Controller,提供一个接口

//SpringBoot主入口
@SpringBootApplication
public class NacosDiscoveryServerApplication {
public static void main(String[] args) {
SpringApplication.run(NacosDiscoveryServerApplication.class, args);
}
}
//接口
@RestController
public class NacosDiscoveryController {
@GetMapping("/nacos/discovery/hello")
public String hello(@RequestParam String name) {
return "hello, nacos discovery! " + name;
}
}

(5)启动服务,控制台打印以下内容,就说明注册成功了。 

2020-04-28 14:49:42.749 INFO 9864 --- [main] c.a.c.n.registry.NacosServiceRegistry:nacos registry, DEFAULT_GROUP nacos-discovery-server 192.168.9.114:9001 register finished

(6)打开nacos控制台,我们可以在服务列表中发现我们的服务了。

 

 注意事项:

(1)为什么我们的启动类上没有加@EnableDiscoveryClient注解,但是已经把服务注册到nacos上了?
在SpringCloud E版本的时候,对服务注册进行了优化,在依赖了spring-cloud-starter-alibaba-nacos-discovery之后,默认会将服务注册到注册中心。
(2)在已经依赖spring-cloud-starter-alibaba-nacos-discovery的情况下,如果我们不想让我们的服务注册到nacos应该怎么做?
在配置文件中添加如下依赖即可:
spring.cloud.nacos.discovery.enabled=false 
服务的消费(调用)
我们已经搭建一个服务注册中心,同时也向这个服务注册中心注册了服务。接下来我们就可以发现和消费服务了。这其中服务的发现由nacos实现,而服务的消费可以由Ribbon或者Feign等实现,也就是说服务的调用需要nacos和Feign(Ribbon)两者配合起来才能实现。Feign的使用待后续.....
版权声明
本文为[悬铃木pp]所创,转载请带上原文链接,感谢
https://www.cnblogs.com/ying-z/p/14630785.html

  1. How to realize encryption and decryption of spring boot interface parameters gracefully?
  2. A new way to play in spring 5! This kind of URL request makes me see better!
  3. Can parameters in spring MVC be passed like this? It's up!
  4. Hand in hand to teach you how to develop mybatis plug-ins
  5. Fine spring boot + thymeleaf, there are so many fun details!
  6. Spring boot logs all kinds of posture, it's time to clear!
  7. Web 3.0踏浪而来,分布式存储举足轻重|时空云邀请您参加Web3.0中国峰会暨分布式存储行业大会
  8. spring-aop 进不了切面方法的解决办法
  9. Web 3.0 is coming, distributed storage is very important | spatiotemporal cloud invites you to attend Web3.0 China Summit and distributed storage industry conference
  10. The solution of spring AOP can't enter the section method
  11. Linux中如何启用root用户
  12. How to enable root in Linux
  13. 踩坑 MySQL 索引,看看你真的会用吗?
  14. Hive优化之配置参数的优化(一)
  15. Step on the MySQL index to see if you really know how to use it?
  16. Optimization of configuration parameters for hive optimization (1)
  17. Linux入门教程资料分享
  18. Introduction to Linux
  19. 外部连接mysql docker容器异常
  20. Exception of external connection MySQL docker container
  21. Zookeeper分布式锁?
  22. Zookeeper distributed lock?
  23. 嵌入式Linux_Framebuffer_03点阵显示ASCII字符串
  24. 嵌入式Linux_Framebuffer_02字符编码
  25. Embedded Linux_ Framebuffer_ 03 dot matrix display ascii string
  26. Embedded Linux_ Framebuffer_ 02 character encoding
  27. Looking forward to new product launch of Xiaomi in spring CNMO takes you to see 11 new products in advance
  28. An inventory of the commonly used garbage collectors in Java
  29. Why is it so easy to get started with HBase?
  30. Implementation of PRC framework based on netty
  31. 2021 Java back end engineer must know knowledge - (Dubbo, distributed RPC framework)
  32. 关于spring advisor和元数据 同时来管理事务的问题
  33. How to manage transactions with spring advisor and metadata at the same time
  34. 使用Playwright对Java API实现自动视觉测试 - applitools
  35. Using playwright to implement automatic visual testing for Java API - applitools
  36. Dubbo和Spring cloud、Istio对比图
  37. Comparison of Dubbo with spring cloud and istio
  38. HttpServletRequest、通过request获得请求头、请求体等、解决中文乱码等问题
  39. Mybatis学习笔记-一对一,一对多,多对多关联查询
  40. Mybatis学习笔记-基本概念与操作
  41. HttpServletRequest, obtaining request header and request body through request, solving Chinese garbled code and other problems
  42. Mybatis learning notes - one to one, one to many, many to many association query
  43. Mybatis learning notes - basic concepts and operation
  44. Spring Cloud 升级之路 - 2020.0.x - 3. Undertow 的 accesslog 配置
  45. Spring cloud upgrade road - 2020.0. X - 3. Accesslog configuration of undertow
  46. 被Java培训机构坑骗后,我在这里找回了自信
  47. After being cheated by java training institutions, I found my confidence here
  48. Linux下安装Mysql出现的常见问题以及解决办法
  49. Common problems and solutions of installing MySQL under Linux
  50. java并发编程JUC第十二篇:AtomicInteger原子整型
  51. Java Concurrent Programming JUC Chapter 12: atomicinteger atomic integer
  52. 面经手册 · 第29篇《Spring IOC 特性有哪些,不会读不懂源码!》
  53. Chapter 29 "what are the features of spring IOC? I can't understand the source code! 》
  54. 浅析linux容器--Docker
  55. Analysis of Linux container -- docker
  56. 换种方法学操作系统,轻松入门Linux内核
  57. 浅析linux容器--Docker
  58. Another way to learn operating system, easy access to Linux kernel
  59. Analysis of Linux container -- docker
  60. 手摸手教你阅读和调试大型开源项目 ZooKeeper