目前市面上用的比较多的配置中心有
更详细的对比可以参考这篇网友写的文章:
https://www.jianshu.com/p/2f0ae9c7f2e1
今天的主要任务是SpringBoot项目如何使用配置中心Nacos。
0x01:新建SpringBoot项目
新建项目com-nacos-config,在pom.xml文件中引入如下配置
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.nacos</groupId>
<artifactId>com-nacos-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>com-nacos-config</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>0.2.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
0x02:编写数据库持久层
使用的jpa的CrudRepository
package com.nacos.config.dao;
import org.springframework.data.repository.CrudRepository;
import com.nacos.config.model.UserModel;
public interface UserRepository extends CrudRepository<UserModel, Integer> {
}
实体类映射关系如下
package com.nacos.config.model;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity(name = "t_user")
public class UserModel {
@Id
private Integer id;
private String username;
private String password;
//get set 省略
}
从实体类可以知道表格很简单就只有id、username、password三个字段。
0x03:编写控制器Controller
package com.nacos.config.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.nacos.config.dao.UserRepository;
import com.nacos.config.model.UserModel;
@RestController("/user")
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping
@ResponseBody
public UserModel get(@RequestParam int id) {
return userRepository.findById(id).get();
}
}
0x04:编写启动类型及配置application.properties文件
package com.nacos.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
@SpringBootApplication
@NacosPropertySource(dataId = "mysql", autoRefreshed = true)
@RestController
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
启动类,用使用如下代码引入Nacos的配置项
@NacosPropertySource(dataId = "mysql", autoRefreshed = true)
application.properties文件内容如下:
spring.application.name=com-nacos-config
server.port=8090
nacos.config.server-addr=127.0.0.1:8848
nacos.config.server-addr配置项指定了配置中心Nacos的服务地址。
0x05:验证
在启动项目之前先确认
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
在浏览器输入
http://127.0.0.1:8090/user?id=1
可以看到如下效果
这条数据是数据库里预埋的数据