当前位置:网站首页>[micro Service ~ advanced] configuration center practice
[micro Service ~ advanced] configuration center practice
2022-07-19 13:19:00 【Classmate Tao Ran】

Here is 【 Microservices ~ senior 】, Pay attention to me to learn micro services without getting lost
If it helps you , Give the blogger a free praise to show encouragement
You are welcome to comment on the collection ️
Column introduction
【 Microservices ~ senior 】 At present, it mainly updates micro services , Learn together and progress together .
Introduction to this issue
This issue mainly introduces the actual combat of the configuration center
List of articles
demand
Set up user testing service
Basic environment
Project name :nacos-config-mysql-2.1
Add coordinates
<dependencies>
<!-- web starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- nacos To configure -->
<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 drive -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--lombok , @Data etc. -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
To write yml file :bootstrap.yml
# Service port number
server:
port: 7777
# service name
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 Service address
prefix: user # 3.1 Prefix , Default ${spring.application.name}
file-extension: yaml # 3.2 suffix
group: DEFAULT_GROUP # 3.3 Group name
# Turn on log4j Print SQL sentence
logging:
level:
com:
czxy:
changgou4:
mapper: debug
# mp Log printing
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- To write sql sentence
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)
);Write the startup class

package com.czxy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class,args);
}
}
Copy configuration class

- To write domain
package com.czxy.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@TableName("t_user")
@Data
public class User {
@TableId(type = IdType.AUTO) // Auto enhance
private Integer id;
private String username;
}
- To write mapper
package com.czxy.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.czxy.domain.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
To write service
Interface
package com.czxy.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.czxy.domain.User;
public interface UserService extends IService<User> {
}- Implementation class
package com.czxy.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czxy.domain.User;
import com.czxy.mapper.UserMapper;
import com.czxy.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}- To write controller
package com.czxy.controller;
import com.czxy.domain.User;
import com.czxy.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@PostMapping
public String save(@RequestBody User user) {
// add to
boolean result = userService.save(user);
// return
if(result) {
return " Add success ";
}
return " Add failure ";
}
}
Test configuration
Default
Provide database :cloud_db5
Yes t_user Table operation

Project package
With jar Run as a package ( Operations staff )
modify pom.xml file , Add the plug-in , Is used to specify the jar The start of class
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- Start class -->
<mainClass>com.czxy.UserApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>pack

function jar, stay cmd In the command window
java -jar nacos-config-mysql-2.1-1.0-SNAPSHOT.jar

nacos Configuration database
Provide database :cloud_db6
stay nacos Middle configuration

server.port: 7776
spring.datasource.url: jdbc:mysql://127.0.0.1:3306/cloud_db6?useUnicode=true&characterEncoding=utf8Must operate : Restart the service 【 No need to modify the source code 】
test

边栏推荐
- [dynamic planning]dp27 jumping game (II) - medium
- 大家好,问一下数据库没开始binlog如何实时同步么,有没有好的方案
- JVM self study summary
- VMware imports ova/ovf virtual machine files
- 模块7(王者荣耀商城异地多活架构设计)
- Code after annotation of hands-on deep learning (Second Edition) [continuous update]
- How can MySQL delete data tables and associated data tables
- XML文件解析
- Array simulation queue
- Azkaban installation documentation
猜你喜欢

In depth sorting: summary of machine learning modeling and parameter adjustment methods

【错误记录/selectpicker】dropdown menu显示位置出现偏移

A general memory management driver code is sorted out

XML file parsing

响应式织梦模板物流货运服务类网站

LeetCode 0118. 杨辉三角

Unveiling secrets of matrixcube 101 - functions and architecture of matrixcube
![[Tencent blue whale] the seventh 7.24 operation and maintenance day holiday greetings ~ come and make a wish~](/img/60/ab96abd599230078b19abad0aecba2.png)
[Tencent blue whale] the seventh 7.24 operation and maintenance day holiday greetings ~ come and make a wish~

C语言进阶——自定义类型:结构体 枚举 联合

动手学深度学习(第二版)注释后代码【持续更新】
随机推荐
Advanced C language -- custom type: structure enumeration Union
2022全球开发者薪资曝光:中国排第19名,平均年薪23,790美元
[pyGame learning notes] 5 Collision detection of rect objects
Is it safe for Everbright futures to open an account online? Are there any account opening guidelines?
2022年最新吉林建筑安全员模拟题库及答案
CMOS开关学习(一)
Wrong again, byte alignment and the use of pragma pack
如何在MFC中添加一个线程
深度梳理:机器学习建模调参方法总结
Metal organic framework / nitrogen carbide nano sheet (uio-66/hocn) composite | mil-101 loaded Au Pd alloy nanoparticles | chemical reagent MOF customization
Advanced C language -- character function and string function
In depth sorting: summary of machine learning modeling and parameter adjustment methods
Equivalent domain name
How can MySQL delete data tables and associated data tables
Supported metal organic framework zif-8 / graphene oxide hydrogen storage material | titanium dioxide /zif-8 composite | silicon dioxide @zif8 nano material
最小交換次數
Nombre minimal d'échanges
[error record /selectpicker] the display position of dropdown menu is offset
Growth of operation and maintenance Xiaobai - week 6 of Architecture
Can you view MySQL data table structure in two ways?