一篇打通微服務架構,Nacos + Gateway + Redis + MySQL + Docker
大家好,我是哪吒。
本系列為SpringCloud微服務系列,上一期分享了 圖解Nginx,系統架構演變 + Nginx反向代理與負載均衡,今天分享一篇一站式微服務架構,讀哪吒程式設計,品技術人生。
一、前期準備
本專案暫定專案名NZBCProject,SpringBoot + Vue構建,具體專案內容未定。
基本元件Nginx、Gateway、Nacos、Sentinel、Ribbon、Feign、Seata、Redis、RabbitMQ、MySQL、docker、Vue。
1、安裝MySQL5.7
2、安裝nacos
我安裝的是window版的nacos和MySQL,安裝nacos時需要注意,要講配置檔案中的叢集版改為單機版,才能啟動!
3、安裝Redis
二、建立父工程
我覺得主要是pom檔案
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="
xmlns:xsi="
xsi:schemaLocation=">
<modelVersion>4.0.0</modelVersion>
<groupId>com.guor</groupId>
<artifactId>NZBCProject</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>01common</module>
<module>02gateway</module>
</modules>
<properties>
<system.version>1.0.0</system.version>
<system.ip>127.0.0.1</system.ip>
<system.sport>808</system.sport>
<system.mode>http</system.mode>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
<skipTests>true</skipTests>
<nacos.version>0.2.2.RELEASE</nacos.version>
</properties>
<packaging>pom</packaging>
<name>NZBCProject</name>
<description>This is parent project</description>
<!-- 本專案的父模組使用spring-boot框架 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.23</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${nacos.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
三、建立gateway子工程
1、pom檔案
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="
xmlns:xsi="
xsi:schemaLocation=">
<parent>
<artifactId>NZBCProject</artifactId>
<groupId>com.guor</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>02gateway</artifactId>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>gateway-${system.version}</finalName>
</build>
</project>
2、配置檔案
spring:
application:
name: gateway
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
file-extension: yml
ext-config:
- data-id: datasource-share-config.yml
group: SHARE_GROUP
refresh: true
- data-id: log-share-config.yml
group: SHARE_GROUP
refresh: true
(1)gateway.yml
server:
port: 8080
spring:
application:
name: gateway
version: 1.0.0
cloud:
gateway:
discovery:
locator:
enabled: true
lowerCaseServiceId: true
filters:
- StripPrefix=1
routes:
- id: management
uri: lb:management # 服務端 service_id
predicates:
- Path=/management/**
filters:
- name: Hystrix
args:
name: fallbackcmd
fallbackUri: forward:/defaultFallback
- id: demo
uri: lb://demo
predicates:
- Path=/demo/**
hystrix:
command:
default:
execution:
isolation:
strategy: SEMAPHORE
thread:
timeoutInMilliseconds: 1500
(2)datasource-share-config.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/blue?serverTimezone=UTC
driverClassName: com.mysql.cj.jdbc.Driver
username: root
password: root
mybatis:
typeAliasesPackage: com.guor.*.bean.**
mapperLocations: classpath*:**/com/guor/**/dao/mapping/*Mapper.xml
configuration:
map-underscore-to-camel-case: true
(3)log-share-config.yml
logging:
path: logs
level:
root: info
com.alibaba.nacos.client.naming: warn
file:
max-size: 20MB
max-history: 30
pattern:
file: "%d{${LOG_DATEFORMAT_PATTERN:yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:%5p} ${PID:- } --- [%15.15t] %-40.40logger{39} [%5.5line] : %m%n${LOG_EXCEPTION_CONVERSION_WORD:%wEx}"
console: "%d{${LOG_DATEFORMAT_PATTERN:yyyy-MM-dd HH:mm:ss.SSS}} %clr(${LOG_LEVEL_PATTERN:%5p}) %clr(${PID:- }){magenta} --- %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr([%5.5line]){cyan} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:%wEx}"
3、啟動類
package com.guor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableDiscoveryClient
@EnableScheduling
@RefreshScope
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
System.out.println("hello world");
}
}
四、建立management管理模組
1、pom檔案
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="
xmlns:xsi="
xsi:schemaLocation=">
<parent>
<artifactId>NZBCProject</artifactId>
<groupId>com.guor</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>03management</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--MySQL依賴 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
<build>
<finalName>management-${system.version}</finalName>
</build>
</project>
2、配置檔案
spring:
application:
name: management
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
file-extension: yml
ext-config:
- data-id: datasource-share-config.yml
group: SHARE_GROUP
refresh: true
- data-id: log-share-config.yml
group: SHARE_GROUP
refresh: true
server:
port: 8081
spring:
application:
name: management
version: 1.0.0
mvc:
static-path-pattern: /management/**
resources:
static-locations:
- file:../../web/management
- file:../../web/common
3、啟動類
package com.guor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
@EnableDiscoveryClient
@SpringBootApplication(scanBasePackages = "com.guor")
@MapperScan("com.guor.management.dao")
@RefreshScope
public class ManagementApplication {
public static void main(String[] args) {
SpringApplication.run(ManagementApplication.class, args);
}
}
五、整合mybatis
1、user表設計
資料庫選擇的是最常用的MySQL
CREATE TABLE `user` (
`user_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(100) NOT NULL,
`password` varchar(40) NOT NULL,
`age` int(11) DEFAULT NULL,
`sex` int(11) DEFAULT NULL,
`telephone` varchar(100) DEFAULT NULL,
`address` varchar(100) DEFAULT NULL,
`create_date` date DEFAULT NULL,
`update_date` date DEFAULT NULL,
`deleted` int(11) DEFAULT NULL,
`version` int(11) DEFAULT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
2、UserController
package com.guor.management.controller;
import com.guor.management.bean.User;
import com.guor.management.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/getUserList")
public List<User> getUserList(){
return userService.getUserList();
}
@PutMapping("/insertUser")
public void insertUser(@RequestBody User user){
userService.insertUser(user);
}
}
3、UserService
package com.guor.management.service;
import com.guor.management.bean.User;
import java.util.List;
public interface UserService {
List<User> getUserList();
void insertUser(User user);
}
package com.guor.management.service.impl;
import com.guor.management.bean.User;
import com.guor.management.dao.UserMapper;
import com.guor.management.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> getUserList() {
return userMapper.getUserList();
}
@Override
public void insertUser(User user) {
userMapper.insertUser(user);
}
}
4、UserMapper
package com.guor.management.dao;
import com.guor.management.bean.User;
import java.util.List;
public interface UserMapper {
public List<User> getUserList();
public void insertUser(User user);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" " >
<mapper namespace="com.guor.management.dao.UserMapper">
<select id="getUserList" resultType="java.util.LinkedHashMap">
select * from user;
</select>
<insert id="insertUser" parameterType="com.guor.management.bean.User">
INSERT INTO gooreey.`user` (username, password) VALUES (#{username}, #{password});
</insert>
</mapper>
5、User
package com.guor.management.bean;
import com.guor.base.bean.BaseBean;
import lombok.Data;
@Data
public class User extends BaseBean {
private Integer userId;
private String username;
private String password;
private Integer age;
private Integer sex;
private String telephone;
private String address;
}
package com.guor.base.bean;
import lombok.Data;
import java.util.Date;
@Data
public class BaseBean {
private Date createDate;
private Date updateDate;
private Integer deleted;
private Integer version;
}
6、postman介面測試
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70024924/viewspace-2937155/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 微服務架構 | 3.2 Alibaba Nacos 註冊中心微服務架構
- 微服務分散式架構之redis篇微服務分散式架構Redis
- spring cloud微服務分散式雲架構-Gateway入門SpringCloud微服務分散式架構Gateway
- 微服務架構 | 2.2 Alibaba Nacos 的統一配置管理微服務架構
- 微服務架構微服務架構
- 微服務2:微服務全景架構微服務架構
- 微服務架構 | *3.5 Nacos 服務註冊與發現的原始碼分析微服務架構原始碼
- 微服務架構:構建PHP微服務生態微服務架構PHP
- 微服務架構初探微服務架構
- 慎用 “微服務” 架構微服務架構
- 【微服務】docker安裝mysql微服務DockerMySql
- 詳解Spring Cloud和Docker的微服務架構SpringCloudDocker微服務架構
- 微服務架構的Akka實現和Docker部署案例微服務架構Docker
- 基於Redis構建微服務的反應式架構 - bitsrcRedis微服務架構
- 一篇故事告訴你什麼是微服務架構!微服務架構
- 單體架構&微服務架構&中臺服務架構架構微服務
- 架構演進之「微服務架構」架構微服務
- 架構之:微服務架構漫談架構微服務
- 如何構建微服務架構微服務架構
- 微服務架構(一):什麼是微服務微服務架構
- [雲原生微服務架構](十)微服務架構的基礎知識微服務架構
- 微服務架構初識微服務架構
- 微服務架構詳談微服務架構
- 微服務核心架構梳理微服務架構
- 微服務與架構師微服務架構
- 聊聊微服務架構思想微服務架構
- 微服務 dubbospring 架構微服務Spring架構
- 微服務架構簡介微服務架構
- 軟體架構模式之微服務架構架構模式微服務
- 微服務實戰(八)整合Sentinel閘道器服務限流功能 SpringCloud GateWay + Sentinel + Nacos微服務SpringGCCloudGateway
- 微服務架構—服務降級微服務架構
- 打通MySQL架構和業務的任督二脈MySql架構
- dubbo+zookeeper+springmvc+mybatis+shiro+redis微服務雲架構SpringMVCMyBatisRedis微服務架構
- 微服務雲架構dubbo+zookeeper+springmvc+mybatis+shiro+redis微服務架構SpringMVCMyBatisRedis
- 微服務架構學習與思考(05):微服務架構適用場景分析微服務架構
- SOA架構和微服務架構的區別架構微服務
- 微服務架構和設計模式 - DZone微服務微服務架構設計模式
- 微服務開發攻略之淺析微服務架構微服務架構