1. 程式人生 > >SpringBoot搭建微服務(二)實現MVC

SpringBoot搭建微服務(二)實現MVC

1.實現流程簡介

1.專案包括三個部分,一個是web端,負責呈現頁面;一個是erp,負責提供資料;還有一個部分是服務發現,使用的是spring-eureka實現。
2.流程是web需要資料時就通過服務發現找到erp服務,拿到erp返回的資料。用於通訊的資料使用json格式。

2.專案框架圖

這裡寫圖片描述

3.eureka伺服器搭建

pom.xml檔案如下:

<?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">
<parent> <artifactId>shop</artifactId> <groupId>com.breeze</groupId> <version>1.0-SNAPSHOT</version> <!--這裡依賴的父專案的pom.xml的內容就是一個基本的伺服器需要的內容,在上一篇中可檢視-->
<relativePath>../pom.xml</relativePath> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>eureka-service</artifactId> <packaging>jar</packaging> <name>eureka-service</name> <description>eureka-service</description
>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <!-- Spring Cloud starter --> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> <version>1.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.1.0.RELEASE</version> </dependency> <dependency> <!-- Eureka service registration --> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <version>1.3.5.RELEASE</version> <optional>true</optional> </dependency> <dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> <version>1.9.15</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.3.2.RELEASE</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>

java檔案內容如下:

package com.breeze.shop;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * Created by Breeze on 2016/12/14.
 */
 //主要就是這一句讓這個伺服器作為服務發現的服務端
@EnableEurekaServer
@SpringBootApplication
public class EurekaServiceApplication {
    public static void main(String[] args)
    {
        SpringApplication.run(EurekaServiceApplication.class,args);
    }
}

另外需要配置一下這個服務發現的服務端的地址:
在resources的資料夾下新建application.yml檔案中配置即可,內容如下:

server:
  port: 8761   # HTTP (Tomcat) port

eureka:
  instance:
    hostname: localhost
  client:  # Not a client, don't register with yourself
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
          defaultZone: http://localhost:8761/eureka/

至此,服務發現的服務端搭建完成,接下來是客戶端的配置:

在web module和erp module的Application類main函式上新增如下註解:
@EnableDiscoveryClient
其次在pom.xml中新增依賴
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
            <version>1.1.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
            <version>1.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <version>1.2.2.RELEASE</version>
        </dependency>
        <!--在dependencys外新增-->
        <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

新增完依賴還需要在application.xml檔案中配置客戶端的名稱和服務端的地址等內容,web module的內容如下:

spring:
  application:
    name: spring-web

eureka:
  instance:
    leaseRenewalIntervalInSeconds: 20
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka

erp模組做同樣的操作後便可實現服務發現的功能了,訪問http://localhost:8761/可看到如下頁面
這裡寫圖片描述
可看到兩個module都在服務發現上出現了。

4.實現web訪問erp

1.使用@FeignClient(“spring-erp”)註解介面類,使得介面類可以作為客戶端去訪問erp的Controller,erp的Controller就是普通的Controller,後續加上資料庫就成為真正的資料提供端。這裡的FeignClient括號中的名稱是否很熟悉呢?其實這個就是在服務發現中註冊的名稱,也就是在配置檔案application.yml中為這個服務起的名稱。

package com.breeze.shop.api;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Created by Breeze on 2016/12/14.
 */
@FeignClient("spring-erp")
public interface GoodsService {
//這裡的url是作為客戶端使用的,向/erp/goods這個地址發起get請求
//這個地址其實就是erp端的某個controller對應的地址
    @RequestMapping(value = "/erp/goods", method = RequestMethod.GET)
    public String getGoodsName(@RequestParam(value = "goodsId") Long goodsId);
}

另外,使用了FeignClient這個註解的介面類不需要實現類,使用Autowired就可以填充了。
重複一下最開始提到的流程,頁面發起請求到Controller後,Controller去請求erp,erp提供資料,資料利用thymeleaf模板技術填充到頁面中,返回頁面即可。

package com.breeze.shop.controller;

import com.breeze.shop.api.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by Breeze on 2016/12/13.
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @RequestMapping(value="/hello",produces ="application/xml+xhtml;UTF-8")
    public String hello(Model model)
    {
        String name = goodsService.getGoodsName(1l);
        model.addAttribute("name",name);
        //這裡的hello是一個html頁面的名稱,使用了thymeleaf技術
        return "hello";
    }
}

最後,還需要在web模組的main類中加上如下註解去是FeignClint生效,這個註解是@EnableFeignClients(“com.breeze.shop”)否則會出現無法使用Autowired填充的情況。
完成後就可以看到頁面中顯示出來的erp提供的資料了。