Introducing defects in photonic crystals is a common method for manipulating and controlling the propagation of electromagnetic waves. By introducing defects in photonic crystal waveguides, the periodicity of the waveguide structure can be disrupted, local modes can be formed, and resonant cavity functions can be achieved. In this study, we designed two groups of two-dimensional valley photonic crystal waveguides, each of which uses different methods to introduce defects and obtained different resonant cavity structures, and designed a resonant cavity sensor. We conducted a detailed theoretical analysis of the resonant cavity through simulation software. In addition, we fabricated the samples and conducted microwave experiments to demonstrate the accuracy of our theoretical research. Our research provides guidance for the application of photonic crystal devices.