WASHINGTON—The U.S. economy slowed but still grew at a solid clip in the second quarter as strong consumer spending offset a drop in business investment, keeping the decadelong expansion on track amid trade tensions and cooling global activity.

Gross domestic product, a broad measure of goods and services produced across the economy, rose at a 2.1% annual rate in the second quarter, adjusted for seasonality and inflation, the Commerce Department said Friday. That is down sharply from a 3.1% pace in the first quarter.

Businesses...