Soviet Foreign Minister Andrei Gromyko arrived in Madrid today and lunched with Prime Minister Felipe Gonzalez almost immediately after the Spanish leader had met with U.S. Ambassador-at-large Vernon Walters.

Gromyko flew to Madrid from Rome on the second leg of a European tour that is seen as designed to demonstrate stability in the Soviet power structure and quash speculation over the illness of Soviet leader Konstantin Chernenko.

The diplomatic sources said that Gromyko also would be seeking to gauge Spain's commitment to the Atlantic Alliance. Gonzalez, once opposed to NATO membership, now favors remaining within the alliance but has said that he will honor a campaign pledge and hold a referendum early next year on the issue.

The talks between Gonzalez and the Reagan administration's U.N. Ambassador-designate Walters appeared aimed at smoothing recent problems in U.S.-Spanish relations. An aide to Gonzalez said the discussions had covered a great deal of international ground of mutual interest. The Walters visit was preceded by a trip to Madrid by senior White House officials to complete details for President Reagan's visit here May 6.

A combination of factors, which included the expulsion from Spain of two U.S. diplomats on charges of spying and the revelation that Washington had contingency plans to stockpile nuclear weapons in Spain in an emergency, has caused considerable anti-U.S. comment recently in Spanish political and media circles.

An indicator of growing involvement in and acceptance of western positions by Gonzalez's Socialist government was a decision yesterday by the Cabinet to negotiate both with the United States and with NATO on measures to control the reexport from Spain of so-called dual technology that can be adapted for military use. The move was in answer to a longstanding Washington complaint that lax Spanish regulations allowed access to sensitive high technology by Soviet Bloc countries.

Spanish officials said they did not expect the talks with Gromyko to focus on Madrid's NATO membership and they indicated that Moscow is resigned to Spain's presence in the alliance.

During a previous official trip to Madrid six years ago, when Spain had not yet entered NATO, the Soviet foreign minister was reported to have argued strongly against alliance membership on the ground that it would upset the balance between East and West.

Gromyko is expected, however, to seek assurances that the NATO referendum will be held as well as further information on Gonzalez's present policy of remaining within NATO but outside the alliance's military command structure, sources said.

In remarks to journalists after welcoming Gromyko at the airport, Foreign Minister Francisco Moran said there was no agenda for his talks with Gromyko. He said Spanish and Soviet officials would be discussing both bilateral and international issues.

The visit appeared deliberately low-key. Officials said there were no plans for a news conference or for a joint statement at the end of the Soviet foreign minister's stay. Three agreements were due to be signed dealing with reciprocal tax allowances, cultural links and research facilities for historians of Spanish-Soviet relations.

The low profile reflects the current state of relations between the two countries. Diplomatic relations between Spain and the Soviet Union were restored in 1977 after the death of Generalissimo Francisco Franco, but they have taken a back seat as Spain has pursued a prowestern foreign policy during the past decade.

Spanish diplomatic sources said that one-time attempts by Moscow to increase the presence of the Soviet fishing fleet using the Canary Islands as a base had been resisted. Another Soviet idea that involved having base facilities for its fishing fleet in the mainland Spanish port of Algeciras, close to both the British base of Gibraltar and the large U.S. Navy depot of Rota, had also been dropped.

In both instances security considerations had outweighed economic factors, which included access for Spanish trawlers to Soviet fishing grounds.