《SpringCloud与Docker高并发微服务架构实施》-1:运行测试


前言

本系列文章为陈韶健老师的《SpringCloud与Docker高并发微服务架构实施》的代码实现,仅供个人学习交流所用,如有侵权必删。


一、开发搭建

使用IDEA,搭建SpringBoot项目
选择下图
在这里插入图片描述

pom,xml:如下

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>spring-boot-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-demo</name>
    <description>spring-boot-demo</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.example.springbootdemo.SpringBootDemoApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

使用JPA

JPA教程提供了Java持久性API的基本和高级概念。此JPA教程专为初学者和专业人士设计。
JPA只是一个简化对象关系映射来管理Java应用程序中的关系数	据的规范。 它提供了一个平台,可以直接使用对象而不是使用SQL语句。//更多请阅读:https://www.yiibai.com/jpa/

数据库配置

在Mysql中新建一个test数据库,配置如下

# 数据库驱动:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据源名称
spring.datasource.name=defaultDataSource
# 数据库连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC
# 数据库用户名&密码:
spring.datasource.username=root
spring.datasource.password=123456

JPA配置

下面是JPA配置,配置中我们将“ddl-auto”设置为“update”,表示将根据实体设计在程序运行时自动更新表结构。

#是否在控制台打印JPA执行过程生成的SQL
spring.jpa.show-sql=true
#表示JPA对应的数据库是MySQL
spring.jpa.database=mysql
#表示在项目启动时根据实体类更新数据库中的表
spring.jpa.hibernate.ddl-auto=update
#表示使用的数据库方言是MySQL57Dialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect

jpa的使用需要对jpa进行配置,用来扫描实体类和dao接口。新建一个配置类

@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
@EnableJpaRepositories(basePackages = "com.**.dao")
@EntityScan(basePackages = "com.**.entity")
public class JpaConfiguration {
	@Bean
	PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor(){
		return new PersistenceExceptionTranslationPostProcessor();
	}
}

@EntityScan是扫描实体类的路径,@EnableJpaRepositories是扫描dao层的路径

数据实体类设计

@Entity
@Table(name = "country")
@Data
public class Country {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	private String countryName;
	private String countryCode;
}

@Table是指定生成的表名

dao接口设计

@Repository
public interface CountryDao extends JpaRepository<Country, Long> {
}

单元测试

@SpringBootTest
@ContextConfiguration(classes = {JpaConfiguration.class,SpringBootDemoApplication.class})
class SpringBootDemoApplicationTests {
	@Autowired
	private CountryDao countryDao;

	public void createCountry(){
		Country country=new Country();
		country.setCountryCode("86");
		country.setCountryName("中国");
		countryDao.save(country);
		assert country.getId()>0:"create error";
	}
	@Test
	void contextLoads() {
		createCountry();
		List<Country> countryList=countryDao.findAll();
		for (Country country:countryList){
			System.out.println("=====country name="+country.getCountryName());
		}
	}

}

如果运行成功并在控制台打印出“=====country name=中国”说明测试成功。

前台使用Thymeleaf

在创建springBoot项目时只要勾选Thymeleaf就会有如下配置

# THYMELEAF (ThymeleafAutoConfiguration)
# 开启模板缓存(默认值: true )
spring.thymeleaf.cache=true
# 检查模板是否存在,然后再呈现
spring.thymeleaf.check-template=true
# 检查模板位置是否正确(默认值 :true )
spring.thymeleaf.check-template-location=true
#Content-Type 的值(默认值: text/html )
spring.thymeleaf.content-type=text/html
# 开启 MVC Thymeleaf 视图解析(默认值: true )
spring.thymeleaf.enabled=true
# 模板编码
spring.thymeleaf.encoding=UTF-8
# 要被排除在解析之外的视图名称列表,⽤逗号分隔
spring.thymeleaf.excluded-view-names=
# 要运⽤于模板之上的模板模式。另⻅ StandardTemplate-ModeHandlers( 默认值: HTML5)
spring.thymeleaf.mode=HTML5
# 在构建 URL 时添加到视图名称前的前缀(默认值: classpath:/templates/ )
spring.thymeleaf.prefix=classpath:/templates/
# 在构建 URL 时添加到视图名称后的后缀(默认值: .html )
spring.thymeleaf.suffix=.html

Controller实现

@Controller
public class CountryController {
	@Autowired
	private CountryDao countryDao;

	@RequestMapping(value = "/")
	public String test(ModelMap modelMap){
		List<Country> list= countryDao.findAll();
		modelMap.addAttribute("countrys",list);
		return "index";
	}
}

页面实现

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
    <meta charset="UTF-8">
    <title>国家代码</title>
</head>
<body>
    <!--/*@thymesVar id="countrys" type="com"*/-->
    <ul th:each="country:${countrys}">
        <li>
            <p th:text="'代码:'+${country.countryCode}"></p>
            <p th:text="'国家:'+${country.countryName}"></p>
        </li>
    </ul>
</body>
</html>

运行

成功启动项目后在访问http://localhost:8080/在页面中显示在这里插入图片描述成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
《基于docker容器的高并发web系统架构设计与实现》随着互联网迅速发展,社交、媒体以及电商等web网站用户数量 越来越大,并发流量也越来越高,这对于传统web系统架构设计提出 新的挑战。本文基于docker容器虚拟化技术来设计实现高并发web系 统架构,实现web系统的高并发、易扩展以及提升系统资源利用均衡 率等功能。 本文基于docker容器以及Kubemetes容器集群技术,从负载均 衡、弹性伸缩以及资源调度等方面设计实现容器化高并发web系统架 构。设计实现基于工作负载特性的动态负载均衡策略,能够实现根据 不同负载类型以及容器集群资源利用率而实时调整容器集群服务的 权重;设计实现基于灰度模型短时间负载预测弹性伸缩策略,能够实 现高效容器集群弹性伸缩以及提升系统并发性能;设计实现基于蚁群 算法并行调度策略,能够有效提升容器集群整体调度效果,提高容器 服务集群的可用性以及改善系统的资源利用均衡率等。 基于docker容器的高并发web系统架构能够实现系统的高并发、 易扩展以及提升系统集群的资源利用率等功能。经过系统测试分析, 基于工作负载特性的动态负载均衡策略比传统轮询、加权轮询策略在 高并发流量下有更好的吞吐量以及响应时间等性能表现,基于灰度模 型预测的弹性伸缩机制比传统KubemetesHPA机制在短时间内具有 更好的集群伸缩特性以及基于蚁群算法的并行容器调度策略比传统 Kubemetesdefaults策略有更高的调度优势,能够有效提升容器的均 匀分发,实现系统高可用性以及提升系统的资源利用均衡率等。 基于docker容器技术高并发web系统架构设计,能够有效解决 当前高并发流量下web系统流量转发、集群伸缩以及资源调度等问 题,对于容器技术的具体应用研究以及高并发系统架构的设计具有一 定的实用价值。 关键词:高并发web系统,容器虚拟化,动态负载均衡,弹性伸 缩,并行资源调度

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值