Spring cloud + Nacos to implement service registry (Hoxton version)

Platanus acerifolia 2021-04-08 09:58:38
spring cloud nacos implement service


Prerequisite
First, download and install a nacos server, Then start nacos server.Nacos Of Download and install process view https://www.cnblogs.com/ying-z/p/14630738.html
 
Register a service to nacos
(1) use SpringBoot Create a new service , The service name is nacos-discovery-server.
(2) stay pom Add the necessary dependencies to  
<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>
<!-- Statement Spring cloud The version is Hoxton.SR3-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Statement Spring cloud alibaba Version is 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>
<!-- Statement nacos rely on -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>

Be careful : edition  2.1.x.RELEASE  The corresponding is Spring Boot 2.1.x edition . edition  2.0.x.RELEASE  The corresponding is Spring Boot 2.0.x edition , edition  1.5.x.RELEASE  The corresponding is Spring Boot 1.5.x edition .

Refer to for more version correspondence : Version Description Wiki

(3) stay application.properties Configure the service name and nacos Address
server.port=9001
spring.application.name=nacos-discovery-server
spring.application.cloud.nacos.discovery.server-addr=127.0.0.1:8848

(4) Create a new one Controller, Provide an interface

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

(5) Start the service , The console prints the following , It means that the registration is successful . 

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) open nacos Console , We can find our services in the list of services .

 

  matters needing attention :

(1) Why didn't we add @EnableDiscoveryClient annotation , But the service has been registered to nacos Yes ?
 stay SpringCloud E At version time , Optimized service registration , I'm depending on you spring-cloud-starter-alibaba-nacos-discovery after , By default, the service will be registered with the registry .
(2) I'm already dependent on you spring-cloud-starter-alibaba-nacos-discovery Under the circumstances , If we don't want our services registered to nacos What to do ?
 Add the following dependency to the configuration file :
spring.cloud.nacos.discovery.enabled=false 
Consumption of services ( call )
We have set up a service registry , At the same time, I also registered the service with the service registry . Then we can discover and consume Services . The discovery of this service was made by nacos Realization , And the consumption of services can be controlled by Ribbon perhaps Feign Such as implementation , That is to say, the invocation of service needs nacos and Feign(Ribbon) Only by combining the two can .Feign We will use it later .....
版权声明
本文为[Platanus acerifolia]所创,转载请带上原文链接,感谢
https://javamana.com/2021/04/20210408094959811f.html

  1. Zookeeper分布式锁?
  2. Zookeeper distributed lock?
  3. 嵌入式Linux_Framebuffer_03点阵显示ASCII字符串
  4. 嵌入式Linux_Framebuffer_02字符编码
  5. Embedded Linux_ Framebuffer_ 03 dot matrix display ascii string
  6. Embedded Linux_ Framebuffer_ 02 character encoding
  7. Looking forward to new product launch of Xiaomi in spring CNMO takes you to see 11 new products in advance
  8. An inventory of the commonly used garbage collectors in Java
  9. Why is it so easy to get started with HBase?
  10. Implementation of PRC framework based on netty
  11. 2021 Java back end engineer must know knowledge - (Dubbo, distributed RPC framework)
  12. 关于spring advisor和元数据 同时来管理事务的问题
  13. How to manage transactions with spring advisor and metadata at the same time
  14. 使用Playwright对Java API实现自动视觉测试 - applitools
  15. Using playwright to implement automatic visual testing for Java API - applitools
  16. Dubbo和Spring cloud、Istio对比图
  17. Comparison of Dubbo with spring cloud and istio
  18. HttpServletRequest、通过request获得请求头、请求体等、解决中文乱码等问题
  19. Mybatis学习笔记-一对一,一对多,多对多关联查询
  20. Mybatis学习笔记-基本概念与操作
  21. HttpServletRequest, obtaining request header and request body through request, solving Chinese garbled code and other problems
  22. Mybatis learning notes - one to one, one to many, many to many association query
  23. Mybatis learning notes - basic concepts and operation
  24. Spring Cloud 升级之路 - 2020.0.x - 3. Undertow 的 accesslog 配置
  25. Spring cloud upgrade road - 2020.0. X - 3. Accesslog configuration of undertow
  26. 被Java培训机构坑骗后,我在这里找回了自信
  27. After being cheated by java training institutions, I found my confidence here
  28. Linux下安装Mysql出现的常见问题以及解决办法
  29. Common problems and solutions of installing MySQL under Linux
  30. java并发编程JUC第十二篇:AtomicInteger原子整型
  31. Java Concurrent Programming JUC Chapter 12: atomicinteger atomic integer
  32. 面经手册 · 第29篇《Spring IOC 特性有哪些,不会读不懂源码!》
  33. Chapter 29 "what are the features of spring IOC? I can't understand the source code! 》
  34. 浅析linux容器--Docker
  35. Analysis of Linux container -- docker
  36. 换种方法学操作系统,轻松入门Linux内核
  37. 浅析linux容器--Docker
  38. Another way to learn operating system, easy access to Linux kernel
  39. Analysis of Linux container -- docker
  40. 手摸手教你阅读和调试大型开源项目 ZooKeeper
  41. Hand in hand teaching you to read and debug large open source project zookeeper
  42. 基于SpringBoot 在线答题系统 含小程序!
  43. 10个优秀开源JavaScript模板引擎
  44. JavaScript基础知识及写法
  45. Based on springboot online answering system, including small procedures!
  46. 重磅!谷歌Fuchsia操作系统将支持运行Linux应用程序
  47. (四十) springcloud分布式商城之跟我学习SpringCloud-Gateway整合Eureka路由转发
  48. Spring IOC 特性有哪些,不会读不懂源码!
  49. 手摸手教你阅读和调试大型开源项目 ZooKeeper
  50. 10 excellent open source JavaScript template engines
  51. Basic knowledge and writing method of JavaScript
  52. win10安装mysql5.7
  53. Heavy weight! Google's Fuchsia operating system will support running Linux applications
  54. (40) Spring cloud distributed mall learn from me spring cloud gateway integrates Eureka routing and forwarding
  55. What are the spring IOC features? I can't understand the source code!
  56. Hand in hand teaching you to read and debug large open source project zookeeper
  57. Install mysql5.7 in win10
  58. SCIP:构造数据抽象--数据结构中队列与树的解释
  59. Spring Cloud+Nacos实现服务注册中心(Hoxton版本)
  60. rabbitmq-server的安装与升级