Spring cloud integrates Nacos

Ask Youlong time 2021-09-15 08:46:58
spring cloud integrates nacos


1、nacos brief introduction

Nacos Is to build in “ service ” Modern application architecture centered ( For example, the microservices paradigm 、 Cloud native paradigm ) Service infrastructure .

nacos-logic.jpg

  • Service management : Implementation services CRUD, domain name CRUD, Service health check , Service weight management and other functions

  • Configuration Management : Implement configuration management CRUD, version management , Gray management , Monitoring management , Push trajectory , Aggregate data and other functions

  • Metadata management : Provide metadata CURD And marking ability

  • Plug-in mechanism : Realize the ability of three modules to be separated and combined , Implementation extension point SPI Mechanism

  • Event mechanism : Implement asynchronous event notification ,sdk Data change, asynchronous notification, etc

  • Log module : Manage log classification , The level of logging , Log portability ( Especially avoiding conflict ), Log format , Abnormal code + Help document

  • Callback mechanism :sdk Notification data , Call back the user through a unified mode . Interfaces and data structures need to be scalable

  • Addressing mode : solve ip, domain name ,nameserver、 Broadcast and other addressing modes , Need to be scalable

  • Push channel : solve server With storage 、server between 、server And sdk The problem of inter push performance

  • Capacity management : Manage every tenant , The capacity under the packet , Prevent the storage from being written explosion , Affect service availability

  • Traffic management : According to the tenant , Grouping and other dimensions on the request frequency , Number of long links , Message size , Request flow control to control

  • Caching mechanisms : Disaster recovery Directory , Local cache ,server Caching mechanisms . Disaster recovery directory needs tools

  • Boot mode : In stand-alone mode , Configuration mode , Service mode ,dns Pattern , perhaps all Pattern , Start different programs +UI

  • Agreement of conformity : Solve different data , In the case of different consistency requirements , Different consistency mechanisms

  • Storage module : Solve data persistence 、 Non persistent storage , Solve the problem of data fragmentation

  • Nameserver: solve namespace To clusterid The routing problem of , Solve the problem of user environment and nacos Physical environment mapping problem

  • CMDB: Solve metadata storage , With the three parties cmdb System docking problem , Solution application , people , Resource relations

  • Metrics: Exposure criteria metrics data , It is convenient to connect with the three party monitoring system

  • Trace: Exposure criteria trace, Convenience and SLA Get the system through , The journal is white and flat , Push trajectory, etc , And it can connect with the metering and billing system

  • Access management : It's equivalent to Alibaba cloud opening service , Assign identities 、 Capacity 、 Permission process

  • User management : Solve user management , Sign in ,sso Other questions

  • Rights management : Solve identification , Access control , Role management and so on

  • Audit system : Expand the interface to facilitate access to audit systems of different companies

  • Notification system : Core data changes , Or operation , Convenient passage SMS Get the system through , Inform the corresponding person of the data change

  • OpenAPI: Exposure criteria Rest style HTTP Interface , Simple and easy to use , Facilitate multilingual Integration

  • Console: Easy to use console , Do service management 、 Configuration management and other operations

  • SDK: Multilingual sdk

  • Agent:dns-f Similar patterns , Or with mesh And so on

  • CLI: Command line lightweight management of products

2、nacos Download start

Download installation package

https://github.com/alibaba/nacos/releases

When the download is complete , Need configuration mysql Connection information , stay nacos Of /conf/application.properties Configuration in file

image.png

Then execute... In the database nacos Database script for /conf/nacos-mysql.sql

Then you can start , When starting, use stand-alone mode

startup.cmd -m standalone

3、 Build a service delivery project

3.1、pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>provider</artifactId>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>

3.2、 The configuration file

Specify the project name and in the configuration file nacos The address of

server.port=8081
spring.application.name=nacos-provider
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
management.endpoints.web.exposure.include=*

3.3、 Interface

Build an interface , Enter who called this interface

@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderDemoApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProviderDemoApplication.class, args);
}
@RestController
public class EchoController {
@GetMapping(value = "/echo/{string}")
public String echo(@PathVariable String string) {
return "Hello Nacos Discovery " + string;
}
}
}

4、 Functions of consumer services

4.1、pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.com.wenyl.alibaba</groupId>
<artifactId>consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>

4.2、 The configuration file

server.port=8080
spring.application.name=nacos-consumer
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
management.endpoints.web.exposure.include=*

4.3、 Interface

Build an interface , The interface of the service provider is invoked in this interface

/**
* @author Mr.Wen
* @version 1.0
* @date 2021-08-24 16:54
*/
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerDemoApplication.class, args);
}
@RestController
public class NacosController{
@Resource
private LoadBalancerClient loadBalancerClient;
@Resource
private RestTemplate restTemplate;
@Value("${spring.application.name}")
private String appName;
@GetMapping("/echo/app-name")
public String echoAppName(){
//Access through the combination of LoadBalanceClient and RestTemplate
ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-provider");
String path = String.format("http://%s:%s/echo/%s",serviceInstance.getHost(),serviceInstance.getPort(),appName);
System.out.println("request path:" +path);
return restTemplate.getForObject(path,String.class);
}
}
//Instantiate RestTemplate Instance
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}

5、 transfer

Call the consumption interface of the service consumer , Then the load balancing client selects a service to invoke (loadBanceClient It's the load balancing client , Call the service according to the service name ), The results are as follows

image.png


title :spring cloud Integrate nacos
author : wenyl
Address : http://www.wenyoulong.com/articles/2021/09/08/1631080254271.html

版权声明
本文为[Ask Youlong time]所创,转载请带上原文链接,感谢
https://javamana.com/2021/09/20210909141628140w.html

  1. La dernière réponse à l'entrevue de développement Android, l'hiver froid de l'industrie
  2. A young Lexus, the new NX refuses to be mediocre and mature
  3. Interprétation approfondie de l'équipe sin7y: application de plookup dans la conception de zkevm
  4. Java basic knowledge point Combing, redis Common Data Structures and Using scenario Analysis,
  5. Five minutes to understand MySQL index push down
  6. Data structure and algorithm (XI) -- algorithm recursion
  7. Programmation asynchrone Java scirp, développement frontal de base
  8. Java basic knowledge point video, three sides ant Gold Clothing successfully obtained offer,
  9. Oracle Linux bascule le noyau uek vers le noyau rhck pour résoudre les problèmes de compatibilité acfs
  10. After the grand finale of spring in jade mansion, after reading many comments, I began to sympathize with white deer
  11. 字节跳动Java高级工程师,统一命名服务、集群管理、分布式应用?
  12. 字节跳动Java高级工程师,深入分布式缓存从原理到实践技术分享,
  13. 字节跳动第三轮技术面,阿里P8架构师Java大厂面试题总结,
  14. 字节跳动社招Java面试,超通俗解析CountDownLatch用法和源码,
  15. 字节跳动最新开源,最经典的HashMap图文详解,
  16. 字節跳動第三輪技術面,阿裏P8架構師Java大廠面試題總結,
  17. Byte Jumping the Third Third Technical surface, Ali P8 Architect Java Factory Interview Question summary,
  18. L'ingénieur Java senior de Byte Hopping approfondit la mise en cache distribuée, du principe au partage de la technologie pratique.
  19. Byte Jump Java Senior Engineer, Unified Naming service, Cluster Management, Distributed application?
  20. Plusieurs méthodes de transfert de fichiers entre Windows et Linux
  21. 快速从 Windows 切换到 Linux 环境
  22. 五分钟向MySql数据库插入一千万条数据
  23. Java日期时间API系列42-----一种高效的中文日期格式化和解析方法
  24. 用Java实现红黑树
  25. 使用Redis Stream来做消息队列和在Asp.Net Core中的实现
  26. 海量列式非关系数据库HBase 架构,shell与API
  27. Architecture, Shell et API de base de données non relationnelle à grande échelle
  28. Mise en œuvre de l'arbre Rouge et noir en Java
  29. Java Date Time API Series 42 - - a efficient Chinese Date Format and Analysis Method
  30. 5 minutes pour insérer 10 millions de données dans la base de données MySQL
  31. Passage rapide de Windows à l'environnement Linux
  32. Notes on Java backend development of PostgreSQL (I)
  33. 海量列式非關系數據庫HBase 架構,shell與API
  34. Byte Jump the latest open source, the most Classic hashtap Graph details,
  35. L'interview Java de Byte Hopping Society, l'analyse super populaire de l'utilisation et du code source de countdownlatch,
  36. "Anti Mafia storm" Wang Zhifei's love history is really wonderful: he divorced Zhang Xinyi and married a 14-year-old wife
  37. In spring in the jade mansion, Jia Fengyuan was not moved by his brother's death. Why was su Yingxue changed? The reason is realistic
  38. Adam Oracle Oracle fully constructs Adam token incentive for ecological development
  39. 实战SpringCloud通用请求字段拦截处理,超过500人面试阿里,
  40. 宅家36天咸鱼翻身入职腾讯,Zookeeper一致性级别分析,
  41. The first starcoin & move hacksong source code analysis - P (a)
  42. Zhaijia 36 days Salt Fish turn into Tencent, Zookeeper Consistency level analysis,
  43. Traitement de l'interception des champs de demande communs de Spring Cloud, plus de 500 personnes interviewent Ali,
  44. About JavaScript modules
  45. Object oriented programming (2)
  46. Java日期时间API系列42-----一种高效的中文日期格式化和解析方法
  47. Java日期時間API系列42-----一種高效的中文日期格式化和解析方法
  48. 宅家36天鹹魚翻身入職騰訊,Zookeeper一致性級別分析,
  49. Java Date Time API Series 42 - - a efficient Chinese Date Format and Analysis Method
  50. 已成功拿下字节、腾讯、脉脉offer,7年老Java一次操蛋的面试经历,
  51. 小米Java社招面试,每次面试必问的二叉树的设计与编码,
  52. 小米Java校招面试,阿里、百度、美团、携程、蚂蚁面经分享,
  53. 小米Java校招面試,阿裏、百度、美團、攜程、螞蟻面經分享,
  54. Xiaomi Java School Recruitment interview, Ali, baidu, meituan, ctrip, ant Facebook Sharing,
  55. La conception et le codage de l'arbre binaire requis pour chaque entrevue d'embauche de la société Java millet;
  56. A remporté avec succès Byte, Tencent, Pulse offer, 7 ans Java une expérience d'entrevue de baise,
  57. 干货来袭,Java岗面试12家大厂成功跳槽,
  58. 常用Java框架面试题目,现在做Java开发有前途吗?
  59. 常用Java框架面試題目,現在做Java開發有前途嗎?
  60. Les questions d'entrevue couramment utilisées pour le cadre Java sont - elles prometteuses pour le développement Java?