代码地址
https://gitee.com/ai_lanlan/spring-cloud/tree/af5d8c8ad3d102e26c9b697e034fffae4905c026
1.common项目
common项目的目录如下
因为支付模块和订单模块都涉及公共的部分(如entity),因此将这些公共的部分抽取出来作为公共模块。
对于pom文件,需要注意在引入hutook依赖时一定要注明 < t y p e > p o m < / t y p e > <type>pom</type> <type>pom</type>,因为对应的jar文件在阿里云中是不存在的,只有pom文件。
2.consumer项目
2.1 项目结构
由于是面向客户端的微服务,因此该微服务相应客户端发来的请求,并请求相应的处理微服务,使用RestTemplate进行请求的发送。
2.2 config代码
package com.tao.springcloud.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
2.3 OrderController代码
package com.tao.springcloud.controller;
import com.tao.springcloud.entities.CommonResult;
import com.tao.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
@Slf4j
public class OrderController {
public static final String PAYMENT_URL = "http://localhost:8001";
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/create")
public CommonResult<Payment>create(@RequestBody Payment payment){
return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
}
@GetMapping("/consumer/payment/get/{id}")
public CommonResult<Payment>getPayment(@PathVariable("id")Long id){
return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
}
}
2.4 RestTemplate
RestTemplate 是 Spring Framework 提供的用于访问 RESTful 服务的客户端工具。它使得在Java应用程序中进行HTTP请求变得更加方便。使用 RestTemplate,你可以执行GET、POST、PUT、DELETE等HTTP请求,并处理响应数据。
以下是使用 RestTemplate 的基本示例:
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
public class RestTemplateExample {
public static void main(String[] args) {
// 创建 RestTemplate 实例
RestTemplate restTemplate = new RestTemplate();
// 发起 GET 请求
String url = "https://jsonplaceholder.typicode.com/posts/1";
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
// 处理响应
if (response.getStatusCode().is2xxSuccessful()) {
String responseBody = response.getBody();
System.out.println("Response Body: " + responseBody);
} else {
System.err.println("Request failed with status code: " + response.getStatusCodeValue());
}
}
}
在这个示例中,我们首先创建了一个 RestTemplate 实例,然后使用 getForEntity 方法发送GET请求。我们指定了请求的URL和响应的类型(在这里是String)。最后,我们处理了HTTP响应,检查状态码并打印响应体。
请注意,为了使用 RestTemplate,你需要添加相关的Spring依赖到你的项目中,并配置适当的 RestTemplate bean。通常,你可以在Spring Boot项目中直接使用 RestTemplate,而在非Spring Boot项目中需要手动配置。
2.5 @Bean和@Resource
@Resource和@Bean是两个不同的注解,它们在Java中的使用方式和用途有一些区别:
用途:
@Resource用于注入外部资源,通常在Java EE或Jakarta EE应用程序中使用,用于将托管资源(如数据源、JMS连接等)注入到Java类中。
@Bean用于定义和配置Spring框架中的Bean,通常在Spring应用程序中使用,用于声明bean的创建方式和属性。
环境:
@Resource通常与Java EE或Jakarta EE相关,并且在这些环境中自动处理资源的注入。它不需要你显式创建bean。
@Bean是Spring框架的一部分,用于定义Spring容器中的bean,需要在Spring应用程序的配置类中使用。
配置:
使用@Resource时,您通常需要在容器中配置资源的名称或JNDI位置,以便注入正确的资源。
使用@Bean时,您需要在Spring的配置类中使用@Bean注解来定义bean的创建方法,并配置bean的属性。
示例:
使用@Resource来注入一个数据源:
import javax.annotation.Resource;
import javax.sql.DataSource;
public class MyService {
@Resource(name = "jdbc/myDataSource")
private DataSource dataSource;
}
使用@Bean来定义一个bean:
@Configuration
public class MyConfig {
@Bean
public MyBean myBean() {
return new MyBean();
}
}
总之,@Resource和@Bean具有不同的用途和环境,前者用于在Java EE或Jakarta EE中注入资源,而后者用于在Spring框架中定义和配置bean。您应根据您的应用程序的需要来选择适当的注解。