Military? What military?
It's an exploration fleet.
However, much like myself, they recognize that the place they're in isn't a peaceful utopia where everyone gets along, and nobody wants to push others around just for being there. So of course they're armed and armored exploration vessels.
The ranks and protocol just make for standardized things across the fleet so people don't need 500 different ways of doing things, and can integrate with each other more easily. Ranks make chain of command clear in stressful situations.
In very few instances is starfleet's intent to end the life of anyone else. Why punch a hole through shields and beam a quantum torpedo into the enemy ship's engineering section when you want them to stop fighting and talk things out? Hard to do when you're dead.
Starfleet has similar patterns to militaries, but as a peaceful exploration focused group tries to use peaceful measures at every turn, even when their opponent has refused to listen every time.
Hell, even the romulans got help when they needed it despite centuries of constant threats, fleet action, intrusions into territory, subterfuge and sabotage, and outright hostility. The klingons are allies despite once being at war, and older generations still being distrusting of the federation.