package com.itmuch.cloud.microservicesimpleprovideruser.entity; import javax.persistence.*; @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column private String username; @Column private String name; @Column private int age; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public float getBalance() { return balance; } public void setBalance(float balance) { this.balance = balance; } @Column private float balance; public Long getId() { return id; } public void setId(Long id) { this.id = id; } }
package com.itmuch.cloud.microservicesimpleprovideruser.repository; import com.itmuch.cloud.microservicesimpleprovideruser.entity.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface UserRepository extends JpaRepository<User ,Long> { }
package com.itmuch.cloud.microservicesimpleprovideruser.Controller; import com.itmuch.cloud.microservicesimpleprovideruser.entity.User; import com.itmuch.cloud.microservicesimpleprovideruser.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @Autowired private UserRepository userRepository; @GetMapping("/simple/{id}") public User findById(@PathVariable Long id){ return userRepository.findOne(id); } }
第二步咱們搭建了一個服務提供者,如今咱們來搭建一個服務消費者,咱們依然從http://start.spring.io網站生成咱們的微服務框架,咱們能夠注意到,在Dependencies這一欄只選擇了一個Web,選好並輸入完後,點擊"Generate Project"按鈕。html
點擊上圖的"Generate Project"按鈕後會彈出下載對話框,以下圖所示,另存爲保存到本地磁盤。java
下面咱們把該消費者導入到Intellij IDEA工具中,咱們點擊File---->New------>Module from Existing Sources...,以下圖所示git
在彈出的對話框中咱們選擇咱們剛纔加壓好的movie微服務的pom.xml文件,而後點擊"OK",以下圖所示。web
下面兩步只需點擊"Next",而後點擊"Finish"按鈕便可把工程加到Intellij IDEA工具中來,以下圖所示spring
package com.itmuch.cloud.microservicesimpleprovidermovie.Controller; import com.itmuch.cloud.microservicesimpleprovidermovie.entity.User; import org.springframework.beans.factory.annotation.Autowired; 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; @RestController public class movieController { @Autowired private RestTemplate restTemplate; @GetMapping("/movie/{id}") public User findById(@PathVariable Long id) { return this.restTemplate.getForObject("http://localhost:7900/simple/" + id, User.class); } }
package com.itmuch.cloud.microservicesimpleprovidermovie.entity; public class User { private Long id; private String username; private String name; private int age; private float balance; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public float getBalance() { return balance; } public void setBalance(float balance) { this.balance = balance; } }
server: port: 7901
package com.itmuch.cloud.microservicesimpleprovidermovie; 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 MicroserviceSimpleProviderMovieApplication { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(MicroserviceSimpleProviderMovieApplication.class, args); } }
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
info: app: name: "@project.name@" description: "@project.description@" version: "@project.version@" spring-boot-version: "@project.parent.version@"
再次訪問 http://localhost:8080/info 結果:
{"app":{"name":"microservice-simple-provider-user","description":"zjmdemo","version":"0.0.1-SNAPSHOT","spring-boot-version":"1.5.9.RELEASE"}} 訪問:http://127.0.0.1:8080/autoconfig
項目GIT庫:https://gitee.com/cyj930307/springcloud_textbook.gitsql