SpringCloud (第002章) シンプルムービーマイクロサービスクラス (コンシューマ、プロバイダはユーザマイクロサービス)
1. 概要
マイクロサービスは Http プロトコルを通じて相互に通信します。
ユーザー マイクロサービスはプロバイダーとして機能し、ムービー マイクロサービスはコンシューマーとして機能し、ムービー マイクロサービスはユーザー マイクロサービスを消費します。
2. 導入手順
2.1 Maven リファレンス パッケージの追加
<?xml version="1.0"coding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3 .org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>springms-simple-consumer-movie</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>com. springms.cloud</groupId>
<artifactId>springms-spring-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<依存関係>
<!-- web模块 -->
<依存関係>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependency>
</project>
2.2 アプリケーション設定ファイルの追加(springms-simple-consumer-movie\src\main\resources\application.yml)
spring:
application:
name: springms-simple-consumer-movie #全部小写
server:
port: 8005
user:
userServicePath: http://localhost:8000/simple/
2.3 エンティティユーザークラスUserの追加(springms-simple-consumer-movie\src\main\java\com\springms\cloud\entity\User.java)
パッケージcom.springms.cloud.entity;
java.math.BigDecimalをインポートします。
パブリック クラス ユーザー {
プライベート ロング ID;
プライベート文字列のユーザー名。
プライベート文字列名。
非公開 年齢が低い。
プライベート BigDecimal 残高。
public Long getId() {
this.id を返します。
public void setId(Long id) {
this.id = id;
}
public
String getUsername() {
this.username を返します。
public
void setUsername(String username) {
this.username = ユーザー名;
public
String getName() {
this.name を返します。
this.name
= 名前;
}
public void setName(文字列名) {
public Short getAge() {
return this.age;
public
void setAge(Short age) {
this.age = age;
public
BigDecimal getBalance() {
return this.balance;
public
void setBalance(BigDecimal バランス) {
this.balance = バランス;
}
}
2.4 ムービー Web アクセス層コントローラーの追加 (springms-simple-consumer-movie\src\main\java\com\springms\cloud\controller\MsSimpleConsumerMovieController.java)
パッケージcom.springms.cloud.controller;
com.springms.cloud.entity.User をインポートします。
org.springframework.beans.factory.annotation.Autowired をインポートします。
org.springframework.beans.factory.annotation.Value をインポートします。
org.springframework.web.bind.annotation.GetMapping をインポートします。
org.springframework.web.bind.annotation.PathVariable をインポートします。
org.springframework.web.bind.annotation.RestController をインポートします。
org.springframework.web.client.RestTemplate をインポートします。
/**
* 電影微服务Controller。
*
* @author hmilyylimh
*
* @version 0.0.1
*
* @date 2017/9/17
*
*/
@RestController
public class MsSimpleConsumerMovieController {
@Autowired
プライベート RestTemplaterestTemplate;
@Value("${user.userServicePath}")
private String userServicePath;
@GetMapping("/movie/{id}")
public User findById(@PathVariable Long id) {
return this.restTemplate.getForObject(this.userServicePath + id, User.class);
}
}
2.5 シンプルなムービー マイクロサービス起動クラスを追加します (springms-simple-consumer-movie\src\main\java\com\springms\cloud\MsSimpleConsumerMovieApplication.java)
パッケージ com.springms.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/ **
* シンプルなムービー マイクロサービス (コンシューマー、プロバイダーはユーザー マイクロサービス)。
*
* @author hmilyylimh
*
* @version 0.0.1
*
* @date 2017/9/17
*
*/
@SpringBootApplication
public class MsSimpleConsumerMovieApplication {
@Bean
public RestTemplaterestTemplate() {
return new RestTemplate();
}
public static void main(文字列[] 引数) {
SpringApplication.run(MsSimpleConsumerMovieApplication.class, args);
System.out.println("[[[[[[Simple Movie Microservice]]]]]] が開始されました。");
}
}
3. テスト
/************************************************ *******************************************
1. 簡単なムービーマイクロサービス (コンシューマー、プロバイダーはユーザー マイクロサービス):
1. springms-simple-provider-user モジュール サービスを開始し、1 つのポートを開始します;
2. springms-simple-consumer-movie モジュール サービスを開始し、1 つのポートを開始します;
3.ブラウザでアドレス http://localhost:8005/movie/1 を入力すると、情報が正常に出力されたことがわかります;
********************* ****** ******************************************* ********* *************/