前回の記事でサービスプロバイダーの作成方法を学習したので、今回はサービスコンシューマーを作成します 実装アイデアは、Spring Boot を通じてマイクロサービスアプリケーションを構築し、それを Eureka を通じて登録センター Eureka に登録することです。クライアント サーバーはサービス コンシューマになります。では、サービス コンシューマはどのようにしてサービス プロバイダのインターフェイスを呼び出すのでしょうか? 次に、最初にコンポーネント RestTemplate の使用を紹介します。
1.RestTemplateとは
RestTemplate は Spring フレームワークによって提供される REST ベースのサービス コンポーネントであり、基礎となる層は HTTP リクエストとレスポンスをカプセル化し、コード開発を簡素化できる RETS サービスにアクセスするための多くのメソッドを提供します。
2. RestTemplateの使い方
- Maven プロジェクトをサブサービスとして作成する
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
- application.ymlを作成します。コードは次のとおりです
server:
port: 8080
spring:
application:
name: consumer
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
- サービスプロバイダーと同じエンティティクラスを作成します。コードは次のとおりです。
package com.frr.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data //生成Getter,Setter,equals,canEqual,hasCode,toString等方法
@AllArgsConstructor //添加一个构造函数,该构造函数含有所有已声明字段属性参数
@NoArgsConstructor //创建一个无参构造函数
public class Student {
private long id;
private String name;
private int age;
}
- コントローラーを作成し、メソッド内でサービス プロバイダーのインターフェイスを呼び出します。コードは次のとおりです。
package com.frr.controller;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.frr.entity.Student;
@RestController
@RequestMapping("/consumer")
public class RestTemplateController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/findAll")
@SuppressWarnings("unchecked")
public Collection<Student> findAll(){
return restTemplate.getForEntity("http://localhost:8010/student/findAll", Collection.class).getBody();
}
@SuppressWarnings("unchecked")
@GetMapping("/findAll2")
public Collection<Student> findAll2(){
return restTemplate.getForObject("http://localhost:8010/student/findAll",Collection.class);
}
@GetMapping("/findById/{id}")
public Student findById(@PathVariable("id") long id){
return restTemplate.getForEntity("http://localhost:8010/student/findById/{id}",Student.class,id).getBody();
}
@GetMapping("/findById2/{id}")
public Student findById2(@PathVariable("id") long id){
return restTemplate.getForObject("http://localhost:8010/student/findById/{id}",Student.class,id);
}
@PostMapping("/save")
public void save(@RequestBody Student student){
restTemplate.postForEntity("http://localhost:8010/student/save",student,null).getBody();
}
@PostMapping("/save2")
public void save2(@RequestBody Student student){
restTemplate.postForObject("http://localhost:8010/student/save",student,null);
}
@PutMapping("/update")
public void update(@RequestBody Student student){
restTemplate.put("http://localhost:8010/student/update",student);
}
@DeleteMapping("/deleteById/{id}")
public void deleteById(@PathVariable("id") long id){
restTemplate.delete("http://localhost:8010/student/deleteById/{id}",id);
}
}
- スタートアップクラスを作成するには、RestTemplateのインスタンスをスタートアップクラスに注入する必要があることに注意してください。ここでは@Beanのメソッドを使用し、コードは次のようになります
package com.frr;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class RestTemplateApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(RestTemplateApplication.class, args);
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
-
登録センター、サービス プロバイダー、サービス コンシューマを順番に起動し、Postman ツールを使用してテストします。異なるリクエストでも同じデータを取得できます。
- 8010 サービス プロバイダーの findAll インターフェイスは次のようなデータを返します
- 8080 サービス コンシューマの findAll インターフェイスによって返されるデータは次のとおりです。
- 結論: 返されるデータには一貫性があり、サービス コンシューマはサービス プロバイダのインターフェイスを正常に呼び出します。
6. まとめ
コードの観点から見ると、サービス プロバイダーとサービス コンシューマーの間に大きな違いはありません。これらはすべて Spring Boot プロジェクトです。これに基づいて、Spring Cloud のいくつかのコンポーネントを通じて、サービス プロバイダーとサービス コンシューマーに異なる ID を持たせます。サービスプロバイダーは外部アクセス用のインターフェースを提供し、サービスコンシューマも外部アクセス用のインターフェースを提供しますが、インターフェースは他のサービスを呼び出すためのインターフェースなので、ある意味サービスコンシューマもサービスプロバイダーです。また、サービスコンシューマからのサービスも消費します。したがって、サービス間に絶対的なプロバイダーとコンシューマーは存在せず、相互に呼び出すことができます。
マイクロサービス アーキテクチャでは、多くのサービスが存在するため、統合されたアクセス ポータルが必要になります。それをどのように実現するかについては、次の記事を楽しみにしていてください。
開発エンジニアの一人も継続学習段階にあり、普段のちょっとした体験談を随時共有しています。私の書いた文章を読んでくれた方が、寄り道をせずに仕事や勉強の成功を祈っていただければ幸いです。
ブロガーの経験は限られていますが、欠点がある場合は、コミュニケーションを取り、一緒に改善することを歓迎します。同じ CSDN の皆さんと一緒に進歩していきたいと思っています。著者 | スイートリトルスイートポテト
プロデュース | リトルスイートポテト