一、SpringCloud Alibaba之普通应用

发布时间 2023-09-09 10:33:00作者: 阿瞒123

1.1、创建一个maven项目

新建项目

操作路径:File->new->project

输入相关的配置:

Name:项目名称
Location:项目所在位置
Artifact Coordinates:项目坐标(制品坐标)

更改配置文件

在项目中指定了打包方式为pom方式,此时项目目录下就不需要src目录,只保留一个pom.xml文件就行。

创建项目子模块

操作路径:右击项目名称->new->Module

 

订单子模块

 

同样创建商品子模块(stock-service),最后的项目结构如下图:

 

父项目pom文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>
​
    <modules>
        <module>order</module>
        <module>stock</module>
    </modules>
​
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.10</version>
        <relativePath/>
    </parent>
    <groupId>com.geely</groupId>
    <artifactId>jjbx</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>jjbx</name>
​
    <description>jjbx</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</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>
​

1.2、stock-service子项目

创建一个stock模块,pom的打包方式为java

stock-service的pom文件

<?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>jjbx</artifactId>
        <groupId>com.geely</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
​
    <groupId>com.geely.jjbx</groupId>
    <artifactId>stock</artifactId>
​
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.5.3</version>
        </dependency>
    </dependencies>
</project>

Application

package com.geely.jjbx.stock;
​
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
​
@SpringBootApplication
public class Application {
​
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}
​

StockController

package com.geely.jjbx.stock;
​
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
​
@RestController
@RequestMapping("/stock")
public class StockController {
​
    @RequestMapping("/getStock")
    public String getStock(){
        return "getStock";
    }
}
​

application.properties

server.port=8081

 

1.3、order-service子项目

创建一个order模块,调用stock模块

order-service的pom文件

<?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>jjbx</artifactId>
        <groupId>com.geely</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
​
    <groupId>com.geely.jjbx</groupId>
    <artifactId>order</artifactId>
​
​
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
​
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.5.3</version>
        </dependency>
    </dependencies>
​
</project>

Application

因为要使用RestTemplate这个bean,所以要把RestTemplate注入到spring容器中,Springboot的启动类也是配置类,此处就把RestTemplate的注入放在启动类中。

package com.geely.jjbx.order;
​
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
​
@SpringBootApplication
public class OrderApplication {
​
    public static void main(String[] args) {
        SpringApplication springApplication=new SpringApplication();
        springApplication.run(Application.class,args);
    }
​
    /**
     * 注入 RestTemplate
     * @param builder
     * @return
     */
    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder){
        RestTemplate restTemplate=builder.build();
        return restTemplate;
    }
}
​

 

OrderController

package com.geely.jjbx.order;
​
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
​
@RestController
@RequestMapping("/order")
public class OrderController {
​
    @Autowired
    private RestTemplate restTemplate;
​
    /**
     * 订单调用库存的接口
     * @return
     */
    @RequestMapping("/getOrder")
    public String getOrder(){
        String result = restTemplate.getForObject("http://localhost:8081/stock/getStock", String.class);
        System.out.println(result);
        return "order";
    }
​
}
​

application.properties

server.port=8080

1.4、run_dashboard