# Spring Cloud Alibaba Nacos Example
## Project description
This project demonstrates how to use Spring Cloud Alibaba Nacos related Starters to complete the service discovery and configuration management of Spring Cloud applications.
[Nacos](https://github.com/alibaba/Nacos) It is Alibaba's open source dynamic service discovery, configuration management and service management platform that is easier to build cloud-native applications.
## Nacos Server 2.3.2 is properly configured and started
In Nacos 2.3.2, functions related to user authentication are added. When starting Nacos Server for the first time, it needs to be configured correctly to avoid the problem of startup failure.
### Download Nacos Server
> The Nacos serv version used in this example is 2.2.3!
Nacos supports both direct download and source code construction. **Nacos Server version 2.2.3 is recommended for Spring Cloud Alibaba 2022.x.**
1. Direct download: [Nacos Server download page](https://github.com/alibaba/nacos/releases)
2. Source code construction: Enter Nacos [Github project page](https://github.com/alibaba/nacos), git clone the code to the local compilation and packaging [参考文档](https://nacos.io/zh-cn/docs/quick-start.html).
### Configure the Nacos Server
Open the `\nacos-server-2.2.3\conf\application.properties` configuration file and modify the following configuration items:
#### Configure the data source
Take the MySQL database as an example here, and use the `nacos-server-2.2.3\conf\mysql-schema.sql` initialization database table file. Modify the following configuration as well
```properties
#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:
spring.datasource.platform=mysql
### Count of DB:
db.num=1
### Connect URL of DB:
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
db.user.0=root
db.password.0=root
### Connection pool configuration: hikariCP
db.pool.config.connectionTimeout=30000
db.pool.config.validationTimeout=10000
db.pool.config.maximumPoolSize=20
db.pool.config.minimumIdle=2
```
#### Turn on authentication
**Note: If it is not enabled, login failure exception will occur in 2.3.2!**
```properties
### The auth system to use, currently only 'nacos' and 'ldap' is supported:
nacos.core.auth.system.type=nacos
### If turn on auth system:
nacos.core.auth.enabled=true
```
#### Set the server authentication key
```properties
nacos.core.auth.server.identity.key=test
nacos.core.auth.server.identity.value=test
```
#### Set the default token
```properties
### The default token (Base64 String):
nacos.core.auth.plugin.nacos.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789
```
** When using the Nacos service discovery and configuration function, be sure to configure `username` and `password` attribute, otherwise the user will not be found! **
#### Open API authentication
Authentication is required when using the Open api interface in nacos server 2.3.2: For more details, please refer to: [Nacos api authentication](https://nacos.io/zh-cn/docs/auth.html)
1. Obtain accessToken: Use username and password to log in to the nacos server:
`curl -X POST '127.0.0.1:8848/nacos/v1/auth/login' -d 'username=nacos&password=nacos'`
If the username and password are correct, the returned information is as follows:
`{"accessToken":"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJuYWNvcyIsImV4cCI6MTYwNTYyOTE2Nn0.2TogGhhr11_vLEjqKko1HJHUJEmsPuCxkur-CfNojDo", "tokenTtl": 18000, "globalAdmin": true}`
2. Use accessToken to request the nacos api interface:
`curl -X GET '127.0.0.1:8848/nacos/v1/cs/configs?accessToken=eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJuYWNvcyIsImV4cCI6MTYwNTYyMzkyM30.O-s2yWfDSUZ7Svd3Vs7jy9tsfDNHs1SuebJB4KlNY8Q&dataId=nacos.example.1&group=nacos_group'`
### Start the Nacos Server
1. Start Nacos Server, enter the folder after downloading to the local and decompressing (enter the folder after compiling and packaging by using the source code construction method), then enter its relative folder `nacos/bin`, and execute the following command according to the actual situation of the operating system. [详情参考此文档](https://nacos.io/zh-cn/docs/quick-start.html)。
1. Linux/Unix/Mac operating system, execute the command
`sh startup.sh -m standalone`
2. Windows operating system, executing command
`cmd startup.cmd`
2. Access Nacos Server Console.
The browser enters the address http://127.0.0.1:8848/nacos , **The first login needs to bind the nacos user, because the new version adds authentication, and the user name and password need to be configured during application registration and configuration binding.**
## Nacos application example
### Spring Cloud Alibaba Nacos Config
#### Application access
Before starting the application sample to demonstrate the project function, first understand how the Spring Cloud application accesses Nacos Config as the service configuration center.
**Note that this section is only for the convenience of understanding the access method. The access work has been completed in this sample code, and no further modification is required.**
1. First, modify the `pom.xml` file to introduce spring-cloud-starter-alibaba-nacos-config;
```xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
```
2. Configuring a Nacos address in an `/src/main/resources/application.yaml` applied configuration file and introducing a service configuration;
```yml
spring:
cloud:
nacos:
serverAddr: 127.0.0.1:8848
username: 'nacos'
password: 'nacos'
config:
import:
- nacos:nacos-config-example.properties?refreshEnabled=true&group=DEFAULT_GROUP
```
3. After completing the above two steps, the application will obtain the corresponding configuration from Nacos Server and add it to the Property Sources of Spring Environment. There are four ways to save a portion of the Nacos configuration using the Nacos Configuration Center:
- BeanAutoRefres hConfig Example: An example of supporting automatic refresh of configuration changes by configuring configuration information as beans;
- ConfigListenerEx ample: example of monitoring configuration information;
- Docking Interface Example: An example of docking the Nacos interface and completing the addition, deletion, modification and query of the configuration information through the interface;
- ValueAnnotation Example: An example of obtaining configuration information through the @ Value annotation.
#### Add Configuration in Nacos Server
Add a configuration to the Nacos Server by executing the following command from the command line. **Can be injected directly through the Nacos console!**
```shell
$ curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?accessToken=XXXXXXXXXXXXXXXXXXXXXXXXXXX&dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=spring.cloud.nacos.config.serverAddr=127.0.0.1:8848%0Aspring.cloud.nacos.config.prefix=PREFIX%0Aspring.cloud.nacos.config.group=GROUP%0Aspring.cloud.nacos.config.namespace=NAMESPACE"
```
Details of the added configuration are as follows:
```properties
# dataId is nacos-config-example.properties
# group is DEFAULT_GROUP
# content is:
spring.cloud.nacos.config.serveraddr=127.0.0.1:8848
spring.cloud.nacos.config.prefix=PREFIX
spring.cloud.nacos.config.group=GROUP
spring.cloud.nacos.config.namespace=NAMESPACE
```
#### The application starts
1. Add configuration, and add basic configuration information in the application `/src/main/resources/application.yml`;
```yml
server:
port: 18084
management:
endpoints:
web:
exposure:
include: '*'
```
2. Start th
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Spring Cloud Alibaba 致力于提供微服务开发的一站式解决方案。此项目包含开发分布式应用微服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。 依托 Spring Cloud Alibaba,您只需要添加一些注解和少量配置,就可以将 Spring Cloud 应用接入阿里微服务解决方案,通过阿里中间件来迅速搭建分布式应用系统。 此外,阿里云同时还提供了 Spring Cloud Alibaba 企业版 微服务解决方案,包括无侵入服务治理(全链路灰度,无损上下线,离群实例摘除等),企业级 Nacos 注册配置中心和企业级云原生网关等众多产品。
资源推荐
资源详情
资源评论
收起资源包目录
Spring Cloud Alibaba为阿里巴巴中间件的分布式解决方案提供一站式应用开发解决方案 (780个子文件)
spring.binders 97B
mvnw.cmd 7KB
broker.conf 970B
broker.conf 162B
broker.conf 162B
broker.conf 162B
jvm.config 95B
Dockerfile 252B
Dockerfile 252B
Dockerfile 241B
Dockerfile 237B
Dockerfile 236B
Dockerfile 236B
Dockerfile 232B
.env 149B
spring.factories 1KB
spring.factories 283B
aot.factories 227B
spring.factories 154B
spring.factories 142B
aot.factories 121B
spring.factories 121B
sca-ai-example-front.gif 861KB
.gitignore 518B
beers.json.gz 28MB
index.html 6KB
order.html 4KB
sentinel.html 3KB
rocketmq.html 2KB
errorPage.html 142B
org.springframework.boot.autoconfigure.AutoConfiguration.imports 697B
org.springframework.boot.autoconfigure.AutoConfiguration.imports 310B
org.springframework.boot.autoconfigure.AutoConfiguration.imports 257B
org.springframework.boot.autoconfigure.AutoConfiguration.imports 193B
org.springframework.boot.autoconfigure.AutoConfiguration.imports 177B
org.springframework.boot.autoconfigure.AutoConfiguration.imports 136B
org.springframework.boot.autoconfigure.AutoConfiguration.imports 123B
org.springframework.boot.autoconfigure.AutoConfiguration.imports 109B
org.springframework.boot.autoconfigure.AutoConfiguration.imports 76B
org.springframework.boot.autoconfigure.AutoConfiguration.imports 52B
maven-wrapper.jar 59KB
NacosDiscoveryProperties.java 19KB
NacosConfigProperties.java 19KB
TongYiChatClient.java 15KB
RocketMQConsumerProperties.java 13KB
TongYiChatOptions.java 13KB
StringUtils.java 11KB
RocketMQProducerMessageHandler.java 11KB
SentinelAutoConfigurationTests.java 11KB
SentinelRestTemplateTests.java 10KB
NacosConfigurationXmlJsonTest.java 10KB
RocketMQMessageChannelBinder.java 10KB
SentinelBeanPostProcessor.java 9KB
NacosConfigDataLocationResolverTest.java 9KB
MockNamingService.java 9KB
SentinelProperties.java 9KB
NacosConfigurationNoSuffixTest.java 9KB
NacosConfigDataLocationResolver.java 8KB
NacosConfigurationNewTest.java 8KB
FeignClientCircuitBreakerRuleIntegrationTest.java 8KB
RocketMQInboundChannelAdapter.java 8KB
IOUtils.java 8KB
TongYiAudioSpeechClient.java 8KB
SentinelDataSourceHandler.java 8KB
RocketMQConsumerFactory.java 8KB
JacksonRocketMQHeaderMapper.java 8KB
NacosPropertySourceLocator.java 8KB
NacosConfigurationTests.java 7KB
CircuitBreakerRuleChangeListener.java 7KB
ReactiveSentinelCircuitBreakerIntegrationTest.java 7KB
NacosAutoServiceRegistrationTests.java 7KB
NacosConfigurationTests.java 7KB
RocketMQMessageConverterSupport.java 7KB
DataSourcePropertiesTests.java 7KB
TongYiImagesClient.java 7KB
SentinelHealthIndicatorTests.java 6KB
SentinelFeign.java 6KB
NacosConfigDataLoader.java 6KB
RocketMQMessageSource.java 6KB
TongYiAutoConfiguration.java 6KB
NacosDiscoveryTest.java 6KB
NacosServiceRegistry.java 6KB
NacosConfigurationExtConfigTests.java 6KB
SentinelGatewayAutoConfiguration.java 6KB
SentinelFeignTests.java 6KB
SentinelSCGAutoConfiguration.java 6KB
Charsets.java 6KB
NacosLoadBalancer.java 6KB
SentinelHealthIndicator.java 6KB
NacosDataParserHandler.java 6KB
RocketMQProduceFactory.java 5KB
TongYiTextEmbeddingClient.java 5KB
SentinelAutoConfiguration.java 5KB
NacosContextRefresher.java 5KB
RocketMQProducerProperties.java 5KB
SentinelProtectInterceptor.java 5KB
NacosXmlPropertySourceLoader.java 5KB
NacosRegistration.java 5KB
SidecarConsulAutoRegistration.java 5KB
RocketMQCommonProperties.java 5KB
共 780 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
资源评论
UnknownToKnown
- 粉丝: 1w+
- 资源: 643
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功