接上一篇文章,Spring Cloud是专门用来开发微服务的框架,它结合了SpringBoot的开发特性,通俗来讲,使用该框架开发微服务就是:1.使用SpringBoot开发一个个的服务 2.在各服务内或者他们之间添加springcloud的相关组件。这样结合起来就可以实现一个微服务的开发。
Spring Cloud本身集成了服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等组件,目的就是为了让使用者可以快速的搭建一个微服务模版,降低微服务开发的复杂性。
所以,使用Spring Cloud首要的第一步就要搞清楚它和SpringBoot版本依赖关系,不然项目搭建就会出问题,他两的版本依赖关系如下:
介绍:该项目包含两个微服务,一个是支付微服务,一个是订单微服务,订单微服务调用支付微服务完成订单支付功能。
1.创建一个maven项目,里面只包含一个支付实体类,作为本项目的公共实体类和工具类pom文件如下:
4.0.0 com.king cloud_common 1.0-SNAPSHOT org.projectlombok lombok 1.18.22
支付实体类:
package com.king.demo.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Payment {
//订单编号
private Integer id;
//支付状态
private String message;
}
2.使用springboot创建支付服务项目
pom文件:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.4.9 com.king.demo cloud_payment_01 0.0.1-SNAPSHOT cloud_payment_01 Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test com.king cloud_common 1.0-SNAPSHOT org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok
可以看到,这里我们在pom文件中引入了之前创建的公共实体类的依赖。
作为示例支付服务中只写controller:
package com.king.demo.controller;
import com.king.demo.entity.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/payment")
public class PaymentController {
//使用@value注解,将配置文件中的某配置赋值给该类的一个属性
@Value("${server.port}")
private String serverPort;
@RequestMapping("/{id}")
public ResponseEntity payment(@PathVariable Integer id){
Payment payment=new Payment();
payment.setId(id);
payment.setMessage("支付成功,服务端口:"+serverPort);
return ResponseEntity.ok(payment);
}
}
配置文件:
server:
port: ${port:9001}
3.使用springboot创建订单服务项目
pom文件:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.4.9 com.king.demo cloud_order_01 0.0.1-SNAPSHOT cloud_order_01 Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test com.king cloud_common 1.0-SNAPSHOT org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok
订单服务也只写controller:
package com.king.demo.controller;
import com.king.demo.client.PaymentClient;
import com.king.demo.entity.Payment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/payment/{id}")
public ResponseEntity getPayment(@PathVariable Integer id){
String url="http://localhost:9001/payment/"+id;
Payment payment = restTemplate.getForObject(url, Payment.class);
return ResponseEntity.ok(payment);
}
启动器:
package com.king.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
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
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
在order项目中,我们需要调用支付服务,这是跨项目调用,Spring提供了一个RestTemplate模板工具类,对基于Http的客户端进行了封装,并且实现了对象与json 的序列化和反序列化。他的使用需要我们在启动器中创建一个RestTemplate 的对象,并将其注册到容器中,然后就可以在controller方法中注入,并使用他的方法了。
配置文件:
server: port: 90024.测试
启动两个服务项目,然后在浏览器访问:http://localhost:9002/order/payment/123
结果:



