栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java第四十六天,SpringCloud框架系列(六),为微服务增加第二个模块

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java第四十六天,SpringCloud框架系列(六),为微服务增加第二个模块

一、新建项目 1.选中父工程,右键为父工程添加子模块

 

2.修改子模块的 pom.xml 配置文件,为其添加必要的库

	
	
		org.springframework.boot
		spring-boot-starter-web
	
	
	
		org.springframework.boot
		spring-boot-starter-actuator
	

	
	
		org.springframework.boot
		spring-boot-devtools
		runtime
		true
	

	
		org.projectlombok
		lombok
		true
	

	
		org.springframework.boot
		spring-boot-starter-test
		test
	


3.为子模块新增 yml 配置文件

server:
  # 80 接口是浏览器默认访问端口,这样可以让用户更加方便,无需手动输入端口号
  # 但是新版 idea 中如果直接使用 80 端口可能会出现问题,需要解决,所以这里先暂时用 8000
  port: 8000
 4.添加启动类

package com.lanyue.order;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }
}
二、编写代码

注意:

       因为客户端(用户)是服务使用方,因此它不需要,也不应该具备操作后台数据库、左右业务功能的能力,他只需要调用后台的其他服务即可(购物、支付、退款 ......);所以客户端只需要有 Controller 层即可,但是它又用到了支付模块的一些实体类和公用工具类,因此需要对应的工具包和实体类包及其代码

1.编写通用客户端返回工具类,JsonResult

package com.lanyue.order.entities;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;


// 替换 get set 方法
@Data
// 全参构造函数
@AllArgsConstructor
// 空参构造函数
@NoArgsConstructor
// 实现序列化接口
public class Payment implements Serializable {

    private Long id;
    private String serial;
}

2.编写 Payment 实体类(与第一个模块的实体类完全一样,这里是为了减少模块间的依赖,直接重新写一份)

package com.lanyue.order.entities;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;


// 替换 get set 方法
@Data
// 全参构造函数
@AllArgsConstructor
// 空参构造函数
@NoArgsConstructor
// 实现序列化接口
public class Payment implements Serializable {

    private Long id;
    private String serial;
}
3.新建一个配置文件

package com.lanyue.order.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ApplicationContextConfig {

    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}
4.编写客户端访问 controller

package com.lanyue.order.controller;

import com.lanyue.order.entities.Payment;
import com.lanyue.order.util.JsonResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;

@RestController
public class OrderController {

    // 被调用模块 Payment 的基础访问路径
    public static final String PAYMENT_URL = "http://localhost:8080/springcloud";

    // 一个 controller 调用另一个服务的 controller 本质上是使用 HttpClient/HttpURLConnection 进行接口的调用
    // 但是后面出现了一些对这基本工具的功能封装,例如 RestTemplate,Feign

    @Resource
    private RestTemplate restTemplate;

    // 调用 Payment 模块的 controller 接口
    @PostMapping("/consumer/payment/insert")
    public JsonResult insert(@RequestBody Payment payment){
        return restTemplate.postForObject(PAYMENT_URL + "/payment/insert", payment, JsonResult.class);
    }

    // 调用 Payment 模块的 controller 接口
    @GetMapping("/consumer/payment/selectPaymentById")
    public JsonResult selectPaymentById(@RequestParam("id")  String id){
        return restTemplate.getForObject(PAYMENT_URL + "/payment/selectPaymentById" + "?id=" + id, JsonResult.class);
    }
}
5.测试

 

三、友情小提示

因为 springcloud 项目会包含很多 springboot 项目,这时候我们开启一个项目需要手动选中所有的 springboot 项目,为了方便我们会使用到 RunDashboard 工具统一启动/关闭所有的 springboot项目,如下图所示:

如果 IDEA 中没有显示这个组件的话,我们选中最外层的 springboot(parent 项目),然后右键选择 show in explorer 或 open in explorer;然后进入项目中的 .idea 隐藏文件夹,修改 workspace.xml 配置文件,添加如下组件;最后选中 view ==> tool windows ==> services 或 RunDashboard 


  

 

 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1020544.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号