SpringCloud (Chapter 002) Simple Movie Microservice Class (Consumer, and Provider is User Microservice)
1. General introduction
Microservices communicate with each other through the Http protocol;
user microservices act as providers, movie microservices act as consumers, and movie microservices consume user microservices;
2. Implementation steps
2.1 Add maven reference package
<?xml version="1.0" encoding="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>
<dependencies>
<!-- web模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
2.2 Add application configuration file (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 Add entity user class User (springms-simple-consumer-movie\src\main\java\com\springms\cloud\entity\User.java)
package com.springms.cloud.entity;
import java.math.BigDecimal;
public class User {
private Long id;
private String username;
private String name;
private Short age;
private BigDecimal balance;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Short getAge() {
return this.age;
}
public void setAge(Short age) {
this.age = age;
}
public BigDecimal getBalance() {
return this.balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
2.4 Add Movie Web Access Layer Controller (springms-simple-consumer-movie\src\main\java\com\springms\cloud\controller\MsSimpleConsumerMovieController.java)
package com.springms.cloud.controller;
import com.springms.cloud.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* 电影微服务Controller。
*
* @author hmilyylimh
*
* @version 0.0.1
*
* @date 2017/9/17
*
*/
@RestController
public class MsSimpleConsumerMovieController {
@Autowired
private RestTemplate restTemplate;
@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 Add a simple movie microservice startup class (springms-simple-consumer-movie\src\main\java\com\springms\cloud\MsSimpleConsumerMovieApplication.java)
package 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;
/ **
* Simple movie microservice (consumer, and provider is user microservice).
*
* @author hmilyylimh
*
* @version 0.0.1
*
* @date 2017/9/17
*
*/
@SpringBootApplication
public class MsSimpleConsumerMovieApplication {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main( String[] args) {
SpringApplication.run(MsSimpleConsumerMovieApplication.class, args);
System.out.println("[[[[[[Simple Movie Microservice]]]]]] started.");
}
}
3. Test
/**************************************************** *****************************************
1. Simple movie micro-services (consumers, And the provider is user microservice):
1. Start the springms-simple-provider-user module service and start 1 port;
2. Start the springms-simple-consumer-movie module service and start 1 port;
3. In the browser Enter the address http://localhost:8005/movie/1 and you can see that the information is successfully printed out;
*************************** ***************************************************** *************/