springCloud Eureca消費者Consumer的項目

消費者的項目:
馬克- to-win:馬克 java社區(qū):防盜版實名手機尾號: 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 跳轉(zhuǎn)</a><br>
<a href="/hello1.do">test DB數(shù)據(jù)庫</a><br>
<a href="/getObj.do">test 獲取數(shù)據(jù)庫對象</a><br>
<hr>
馬克-to-win@馬克java社區(qū)此表測試post

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

<hr>
馬克-to-win@馬克java社區(qū)此表測試put(The other HTTP methods (i.e. other than GET and POST) are not available in HTML 4.1 or XHTML 1.0.

也就是說實際上HTML5以前,F(xiàn)ORM都僅支持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社區(qū)此表測試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,但動態(tài)Thymeleaf跳轉(zhuǎn)必須用下面的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);
/*跳轉(zhuǎn)如果用 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社區(qū):跳轉(zhuǎn)如果用 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);
/*跳轉(zhuǎn)如果用 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);
/*跳轉(zhuǎn)如果用 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一樣得到反饋,得通過其他像異常的方法得到反饋,很復(fù)雜,望后續(xù)版本改進,此例中不研究*/
/*下面的第一個參數(shù)賦值為300,到provider那/deleteObj/{id},所以id=300*/      
        template.delete("http://provider/deleteObj/{1}",300);
/*跳轉(zhuǎn)如果用 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社區(qū) 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) {
/*發(fā)現(xiàn)客戶的工具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;
    }

}