1. 程式人生 > >使用SpringCloud搭建微服務---------建立微服務的消費者

使用SpringCloud搭建微服務---------建立微服務的消費者

上一節(使用SpringCloud搭建微服務<一>),我們建立了服務的提供者,也就是使用者微服務,使用者微服務提供了一個介面,根據ID獲取使用者的餘額和其他資訊。今天我們來建立微服務的消費者---電影微服務。

一、使用Spring Initializr 來快速建立Spring Boot專案

             Spring Initializr 有一下幾種使用方法:

                 通過網頁使用(http://start.spring.io/)

                 通過Intellig idea 使用。

                 通過Spring Boot CLI使用

我們選擇第二種方式建立,我使用的是IDEA的版本是IDEA.2017.2,

選擇file-->new Project

選擇 Spring Initializr 選擇自己需要的SDK,URL選擇預設的,點選下一步

填寫專案的GroupId和ArtifactId


選擇專案需要對應的jar,我們先引入SpringMVC的相關jar

點選NEXT、finish就完成了建立

二、編寫服務消費者

pom檔案中的內容如下
<?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>

	<groupId>com.mountain.springboot</groupId>
	<artifactId>microservice-consumer-movie-metadata</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>microservice-simple-consumer-movie</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.8.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

	</dependencies>
	<!--引入spring cloud的依賴-->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId> org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Camden.SR4</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>
建立使用者實體類,該類是一個pojo
@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column
    private String name;

    @Column
    private String username;

    @Column
    private Integer age;

    @Column
    private BigDecimal balance;
	此處省略set/get
}
建立啟動類的程式碼
@SpringBootApplication
public class MicroserviceSimpleConsumerMovieApplication {
	@Bean
	public RestTemplate restTemplate(){
		return new RestTemplate();
	}

	public static void main(String[] args) {
		SpringApplication.run(MicroserviceSimpleConsumerMovieApplication.class, args);
	}
}

建立Controller,在其中使用RestTemplate請求使用者微服務的API
@RestController
public class MoVieController {
    @Autowired
    private RestTemplate restTemplate;
    
    @Value("${user.userServiceUrl}")
    private String userServiceUrl;

    @GetMapping("/user/{id}")
    public User findById(@PathVariable Long id) {
        return this.restTemplate.getForObject(userServiceUrl + id, User.class);
    }

}

編寫配置檔案application.yml

server:
  port: 8010
user:
  userServiceUrl: http://localhost:8000/

這樣一個簡單的電影微服務就完成了

訪問http://localhost:8010/user/1  結果如下:

注意:使用者微服務必須先啟動,應為我們是電影微服務呼叫使用者的微服務獲取使用者資訊

{
    "id": 1,
    "name": "張三",
    "username": "account1",
    "age": 20,
    "balance": 100
}