cloud:Hoxton.SR1 boot:2.2.2.RELEASE cloud alibaba:2.1.0RELEASE
Java:java8 Maven:3.5及以上 MySQL:5.7及以上
架构理论:
分布式架构一般包含:
服务注册与发现 服务调用 服务熔断 负载均衡 服务降级
服务消息队列 配置中心管理 服务网关 服务监控
全链路追踪 自动化构建部署 服务定时任务调度
SpringCloud:
分布式微服务架构的一站式解决方案,是多种微服务架构落地技术的集合体,俗称微服务全家桶
一般使用的技术栈:
服务注册与发现:EUREKA Zookeeper Consul Nacos
服务负载与调用:Ribbon LoadBalancer
服务负载与调用:Feign OpenFeign
服务熔断降级:Hystrix resilience4j sentinel
服务网关:Zuul Zuul2 gateway
服务分布式配置:Config Nacos
服务总线:Bus Nacos
RestTemplate:
RestTemplate提供了多种便捷访问远程Http服务的方法,
是一种简单便捷的访问restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具集
向容器注入RestTemplate
- package com.chen.springcloud.config;
-
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.client.RestTemplate;
-
- /**
- * Created by 莫荒 on 2022/9/17 20:09
- */
- @Configuration
- public class ApplicationContextConfig {
- @Bean
- public RestTemplate getRestTemplate(){
- return new RestTemplate();
- }
- }
controller调用即可
- package com.chen.springcloud.controller;
-
- import com.chen.springcloud.entities.CommonResult;
- import com.chen.springcloud.entities.Payment;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.client.RestTemplate;
-
- import javax.annotation.Resource;
-
- /**
- * Created by 莫荒 on 2022/9/17 20:23
- */
- @RestController
- @Slf4j
- public class OrderController {
-
- public static final String PAYMENT_URL="http://localhost:8001";
-
- @Resource
- private RestTemplate restTemplate;
-
- @GetMapping("/consumer/payment/create")
- public CommonResult
create(Payment payment){ - return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
- }
- }
工程中有重复的部分,为减少冗余,新建一个module,用于放置重复部分

clean后install,打包便于给其他服务引用

需要引用的服务在pom中引入
