1.项目包括三个部分,一个是web端,负责呈现页面;一个是erp,负责提供数据;还有一个部分是服务发现,使用的是spring-eureka实现。
2.流程是web需要数据时就通过服务发现找到erp服务,拿到erp返回的数据。用于通信的数据使用json格式。
pom.xml文件如下:
<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>shopartifactId>
<groupId>com.breezegroupId>
<version>1.0-SNAPSHOTversion>
<relativePath>../pom.xmlrelativePath>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>eureka-serviceartifactId>
<packaging>jarpackaging>
<name>eureka-servicename>
<description>eureka-servicedescription>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starterartifactId>
<version>1.1.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eurekaartifactId>
<version>1.1.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eureka-serverartifactId>
<version>1.2.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<version>1.3.5.RELEASEversion>
<optional>trueoptional>
dependency>
<dependency>
<groupId>net.sourceforge.nekohtmlgroupId>
<artifactId>nekohtmlartifactId>
<version>1.9.15version>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Brixton.SR5version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<version>1.3.2.RELEASEversion>
<executions>
<execution>
<goals>
<goal>repackagegoal>
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.cloudgroupId>
<artifactId>spring-cloud-starterartifactId>
<version>1.1.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-feignartifactId>
<version>1.2.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eureka-serverartifactId>
<version>1.2.2.RELEASEversion>
dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Brixton.SR5version>
<type>pomtype>
<scope>importscope>
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都在服务发现上出现了。
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提供的数据了。