[英]Do we need to stub the other micro service in Spring cloud contract
[英]Spring Cloud microservice, consume password protected micro service with other micro service
目前我正在學習一些教程,用spring和neflix堆棧在java中創建微服務。 我遇到的一個問題是,我啟動的每項服務似乎都受到生成密碼的保護。 因此,另一個微服務不可能使用另一個微服務。 那么,一個微服務通過休息呼叫消費另一個微服務的最佳方式是什么? 我是否必須進一步調整application.yml以及如何?
這是一個例子(非常粗略和基本)。 我有一個微服務,使用以下功能調用另一個:
@RestController
@SpringBootApplication
public class BookstoreApplication {
@RequestMapping(value = "/recommended")
public String readingList(){
return "Spring in Action (Manning), Cloud Native Java (O'Reilly), Learning Spring Boot (Packt)";
}
public static void main(String[] args) {
System.setProperty("spring.config.name", "springTut2CircuitBreakers/circuitApplication");
SpringApplication.run(BookstoreApplication.class, args);
}
}
另一個微服務正在使用以下代碼監聽該調用:
@RestController
@SpringBootApplication
public class ReadingApplication {
@RequestMapping("/to-read")
public String readingList() {
RestTemplate restTemplate = new RestTemplate();
URI uri = URI.create("http://localhost:8090/recommended");
return restTemplate.getForObject(uri, String.class);
}
public static void main(String[] args) {
System.setProperty("spring.config.name", "springTut2CircuitBreakers/readingApplication");
SpringApplication.run(ReadingApplication.class, args);
}
}
當我嘗試使用瀏覽器的第一個服務時,我被要求輸入密碼。 當我進入時,我已經訪問它,但它顯示401錯誤,因為第一個服務無法訪問第二個(我相信)。 那么我該如何防止這種情況發生呢?
編輯:修復了復制粘貼錯誤(重復的代碼)
我認為在兩個服務中啟用安全性時,您需要在其余模板調用的標頭中發送會話ID,如下所示,
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Cookie", "JSESSIONID=" + session.getValue());
HttpEntity requestEntity = new HttpEntity(null, requestHeaders);
ResponseEntity rssResponse = restTemplate.exchange(
"http://localhost:8090/recommended",
HttpMethod.GET,
requestEntity,
Rss.class);
Rss rss = rssResponse.getBody();`
這可能有助於在您的微服務之間進行休息模板調用(此休息模板調用將相應更改)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.