没有实战过微服务架构的人,不能说真懂微服务。因为除了技术的实现,这其中还涉及了难以量化的业务拆分和组件化思想。这门课就带大家以电商工程为例,基于Spring Cloud / SpringCloud Alibaba ,
融合常用组件/中间件,进行微服务架构设计和开发,获取真正的企业级微服务解决方案。
链接:https://pan.baidu.com/s/1HpPt-NmcH7uk7SqDxlERrQ
提取码:xtdd
链接:https://pan.baidu.com/s/1Ypi4RAch5EXivNc5TvNfpQ
提取码:9gl1
失效+\/❤:x923713
package com.imooc.ecommerce.controller;
import com.imooc.ecommerce.config.ProjectConfig;
import com.imooc.ecommerce.service.NacosClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* <h1>nacos client controller</h1>
* */
("/nacos-client")
public class NacosClientController {
private final NacosClientService nacosClientService;
private final ProjectConfig projectConfig;
public NacosClientController(NacosClientService nacosClientService,
ProjectConfig projectConfig) {
this.nacosClientService = nacosClientService;
this.projectConfig = projectConfig;
}
/**
* <h2>根据 service id 获取服务所有的实例信息</h2>
* */
("/service-instance")
public List<ServiceInstance> logNacosClientInfo(
(defaultValue = "e-commerce-nacos-client") String serviceId) {
log.info("coming in log nacos client info: [{}]", serviceId);
return nacosClientService.getNacosClientInfo(serviceId);
}
/**
* <h2>动态获取 Nacos 中的配置信息</h2>
* */
("/project-config")
public ProjectConfig getProjectConfig() {
return projectConfig;
}
}
package com.imooc.ecommerce.service;
import com.alibaba.fastjson.JSON;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;
public class NacosClientService {
private final DiscoveryClient discoveryClient;
public NacosClientService(DiscoveryClient discoveryClient) {
this.discoveryClient = discoveryClient;
}
/**
* <h2>打印 Nacos Client 信息到日志中</h2>
* */
public List<ServiceInstance> getNacosClientInfo(String serviceId) {
// 测试 UseHystrixCommandAnnotation 的超时
// try {
// Thread.sleep(2000);
// } catch (InterruptedException ex) {
// //
// }
// 测试 NacosClientHystrixCommand 熔断
// throw new RuntimeException("has some error");
log.info("request nacos client to get service instance info: [{}]", serviceId);
return discoveryClient.getInstances(serviceId);
}
/**
* <h2>提供给编程方式的 Hystrix 请求合并</h2>
* */
public List<List<ServiceInstance>> getNacosClientInfos(List<String> serviceIds) {
log.info("request nacos client to get service instance infos: [{}]",
JSON.toJSONString(serviceIds));
List<List<ServiceInstance>> result = new ArrayList<>(serviceIds.size());
serviceIds.forEach(s -> result.add(discoveryClient.getInstances(s)));
return result;
}
// 使用注解实现 Hystrix 请求合并
(
batchMethod = "findNacosClientInfos",
scope = com.netflix.hystrix.HystrixCollapser.Scope.GLOBAL,
collapserProperties = {
(name = "timerDelayInMilliseconds", value = "300")
}
)
public Future<List<ServiceInstance>> findNacosClientInfo(String serviceId) {
// 系统运行正常, 不会走这个方法
throw new RuntimeException("This method body should not be executed!");
}
public List<List<ServiceInstance>> findNacosClientInfos(List<String> serviceIds) {
log.info("coming in find nacos client infos: [{}]", JSON.toJSONString(serviceIds));
return getNacosClientInfos(serviceIds);
}
}
package com.imooc.ecommerce;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* <h1>Nacos Client 工程启动入口</h1>
* */
// 启动 Hystrix
// 刷新配置
public class NacosClientApplication {
public static void main(String[] args) {
SpringApplication.run(NacosClientApplication.class, args);
}
}
链接:https://pan.baidu.com/s/1Ypi4RAch5EXivNc5TvNfpQ
提取码:9gl1
失效+\/❤:x923713