1,背景介绍
在现在功能繁多的业务架构中,传统的 maven 项目已经无法支持项目的流畅需求,所以现在的项目已经开始开始走向分布式架构,在这主要搭建一个空的 maven 分布式架构,可以运用到实际项目中进行扩展,源码会在git上同步。
maven 分布式工程的基本架构,如下:
parent-project
---- spring-application1
---- spring-application2
---- spring-common
spring-common工程截图没截上,明白有这个工程就行
2,父工程搭建
因使用的是idea编辑器,在这就直接开始了
打开 IDEA, File -> New -> New Project,然后选择 Maven,如下:
Next,然后给 maven 分布式项目起个名儿,如 parent-project。
创建成功,然后删除多余的文件包,留一个pom.xml即可。
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">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<groupId>com.yuntian</groupId>
<artifactId>parent-project</artifactId>
<version>1.0-SNAPSHOT</version>
<name>parent-project</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modules>
<module>spring-application1</module>
<module>spring-application2</module>
<!--<module>spring-eureka-server</module>-->
<module>spring-common</module>
<!--<module>spring-shiro</module>
<module>spring-rabbitmq</module>-->
</modules>
<properties>
<java.version>1.8</java.version>
<shiro.version>1.4.0</shiro.version>
<shiro.redis>2.8.24</shiro.redis>
<fastjson.version>1.2.39</fastjson.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2,子工程搭建
parent-project右键new —>module搭建三个子工程分别为:
spring-common是存放公共类、pojo的地方
spring-application1和spring-application2是业务服务
spring-common中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>parent-project</artifactId>
<groupId>com.yuntian</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-common</artifactId>
</project>
spring-application1和spring-application2中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>parent-project</artifactId>
<groupId>com.yuntian</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-application1</artifactId>
<dependencies>
<!--公共类 -->
<dependency>
<groupId>com.yuntian</groupId>
<artifactId>spring-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
spring-application1中application.properties:
server.port=8081
#对外暴漏的服务名称
spring.application.name=app1
RunApplication:
@SpringBootApplication
public class RunApplication {
public static void main(String[] args) {
SpringApplication.run(RunApplication.class,args);
}
}
TestController:
@Controller
@RequestMapping("/test")
public class TestController {
@Resource
private ITestService testService;
@RequestMapping("/getOrder/{id}")
@ResponseBody
public Order getOrder(@PathVariable Integer id){
Order order = testService.getOrderById(id);
return order;
}
}
service自己随便写就行。
启动测试:localhost:8081/test/getOrder/1访问返回自己设置的值,就OK
spring-application2中application.properties:
server.port=8080
#对外暴漏的服务名称
spring.application.name=app2
#订单服务地址
ORDER_URL=http://localhost:8081
RunApplication1:
@SpringBootApplication
public class RunApplication1 {
public static void main(String[] args) {
SpringApplication.run(RunApplication1.class,args);
}
}
在微服务都是以 HTTP 接口的形式暴露自身服务的,因此在调用远程服务时就必须使用 HTTP 客户端。Spring Boot 中使用的是 RestTemplate,首先,我们写一个配置类,将 RestTemplate 作为一个 Bean 交给 Spring 来管理。
RestTmplateConfig:
@Configuration
public class RestTmplateConfig {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
OrderConsumerController:
@Controller
@RequestMapping("/order/consumer")
public class OrderConsumerController {
@Value("${ORDER_URL}")
private String ORDER_URL;
@Resource
private RestTemplate restTemplate;
/***
*@Description 和别的服务通信
*@Param [id]
*@Return com.yuntian.pojo.Order
*@Author lvjie
*@Date 2019/3/7/0007 15:36
*/
@RequestMapping("/getOrder/{id}")
@ResponseBody
public Order getOrder(@PathVariable Integer id){
return restTemplate.getForObject(ORDER_URL+"/test/getOrder/1",Order.class);
}
}
启动spring-application1和spring-application2
访问:localhost:8080/order/consumer/getOrder/1正常返回自己所需要的值,OK
RestTemplate 的使用,在 Controller 中,我们定义了一个订单服务提供者的 url 前缀,这是 spring-application1 的服务地址,因为我们等会要远程调用这个服务。restTemplate.getForObject 方法是 GET 请求方法,它有两个参数:
url:请求地址
ResponseBean.class:HTTP 相应被转换成的对象类型
对于实体类或者 List 均可以接收,同样地,还有处理 POST 请求的方法 restTemplate.postForObject
,该方法有三个参数,如下:
url:请求地址
requestMap:请求参数,封装到map中
ResponseBean.class:HTTP响应被转换成的对象类型
总体上maven搭建的一个简单分布式架构已经完成了
源码地址:https://github.com/yuntian0215/spring_cloud
里面我已经集成了别的功能,要是运行的话,一定要先看代码说明的一个txt文档