1. 配置中心实战
1.1
- 体验一把配置中心在项目中的作用
1.2
- 编写user测试服务:添加数据 --> cloud_db5.t_user
- 将程序打包jar包,运行jar包
- 编写nacos配置中心, cloud_db6.t_user
1.3
1)user测试服务
-
修改父项目,添加依赖(锁定)
<!-- 1 确定spring boot的版本--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> </parent> <!--2 确定版本--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <spring-cloud-release.version>Hoxton.SR3</spring-cloud-release.version> <nacos.version>1.1.0</nacos.version> <alibaba.cloud.version>2.2.1.RELEASE</alibaba.cloud.version> <mysql.version>5.1.32</mysql.version> <mybatis.plus.version>3.4.0</mybatis.plus.version> <jwt.jjwt.version>0.9.0</jwt.jjwt.version> <jwt.joda.version>2.9.7</jwt.joda.version> <swagger.version>2.7.0</swagger.version> </properties> <!-- 3 锁定版本--> <dependencyManagement> <dependencies> <!-- sprig cloud--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud-release.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!--nacos --> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>${nacos.version}</version> </dependency> <!--nacos cloud 发现 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>${alibaba.cloud.version}</version> </dependency> <!--nacos cloud 配置 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>${alibaba.cloud.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-sentinel --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>${alibaba.cloud.version}</version> </dependency> <!-- mybatis plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis.plus.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-annotation</artifactId> <version>${mybatis.plus.version}</version> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!--swagger2--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${swagger.version}</version> </dependency> </dependencies> </dependencyManagement>
-
创建user服务:test-nacos-cloud-config-mysql
-
修改pom添加依赖
<dependencies> <!-- web 启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- nacos 配置--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <!--swagger2--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> </dependency> <!-- mybatis plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--lombok , @Data 等--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies>
-
编写yml文件:bootstrap.yml
# 服务端口号
server:
port: 7777
# 服务名
spring:
application:
name: user-service
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/cloud_db5?useUnicode=true&characterEncoding=utf8
username: root
password: 1234
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848 # nacos 服务地址
prefix: user # 3.1 前缀,默认 ${spring.application.name}
file-extension: yaml # 3.2 后缀 , user.yaml
group: DEFAULT_GROUP # 3.3 组名
#开启log4j打印SQL语句
logging:
level:
com:
czxy:
changgou4:
mapper: debug
# mp日志打印
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
编写启动类
@SpringBootApplication
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class,args);
}
}
初始化数据库
create database cloud_db5;
use cloud_db5;
CREATE TABLE t_user(
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50)
);
create database cloud_db6;
use cloud_db6;
CREATE TABLE t_user(
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50)
);
编写domain:User
@TableName("t_user")
@Data
public class User {
@TableId(type = IdType.AUTO)
private Integer id;
private String username;
}
编写mapper
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
编写service
public interface UserService extends IService<User> {
}
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
编写controller
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@PostMapping
public String save(@RequestBody User user) {
// 保存
boolean result = userService.save(user);
// 返回
if(result) {
return "添加成功";
}
return "添加失败";
}
}
2)打包项目,并运行
- 修改pom文件,添加maven插件,确定启动类
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--启动类-->
<mainClass>com.czxy.changgou4.user.UserServiceApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
点击package进行打包
- 通过cmd运行程序
java -jar xxxx.jar
3) nacos配置,重新运行
- 目的:切换数据库
server.port: 7778
spring.datasource.url: jdbc:mysql://127.0.0.1:3306/cloud_db6?useUnicode=true&characterEncoding=utf8
- 重新运行jar,再测试
总结
本次总结在此告一段落,以后的分享会更加 -丰富~精彩。同时欢迎各位老师和同学们提出宝贵意见,共同进步。