SpringCloud(第 005 篇)電影微服務,註冊到 EurekaServer 中,通過 Http 協議訪問使用者微服務

HMILYYLIMH發表於2017-09-18

SpringCloud(第 005 篇)電影微服務,也註冊到 EurekaServer 中,通過 Http 協議訪問已註冊到生態圈中的使用者微服務

一、大致介紹

1、在 eureka 服務治理框架中,微服務與微服務之間通過 Http 協議進行通訊;
2、使用者微服務作為消費方、電影微服務作為提供方都註冊到 EurekaServer 中;
3、在電影微服務Web層通過 application.yml 的硬編碼配置方式實現服務之間的通訊;

二、實現步驟

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>

        <!-- 監控和管理生產環境的模組 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- 客戶端發現模組 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
    </dependencies>

</project>

2.2 新增應用配置檔案(springms-consumer-moviesrcmainresourcesapplication.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 新增實體使用者類User(springms-consumer-moviesrcmainjavacomspringmscloudentityUser.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訪問層Controller(springms-consumer-moviesrcmainjavacomspringmscloudcontrollerMovieController.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 新增電影微服務啟動類(springms-consumer-moviesrcmainjavacomspringmscloudMsConsumerMovieApplication.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;

/**
 * 電影微服務,也註冊到 EurekaServer 中,通過 Http 協議訪問已註冊到生態圈中的使用者微服務。
 *
 * @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("【【【【【【 電影微服務 】】】】】】已啟動.");
    }
}

三、測試

/****************************************************************************************
 一、電影微服務,也註冊到 EurekaServer 中,通過 Http 協議訪問已註冊到生態圈中的使用者微服務:

 1、啟動 springms-discovery-eureka 模組服務,啟動1個埠;
 2、啟動 springms-provider-user 模組服務,啟動1個埠;
 3、啟動 springms-consumer-movie 模組服務,啟動1個埠;
 4、在瀏覽器輸入地址 http://localhost:7900/simple/1 可以看到資訊成功的被列印出來,說明使用者微服務正常;
 5、在瀏覽器輸入地址 http://localhost:8761 並輸入使用者名稱密碼 admin/admin 進入Eureka微服務顯示在網頁中,驗證使用者微服務、電影微服務確實註冊到了 eureka 服務中;

 6、在瀏覽器輸入地址http://localhost:7901/movie/1 可以看到使用者資訊成功的被列印出來;
 ****************************************************************************************/

四、下載地址

https://gitee.com/ylimhhmily/SpringCloudTutorial.git

SpringCloudTutorial交流QQ群: 235322432

SpringCloudTutorial交流微信群: 微信溝通群二維碼圖片連結

歡迎關注,您的肯定是對我最大的支援!!!

相關文章