フェンネル豆を書く方法はいくつかありますか?別名マイクロサービスが相互に呼び出すためのいくつかのメソッド

最近、このプロジェクトは他のシステムとの共同デバッグを行っており、1つのシステムに2つのマイクロサービスがあります。そこで、マイクロサービス間で相互に呼び出す3つの方法について簡単に説明します。
最初は最も簡単です。@FeignClientアノテーションを使用します。
まず、独自のマイクロサービスのメインスタートアップクラスでFeignサービスを自動的に開始するためのアノテーションを追加する必要があります。
次に、インターフェースを定義し、@ FeignClientアノテーションをインターフェースに追加し、マイクロサービスのコントローラーレイヤーメソッドを抽象メソッド、パス、パラメーター、リクエストタイプに呼び出すように抽象化します一貫している。
最後に、マイクロサービスを呼び出す必要がある場所で、独自のメソッドを呼び出すのと同じように、この抽象クラスを挿入して直接使用します。コードは次のとおり
です。パッケージcom.example.demo;

import java.util.Map;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient( "euroakeに登録されるマイクロサービスの名前")
@RequestMapping(値= "呼び出される
マイクロサービスのコントローラークラスは同じ")パブリックインターフェイスIMsService {

// 此处的路径和要调用的微服务的controller配置的路径一致
@RequestMapping(value = "/search/getRes", method = RequestMethod.POST)
Map<String,Object> getRes(@RequestBody Map<String,Object> map);

}
RestTemplate法により、第2、。restTemplateメソッドを定義し、それをパラメーターとして使用します。コードは以下の通りです。
import java.net.URI;
import java.net.URISyntaxException;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http。*;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

パブリッククラスRestTemplateService {

private String requestCloudRest(String url,String para,HttpMethod method) throws URISyntaxException {
	HttpHeaders headers = new HttpHeaders();
	RestTemplate restTemplate = new RestTemplate(); 
	headers.setContentType(MediaType.valueOf("application/json;charset=utf-8"));
	ParameterizedTypeReference<String> responseType 
	= new ParameterizedTypeReference<String>() {};
	RequestEntity<String> request = null;
	ResponseEntity<String> response = null;
	if (HttpMethod.GET.equals(method) && StringUtils.isEmpty(para)) {
		request = new RequestEntity<String>(headers,method,new URI(url));
		response = restTemplate.exchange(request,responseType);
	} else {
		request = new RequestEntity<String>(para,headers,method,new URI(url));
		response = restTemplate.exchange(request,responseType);
	}
	String res = response.getBody();
	return res ;
}

}

3つ目は、石鹸の管理です。

元の記事を6件公開 ・いい ね0 訪問数354

おすすめ

転載: blog.csdn.net/weixin_44863376/article/details/105422688