SpringCloud(第005篇)電影微服務(wù),也注冊(cè)到EurekaServer中,通過(guò)Http協(xié)議訪問(wèn)已注冊(cè)到生態(tài)圈中的用戶微服務(wù)
一、大致介紹
1、在 eureka 服務(wù)治理框架中,微服務(wù)與微服務(wù)之間通過(guò) Http 協(xié)議進(jìn)行通信;
2、用戶微服務(wù)作為消費(fèi)方、電影微服務(wù)作為提供方都注冊(cè)到 EurekaServer 中;
3、在電影微服務(wù)Web層通過(guò) application.yml 的硬編碼配置方式實(shí)現(xiàn)服務(wù)之間的通信;
二、實(shí)現(xiàn)步驟
2.1 添加 maven 引用包
<?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-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>
<!-- 監(jiān)控和管理生產(chǎn)環(huán)境的模塊 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 客戶端發(fā)現(xiàn)模塊 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
</project>
2.2 添加應(yīng)用配置文件(springms-consumer-movie\src\main\resources\application.yml)
spring:
application:
name: springms-consumer-movie
server:
port: 7901
user:
userServicePath: http://localhost:7900/simple/
eureka:
client:
# healthcheck:
# enabled: true
serviceUrl:
defaultZone: http://admin:admin@localhost:8761/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
2.3 添加實(shí)體用戶類User(springms-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 添加電影Web訪問(wèn)層Controller(springms-consumer-movie\src\main\java\com\springms\cloud\controller\MovieController.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;
@RestController
public class MovieController {
@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 添加電影微服務(wù)啟動(dòng)類(springms-consumer-movie\src\main\java\com\springms\cloud\MsConsumerMovieApplication.java)
package com.springms.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* 電影微服務(wù),也注冊(cè)到 EurekaServer 中,通過(guò) Http 協(xié)議訪問(wèn)已注冊(cè)到生態(tài)圈中的用戶微服務(wù)。
*
* @author hmilyylimh
*
* @version 0.0.1
*
* @date 2017/9/17
*
*/
@SpringBootApplication
@EnableEurekaClient
public class MsConsumerMovieApplication {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(MsConsumerMovieApplication.class, args);
System.out.println("【【【【【【 電影微服務(wù) 】】】】】】已啟動(dòng).");
}
}
三、測(cè)試
/****************************************************************************************
一、電影微服務(wù),也注冊(cè)到 EurekaServer 中,通過(guò) Http 協(xié)議訪問(wèn)已注冊(cè)到生態(tài)圈中的用戶微服務(wù):
1、啟動(dòng) springms-discovery-eureka 模塊服務(wù),啟動(dòng)1個(gè)端口;
2、啟動(dòng) springms-provider-user 模塊服務(wù),啟動(dòng)1個(gè)端口;
3、啟動(dòng) springms-consumer-movie 模塊服務(wù),啟動(dòng)1個(gè)端口;
4、在瀏覽器輸入地址 http://localhost:7900/simple/1 可以看到信息成功的被打印出來(lái),說(shuō)明用戶微服務(wù)正常;
5、在瀏覽器輸入地址 http://localhost:8761 并輸入用戶名密碼 admin/admin 進(jìn)入Eureka微服務(wù)顯示在網(wǎng)頁(yè)中,驗(yàn)證用戶微服務(wù)、電影微服務(wù)確實(shí)注冊(cè)到了 eureka 服務(wù)中;
6、在瀏覽器輸入地址http://localhost:7901/movie/1 可以看到用戶信息成功的被打印出來(lái);
****************************************************************************************/
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。