package com.atguigu.springcloud.controller;
import cn.hutool.core.swing.clipboard.ClipboardUtil;
import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentService;
import com.atguigu.springcloud.tool.RandomCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@Value("${server.port}")
private String serverPort;
@Resource
private DiscoveryClient discoveryClient;
@PostMapping("/payment/create")
public CommonResult create(@RequestBody Payment payment){
log.info("******传过来的payment是:【"+payment+"】");
int result= paymentService.create(payment);
log.info("******插入结果:【"+result+"】");
if(result>0){
return new CommonResult(200,"插入成功,serverPort是:"+serverPort,result);
}else {
return new CommonResult(444,"插入失败",result);
}
}
@GetMapping("/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") Long id){
Payment payment= paymentService.getPaymentById(id);
log.info("******插入结果:"+payment);
if(payment!=null){
return new CommonResult(200,"查询成功,serverPort是:"+serverPort,payment);
}else {
return new CommonResult(444,"查询失败,查询id:"+id,null);
}
}
@GetMapping("/payment/discovery")
public Object discovery(){
//看当前注册中心的服务有哪些
List<String> services = discoveryClient.getServices();
for (String service:services) {
log.info("当前服务名字是为:"+service);
//看当前服务名下的有哪些服务
List<ServiceInstance> instances = discoveryClient.getInstances(service);
for (ServiceInstance instance:instances) {
//service服务名称、instance.getServiceId()id、instance.getHost()主机名、instance.getPort()端口号、instance.getUri()地址
log.info("当前服务【"+service+"】下的服务id:"+instance.getServiceId()+"主机名:"+instance.getHost()+"端口号:"+instance.getPort()+"地址:"+instance.getUri());
}
}
return this.discoveryClient;
}
@GetMapping("/payment/lb")
public String getPaymentLB(){
return serverPort;
}
@GetMapping("/payment/feign/timeout")
public String paymentFeignTimeout(){
try {
TimeUnit.SECONDS.sleep(3);
}catch (InterruptedException e){
e.printStackTrace();
}
return serverPort;
}
//生成随机码
@GetMapping("/payment/getcode/{length}")
public String getCode(@PathVariable("length") int length){
System.out.println("length的值是:"+length);
return RandomCode.CreateCode(length);
}
}
评论0