一、环境
开发工具:IntelliJ Idea
JDK 1.8
Spring boot 2.3.12.RELEASE
spring cloud Alibaba 2.2.7.RELEASE
openfeign 2.2.9.RELEASE
二、程序目录
可以通过开发工具中的maven、spring initializr等进行项目创建。内容包括:父工程、两个子工程。结构如下图:

①父工程,该工程仅是pom工程,向子工程提供pom的继承。
②子工程,用于两个服务之间的调用
- 工程说明:
- order服务通过restTemplate和openfeign两种方式分别调用stock服务中的reduct接口
三、配置文件
<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.0modelVersion>
<groupId>com.jwsswgroupId>
<artifactId>microserviceartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>microservicename>
<description>microservicedescription>
<packaging>pompackaging>
<modules>
<module>ordermodule>
<module>stockmodule>
modules>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.3.12.RELEASEversion>
<relativePath/>
parent>
<properties>
<java.version>8java.version>
<spring.cloud.version>Hoxton.SR12spring.cloud.version>
<alibaba.cloud.version>2.2.7.RELEASEalibaba.cloud.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring.cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>${alibaba.cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
exclude>
excludes>
configuration>
plugin>
plugins>
build>
project>
- 3.2 子工程POM,除artifactId外两个子工程配置一致
<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>microserviceartifactId>
<groupId>com.jwsswgroupId>
<version>0.0.1-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>orderartifactId>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
dependencies>
project>
- 3.3 application.yml配置文件,除application.name外两个子工程配置一致
server:
port: 8011
spring:
application:
name: order-server
cloud:
nacos:
discovery:
server-addr: 192.168.0.182:8848
feign:
hystrix:
enabled: true
四、程序代码
4.1 Order子工程的相关代码
- 4.1.1 启动类OrderApplication
package com.jwssw.order;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableFeignClients
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
@LoadBalanced
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
}
package com.jwssw.order;
import com.jwssw.order.service.OrderService;
import org.springframework.beans.factory.annotation.Qualifier;
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 {
private final RestTemplate restTemplate;
private final OrderService orderService;
public OrderController(RestTemplate restTemplate,
@Qualifier("com.jwssw.order.service.OrderService") OrderService orderService) {
this.restTemplate = restTemplate;
this.orderService = orderService;
}
@RequestMapping("/add")
public String add() {
String str = restTemplate.getForObject("http://stock-server/stock/reduct", String.class);
return "Hello World " + str;
}
@RequestMapping("/feignAdd")
public String feignAdd() {
String str = orderService.reduct();
return "feign调用:" + str;
}
}
- 4.1.3 openfeign接口类OrderService
package com.jwssw.order.service;
import com.jwssw.order.service.impl.OrderServiceImpl;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name = "stock-server", fallback = OrderServiceImpl.class)
public interface OrderService {
@GetMapping("/stock/reduct")
String reduct();
}
- 4.1.4 回调类OrderServiceImpl
package com.jwssw.order.service.impl;
import com.jwssw.order.service.OrderService;
import org.springframework.stereotype.Component;
@Component
public class OrderServiceImpl implements OrderService {
@Override
public String reduct() {
return "库存服务不可达";
}
}
4.2 stock子工程的相关代码
- 4.2.1 启动类StockApplication
package com.jwssw.stock;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class StockApplication {
public static void main(String[] args) {
SpringApplication.run(StockApplication.class, args);
}
}
- 4.2.2 对外提供服务类StockController
package com.jwssw.stock;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/stock")
public class StockController {
@Value("${server.port}")
private String port;
@RequestMapping("/reduct")
public String reduct() {
System.out.println("扣减库存成功");
return "扣减库存" + port;
}
}
五、总结
以上内容主要给予spring cloud alibaba的最小可运行的微服务Demo工程,如果你在实际项目中尚未微服务,不妨可以采用本文上述方式在实际项目中推行一下。