微服務多模塊(服務提供者與服務消費者)開發圖解

技術/楊33

這篇文章用編碼來實現下面這張圖的工程結構:

微服務多模塊(服務提供者與服務消費者)開發圖解

一、創建一個存放公共代碼的子工程:cloud-common-util

1、pom.xml文件內容:

<code> 

<

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

>

cloudproject

artifactId

>

<

groupId

>

com.project.cloud

groupId

>

<

version

>

1.0-SNAPSHOT

version

>

parent

>

<

modelVersion

>

4.0.0

modelVersion

>

<

artifactId

>

cloud-common-util

artifactId

>

<

dependencies

>

<

dependency

>

<

groupId

>

org.projectlombok

groupId

>

<

artifactId

>

lombok

artifactId

>

dependency

>

<

dependency

>

<

groupId

>

org.springframework.boot

groupId

>

<

artifactId

>

spring-boot-devtools

artifactId

>

<

scope

>

runtime

scope

>

<

optional

>

true

optional

>

dependency

>

<

dependency

>

<

groupId

>

cn.hutool

groupId

>

<

artifactId

>

hutool-all

artifactId

>

<

version

>

5.2.3

version

>

dependency

>

dependencies

>

project

>

/<code>

2、把多個子工程公用的代碼提出來,放到公共項目中

微服務多模塊(服務提供者與服務消費者)開發圖解

3、先maven clean,再maven install,將公共項目添加到maven倉庫中。

微服務多模塊(服務提供者與服務消費者)開發圖解

二、創建服務消費者訂單模塊:cloud-consumer-order80

1、pom.xml文件內容:

<code> 

<

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

>

cloudproject

artifactId

>

<

groupId

>

com.project.cloud

groupId

>

<

version

>

1.0-SNAPSHOT

version

>

parent

>

<

modelVersion

>

4.0.0

modelVersion

>

<

artifactId

>

cloud-consumer-order80

artifactId

>

<

dependencies

>

<

dependency

>

<

groupId

>

org.springframework.boot

groupId

>

<

artifactId

>

spring-boot-starter-web

artifactId

>

dependency

>

<

dependency

>

<

groupId

>

org.springframework.boot

groupId

>

<

artifactId

>

spring-boot-starter-actuator

artifactId

>

dependency

>

<

dependency

>

<

groupId

>

org.projectlombok

groupId

>

<

artifactId

>

lombok

artifactId

>

dependency

>

<

dependency

>

<

artifactId

>

cloud-common-util

artifactId

>

<

groupId

>

com.project.cloud

groupId

>

<

version

>

1.0-SNAPSHOT

version

>

dependency

>

dependencies

>

project

>

/<code>

2、修改application.yml配置文件

<code>

server:

port:

80

/<code>

3、編寫主啟動類

<code>

package

com.cloud;

import

org.springframework.boot.SpringApplication;

import

org.springframework.boot.autoconfigure.SpringBootApplication;

public

class

OrderMain80

{

public

static

void

main

(String[] args)

{ SpringApplication.run(OrderMain80

.

class

,

args

)

; } }/<code>

4、創建一個配置類

<code>

package

com

.cloud

.config

;

import

org

.springframework

.context

.annotation

.Bean

;

import

org

.springframework

.context

.annotation

.Configuration

;

import

org

.springframework

.web

.client

.RestTemplate

;

@Configuration

public class ContextConfig {

@Bean

public RestTemplate getRestTemplate() {

return

new

RestTemplate

(); } }/<code>

5、編寫controller類

<code>

package

com.cloud.controller;

import

com.cloud.entities.CommonResult;

import

com.cloud.entities.Payment;

import

org.springframework.web.bind.

annotation

.GetMapping;

import

org.springframework.web.bind.

annotation

.PathVariable;

import

org.springframework.web.bind.

annotation

.PostMapping;

import

org.springframework.web.bind.

annotation

.RestController;

import

org.springframework.web.client.RestTemplate;

import

javax.

annotation

.Resource;

public

class

OrderController

{

private

static

final

String URL =

"http://localhost:8001"

;

private

RestTemplate restTemplate;

public

CommonResult create(Payment payment) {

return

restTemplate.postForObject(URL +

"/payment/create"

, payment, CommonResult

.

class

);

}

public

CommonResult create(

Long

id) {

return

restTemplate.getForObject(URL +

"/payment/getPayment/"

+ id, CommonResult

.

class

);

} }/<code>

三、分別啟動服務提供者支付模塊、服務消費者訂單模塊,通過接口交互數據

後臺代碼使用RestTemplateAPI中的postForObject()和getForObject()方法實現接口遠程調用。

如圖:服務消費者訂單模塊請求服務提供者支付模塊,成功。

微服務多模塊(服務提供者與服務消費者)開發圖解

作者:楊33,北京互聯網公司在職Java開發,專注分享寫作乾貨。歡迎關注我,期待你的點贊評論。


分享到:


相關文章: