package demo.helloconsumer;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
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.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class HelloConsumerController {

    @Autowired
    private HelloFeignService helloFeignService;

    @SentinelResource(value="enter",
            blockHandler="handleBlock",blockHandlerClass=MyBlockHandler.class,
            fallback="handleException",fallbackClass = MyExceptionHandler.class,
            exceptionsToIgnore={NullPointerException.class}
    )
    @GetMapping(value = "/enter/{username}")
    public String sayHello(@PathVariable String username) {
        if(username.equals("Monster"))
            throw new IllegalArgumentException("Illegal Argument");
        return helloFeignService.sayHello(username);
    }
    /*
    public String handleBlock(String username, BlockException ex) {
        ex.printStackTrace();
        return username+",request is blocked.";
    }

    public String handleException(String username, Throwable ex) {
        ex.printStackTrace();
        return username+",something is wrong.";
    }
    */
    @GetMapping(value = "/enter")
    public String sayHello() {
        return helloFeignService.sayHello();
    }

    @Autowired
    DiscoveryClient discoveryClient;

    @SentinelResource(value="list")
    @GetMapping(value = "/list")
    public String showServices() {
        List<String> services=discoveryClient.getServices();
        for(String service:services)
            System.out.println("服务名: "+service);

        List<ServiceInstance> instances = discoveryClient.getInstances("hello-provider-service");
        for(ServiceInstance instance:instances)
          System.out.println("URI: "+instance.getUri());

        return "ok";
    }

    @Autowired
    RestTemplate restTemplate;

    @GetMapping(value = "/enter1/{username}")
    public String sayHello1(@PathVariable String username){
        List<ServiceInstance> instances = discoveryClient.getInstances("hello-provider-service");
        //返回：http://192.168.100.106:8081
        String rootUrl=instances.get(0).getUri().toString();

        String serviceUrl=rootUrl+"/greet/"+username;

        ResponseEntity<String> responseEntity=
                restTemplate.getForEntity(serviceUrl,String.class);
        String result=responseEntity.getBody();
        return result;
    }

    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @GetMapping(value = "/enter2/{username}")
    public String sayHello2(@PathVariable String username){
        ServiceInstance instance = loadBalancerClient.choose("hello-provider-service");
        String rootUrl=instance.getUri().toString();

        String serviceUrl=rootUrl+"/greet/"+username;

        ResponseEntity<String> responseEntity=
                restTemplate.getForEntity(serviceUrl,String.class);
        String result=responseEntity.getBody();
        return result;
    }
    /*
    @GetMapping(value = "/enter3/{username}")
    public String sayHello3(@PathVariable String username){
        String rootUrl="http://hello-provider-service";  //指定微服务的虚拟地址
        String serviceUrl=rootUrl+"/greet/"+username;

        ResponseEntity<String> responseEntity=
                restTemplate.getForEntity(serviceUrl,String.class);
        String result=responseEntity.getBody();
        return result;
    }

     */

}



/*********************************************************
 * 作者：孙卫琴                                   
 * 来源：《Spring Cloud Alibaba微服务开发零基础入门到实战》源代码  
 * 技术支持网址：www.javathinker.net/alibaba                    
 *********************************************************/
