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提供的数据了。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值