This is the 18th day of my participation in Gwen Challenge
Accumulate over a long period, constant dripping wears away a stone 😄
In my last article, I explained how to install Nacos, and this one will be the focus.
Set up the SpringBoot project
Join the rely on
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
Copy the code
The nacos-config dependency is equivalent to SpringCloud Config, and the nacos-Discovery dependency is equivalent to Eureka.
Configuration center
Create the bootstrap.yml file in the project and add the following configuration:
spring:
application:
# service name
name: naocs-service
profiles:
active: dev
cloud:
nacos:
config:
# config file environment
group: ${spring.profiles.active}
Format of the configuration file
file-extension: yaml
Configure the address of the center
server-addr: 47.105198.54.: 8848
# configure file prefix
prefix: ${spring.application.name}
# namespaces
namespace: mall
Copy the code
Then log in to NacOS and select Configuration Management on the NacOS page to create a configuration file
There are three main things to configure: DataID, Group, and what to configure.
${prefix}-${spring.profile.active}.${file-extension}.
${prefix}
The default value isspring.application.name
Is also available through the configuration itemspring.cloud.nacos.config.prefix
To configure.${spring.profile.active}
: indicates the current environmentprofile
, note: whenspring.profiles.active
When null, the corresponding concatenation – will also not exist, and the DataID concatenation format will become${prefix}.${file-extension}
.${file-extension}
Indicates the extension of the configuration file, which can be passed through the configuration itemspring.cloud.nacos.config.file-extension
To configure. Currently only supportedproperties
和yaml
Type.
A Group is a Group. In this article, it is divided according to the environment. For example, dev, test, uAT.Author configuration:
-
Data Id: naocs – service – dev. Yaml
-
Group :dev
-
Creating a Namespace
Provide the Controller for the test
@RestController
@RefreshScope // Dynamic refresh
public class HelloController {
@Value("${name}")
private String name;
@GetMapping
public String hell(a){
returnname; }}Copy the code
Then call interface: http://localhost:8080/hello
The registry
Unshackle nacos-Discovery dependency annotations. Then add the following configuration under the nacOS node of the bootstrap.yml file:
discovery:
server-addr: 47.105198.54.: 8848
group: ${spring.profiles.active}
namespace: mall
Copy the code
Start the project and log in to NACOS. You can seeService Management – Service listContains information about registered services.
Load multiple configuration files
In real development, it would be impossible to put all the configurations in the same configuration file at the same time; it would be too many and messy.
product-dev.yaml
Create a new product-dev.yaml file, which you can also use.
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
# MySQL configuration
driverClassName: com.mysql.cj.jdbc.Driver
url: JDBC: mysql: / / 47.105.198.54:3306 / test? useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: xxxx
jackson:
date-format: yyyy-MM-dd HH:mm:ss If a string representation is used, format it with this line
timezone: GMT+8
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
Copy the code
Yaml = datasource-dev. Yaml = mybatis-plus-dev.
datasource-dev.yaml
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
# MySQL configuration
driverClassName: com.mysql.cj.jdbc.Driver
url: JDBC: mysql: / / 47.105.198.54:3306 / test? useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: xxxx
Copy the code
mybatis-plus-dev.yaml
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
Copy the code
Then add the configuration under the config node in the bootstrap.yml application:
spring:
profiles:
active: ${DEPLOY_ENV}
application:
name: product
cloud:
nacos:
config:
group: ${spring.profiles.active}
file-extension: yaml
server-addr: ${NACOS}
prefix: ${spring.application.name}
namespace: ${nacos.client.namespace}
extension-configs:
- dataId: datasource-${spring.profiles.active}.yaml
group: ${spring.profiles.active}
# Whether to refresh automatically
refresh: true
- dataId: mybatis-plus-${spring.profiles.active}.yaml
group: ${spring.profiles.active}
refresh: true
Copy the code
Note that ${DEPLOY_ENV} and ${NACOS} are startup configurations that correspond to the loading environment such as dev and NACOS address such as 47.105.198.54:8848.