赞
踩
电商首页的内容也是需要做高并发的处理,Canal就可以很好的解决这个问题,下面我们实现Canal广告微服务,并进行高并发的测试
搭建一个Maven项目如下结构
POM
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springcloud-nacos</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>advertise</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<!--add-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<dependencies>
<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-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>cn.itlym.shoulder</groupId>
<artifactId>lombok</artifactId>
<version>0.1</version>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.69</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
</project>
YML
server:
port: 14000
spring:
cloud:
nacos:
discovery:
service: adv-serv
server-addr: localhost:8848
sentinel:
transport:
dashboard: localhost:8080
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
username: root
password: fengzhizi98!
redis:
host: localhost
port: 6379
canal:
server: 127.0.0.1:11111
destination: example
Controller
package com.advertise.controller;
import com.advertise.entity.AdEntity;
import com.advertise.service.AdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class AdController {
@Autowired
private AdService adService;
@GetMapping("/ad")
@ResponseBody
public List<AdEntity> getAdList() {
return adService.getAdList();
}
}
AdService
package com.advertise.service;
import com.advertise.entity.AdEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Set;
@Service
public class AdService {
@Autowired
private RedisTemplate redisTemplate;
public static final String AD_LIST = "AD_LIST";
public List<AdEntity> getAdList() {
return redisTemplate.boundHashOps(AD_LIST).values();
}
public void modify(AdEntity adEntity) {
redisTemplate.boundHashOps(AD_LIST).put(adEntity.getId(),adEntity);
}
public void delete(Integer id) {
redisTemplate.boundHashOps(AD_LIST).delete(id);
}
@Scheduled(fixedDelay = 2000)
public void prepareGood() {
Set keys = redisTemplate.boundHashOps(AD_LIST).keys();
int i = 0;
for (Object s : keys) {
i++;
}
System.out.println("广告剩余:"+ i);
}
}
AdHandle
package com.advertise.service;
import com.advertise.entity.AdEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.handler.EntryHandler;
@Component
public class AdHandle implements EntryHandler<AdEntity> {
@Autowired
private AdService adService;
@Override
public void insert(AdEntity adEntity) {
adService.modify(adEntity);
}
@Override
public void update(AdEntity before, AdEntity after) {
adService.modify(after);
}
@Override
public void delete(AdEntity adEntity) {
adService.delete(adEntity.getId());
}
}
AdEntity
package com.advertise.entity;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.io.Serializable;
@Data
@Entity
@Table(name = "ad")
@NoArgsConstructor
public class AdEntity implements Serializable {
private static final long serialVersionUID = 1l;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "click_url")
private String clickUrl;
@Column(name = "img_url")
private String imgUrl;
@Column(name = "status")
private String status;
@Column(name = "order")
private Integer order;
}
AdvertiseApplication
package com.advertise;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class AdvertiseApplication {
public static void main(String[] args) {
SpringApplication.run(AdvertiseApplication.class);
}
@Bean
public RedisTemplate<Object, Object> redisTemplate (RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(new StringRedisSerializer());
return template;
}
}
先启动Nacos,Redis,Sentinel,Canal,MySQL
Nacos
Redis
Canal
启动项目
修改数据库数据,接口可以实时获取最新数据,自测
数据库数据
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。