springCloud Eureca消费者Consumer的项目

消费者的项目:
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。

index.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
index1
<a href="/hello.do">test jump 跳转</a><br>
<a href="/hello1.do">test DB数据库</a><br>
<a href="/getObj.do">test 获取数据库对象</a><br>
<hr>
马克-to-win@马克java社区此表测试post

<form  action="insertObj" method="post">
       姓名:<input type="text" name="markname" placeholder="用户名" /><br />
    <button type="submit" >提交</button>
</form>

<hr>
马克-to-win@马克java社区此表测试put(The other HTTP methods (i.e. other than GET and POST) are not available in HTML 4.1 or XHTML 1.0.

也就是说实际上HTML5以前,FORM都仅支持GET和POST。所以用post模拟put)

<form  action="updateObj" method="post">
       姓名:<input type="text" name="markname" placeholder="用户名" /><br />
    <button type="submit" >提交</button>
</form>

<hr>
马克-to-win@马克java社区此表测试delete

<form  action="deleteObj" method="post">
    <button type="submit" >提交</button>
</form>

上传
<form method="POST" action="/upload" enctype="multipart/form-data">
    <input type="file" name="file" /><br/><br/>
    <input type="submit" value="Submit" />
</form>


</body>
</html>


result2.html:


<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>Title</title>
</head>

<body>
here RResult2 <span th:text="${session.user}"> 您好</span>
</body>
</html>




package com;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplication {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate () {
        return new RestTemplate ();
    }
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

@RestController
class ConsumerCotroller {
    @Autowired
    private RestTemplate template;
   
    @RequestMapping("/consumer")
    public void consume(HttpServletResponse res) throws IOException {
        ResponseEntity<String> responseEntity = template.getForEntity("http://provider/acquire", String.class);
        String body = responseEntity.getBody();
        System.out.println("body is "+body);
        res.sendRedirect("index.html");
    }

    @RequestMapping("/hello")
    void myhome(HttpServletResponse res) throws IOException {
        System.out.println("spring  boot springMvc 马克-to-win!");
/*跳回到static目录下的html时可以用sendRedirect,但动态Thymeleaf跳转必须用下面的ModelAndView, String都不行。*/      
        res.sendRedirect("index.html");
    }
  
  




    @RequestMapping(value="/hello1", method=RequestMethod.GET)
    public ModelAndView login(HttpSession session){
        System.out.println("spring  boot springMvc 马克-to-win!");
        ResponseEntity<String> responseEntity = template.getForEntity("http://provider/acquire", String.class);
        String body = responseEntity.getBody();
        System.out.println("body is "+body);

        session.setAttribute("user",body);
/*跳转如果用 return "result2";是不可以的, 因为这是RestController*/      
        ModelAndView mv = new ModelAndView("result2");
        return mv;
    }
 
  
  
  
    @RequestMapping(value="/getObj", method=RequestMethod.GET)
    public ModelAndView getObj(HttpSession session){
        System.out.println("spring  boot springMvc 马克-to-win!");
        ResponseEntity<Register> responseEntity = template.getForEntity("http://provider/getObj", Register.class);
        Register register = responseEntity.getBody();
        System.out.println("body is "+register);

        session.setAttribute("user",register.toString());
/*马克-to-win@马克java社区:跳转如果用 return "result2";是不可以的, 因为这是RestController*/      
        ModelAndView mv = new ModelAndView("result2");
        return mv;
    }
  
    @RequestMapping(value="/insertObj", method=RequestMethod.POST)
    public ModelAndView insertObj(@RequestParam String markname,HttpSession session){
        System.out.println("spring  boot springMvc 马克-to-win!");
        Register register=new Register();
        register.setName(markname);
        register.setId(300);
        register.setAge(23);
/*下面的方法是一个聪明的方法,能更得到String作为是否插成功的标志*/
        String response=template.postForObject("http://provider/insertObj", register, String.class);
        System.out.println("response is "+response);

        session.setAttribute("user",response);
/*跳转如果用 return "result2";是不可以的, 因为这是RestController*/      
        ModelAndView mv = new ModelAndView("result2");
        return mv;
    }
  
    @RequestMapping(value="/updateObj", method=RequestMethod.POST)
    public ModelAndView updateObj(@RequestParam String markname,HttpSession session){
        System.out.println("spring  boot springMvc 马克-to-win!");
        Register register=new Register();
        register.setName(markname);
        register.setId(300);
        register.setAge(23);
/*不能像post一样得到反馈,得通过其他像异常的方法得到反馈,此例中不研究*/
        template.put("http://provider/updateObj", register);
/*跳转如果用 return "result2";是不可以的, 因为这是RestController*/      
        ModelAndView mv = new ModelAndView("result2");
        return mv;
    }
  
  
    @RequestMapping(value="/deleteObj", method=RequestMethod.POST)
    public ModelAndView deleteObj(HttpSession session){
        System.out.println("spring  boot springMvc 马克-to-win!");
/*不能像post一样得到反馈,得通过其他像异常的方法得到反馈,很复杂,望后续版本改进,此例中不研究*/
/*下面的第一个参数赋值为300,到provider那/deleteObj/{id},所以id=300*/      
        template.delete("http://provider/deleteObj/{1}",300);
/*跳转如果用 return "result2";是不可以的, 因为这是RestController*/      
        ModelAndView mv = new ModelAndView("result2");
        return mv;
    }
  

  




    private static String UPLOADED_FOLDER = "e://temp//";

    @RequestMapping("/upload")
    public void singleFileUpload(@RequestParam("file") MultipartFile file,HttpServletResponse res) throws IOException {
        try {
            byte[] bytes = file.getBytes();
            Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
            Files.write(path, bytes);
            System.out.println("马克-to-win@马克java社区 successfully");
        } catch (IOException e) {
            e.printStackTrace();
        }
        res.sendRedirect("index.html");
    }
  


    @Autowired
    private DiscoveryClient discoveryClient;
/*http://192.168.0.101:9000/services/provider测的时候,这么测*/
    @RequestMapping("/services/{appName}")
    public String myGetIns(@PathVariable String appName) {
/*发现客户的工具discoveryClient,可以通过getInstances,找到分布的一堆叫这个名字的service*/  
//        ServiceInstance si=this.discoveryClient.getLocalServiceInstance();
//        return si.getHost();
         List<ServiceInstance> list = this.discoveryClient.getInstances(appName);
           if (list != null && list.size() > 0 )
           { return list.get(0).getHost()+list.get(0).getMetadata(); }
           return null;
    }

}