The easiest defense I've used that isn't just walling myself off is to build an entrance that forces invaders to go through a large open area and to give every dwarf who isn't a miner, woodcutter or military melee specialist a crossbow. It's not quite as effective as some other methods, but once you have enough crossbows and a bolt-making industry running, it's pretty much hands-off. Small attacks are shot to pieces without any intervention, and for large attacks you can station your entire fortress population to shoot at the invaders. You still want to have a small professional military with melee specialists for dispatching the wounded and handling enemy ranged units, your main threat.
I've used a variety of other techniques depending on the local geography and my whim. The method I've used most is a single entry path that funnels invaders past archers, around curves and into a killing zone where professional soldiers practice their craft. I may have multiple outer entrances (e.g. into a walled courtyard), but I almost never have more than one way past the core choke points; when I do, it's either a temporary entrance for tower construction that'll be sealed as soon as possible, or it's an emergency exit sealed with redundant bridges.
In my favorite example, my main above-ground presence was a squad marble tower. Merchants and invaders would come into a gate on the south side, an area that was usually defended by archers, then curve to the left, descend a ramp, go left again, down another ramp, and then around a U-bend before reaching a temporary wood stockpile and the trade depot. The archers would scare off small groups of attackers, and there were a few cage traps on the inside curves to ensure a supply of prisoners for sacrifice. Past the trade depot, the path curved again, joined up with paths from two separate mineshafts, and then came past the two-story security office before reaching the main fortress. A mechanism was in place to flood the mineshafts with water from the aquifier.
During the first and the last month of each season, I kept archers in the tower. I rotated my primary melee squads with one month off, one month training and one month on patrol. At all times there were at least three melee troops guarding the trade depot, with more back in the security office. The guards would deal with small incursions, and the standby troops would surge around the corner to handle bigger attacks. When I sent my standby troops in, I called off-duty and reserve units to the security office in case I needed reinforcements.
Had an attacker ever made it past this, I'd have had my civilians retreat across the covered bridge into the housing area, which was stocked with years of food and had a secure cistern. I had military alerts which would have called surviving militia and Fortress Guard to defend the retreat and protect the choke point. I only used this procedure once, out of an abundance of caution during a flying-titan attack; in the end, marksdwarves shot the titan down and melee troops finished him before he reached the secure area.
Another fortress used a similar defense: this time, if I didn't pull the levers to cover the surface with lava, invaders advanced up a hill under fire from my battlements and into the entry tunnel. To cover the tunnel from outside fire, the entry tunnel's entrance was on the north, but the tunnel itself went west. I had a few cage traps here to provide prisoners, and since I couldn't cover the area with fire from the battlements, I installed reciprocating glass spikes to dislodge anyone who got stuck here. The entry tunnel went into the hill and then down a ramp, through the main aquifer piercing and down to the killing level.
On the killing level, I could optionally raise bridges to send intruders through a trapped path. It started out with a mix of weapon traps & reciprocating spikes before narrowing into a dodge-me trap with a four or five z-level fall onto spikes. I could optionally start up an auxiliary pump which would fill the trap's landing area with magma. After the dodge-me trap, the path curved left in a U-bend into an area where I'd chained some of my cave crocodiles.
Regardless of which path I left open, intruders would reach another U-bend meant to protect from archers. After the U-bend was the killing zone where I stationed my melee troops. I'd originally planned to put a ballista behind fortifications at the end of the corridor, but I ended up using that space for archers. After this area, the path curved down another spiral ramp before reaching the trade depot and the security office
I had problems with troops pursuing fleeing enemies around the corner and out of view of my ranged support. Melee attrition (against modded enemies) was greater than I wanted, which is why I added the dodge-me traps and the cave crocodiles; sadly, the crocodiles never did taste blood because the traps were too effective. Even though some elite enemies could get past the traps, invaders usually decided to retreat after a squad or two had been shredded.