springcloud简单搭建-视图微服务-RIBBON

我们先启动一个8001的数据。
创建子项目 product-view-service-ribbon
pom.xml,这边有thymeleaf需要学习下,不过不影响


  4.0.0
  
    cn.how2j.springcloud
    springcloud
    0.0.1-SNAPSHOT
  
  product-view-service-ribbon
   
    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.boot
            spring-boot-starter-web
        
 
      
        org.springframework.boot
        spring-boot-starter-thymeleaf
      
         
     
   

不说了上代码大家都懂的什么层

package cn.how2j.springcloud.pojo;
 
public class Product {
 
    private int id;
    private String name;
    private int price;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public Product() {
         
    }
    public Product(int id, String name, int price) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
    }
 
}

建个client包

package cn.how2j.springcloud.client;
 
import java.util.List;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
 
import cn.how2j.springcloud.pojo.Product;
 
@Component
public class ProductClientRibbon {
 
    @Autowired
    RestTemplate restTemplate;
 
    public List listProdcuts() {
        return restTemplate.getForObject("http://PRODUCT-DATA-SERVICE/products",List.class);
    }
 
}
package cn.how2j.springcloud.service;
 
import java.util.List;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import cn.how2j.springcloud.client.ProductClientRibbon;
import cn.how2j.springcloud.pojo.Product;
 
@Service
public class ProductService {
    @Autowired ProductClientRibbon productClientRibbon;
    public List listProducts(){
        return productClientRibbon.listProdcuts();
 
    }
}
package cn.how2j.springcloud.web;
import java.util.List;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
 
import cn.how2j.springcloud.pojo.Product;
import cn.how2j.springcloud.service.ProductService;
  
@Controller
public class ProductController {
  
    @Autowired ProductService productService;
     
    @RequestMapping("/products")
    public Object products(Model m) {
        List ps = productService.listProducts();
        m.addAttribute("ps", ps);
        return "products";
    }
}

创建一个
springcloud简单搭建-视图微服务-RIBBON_第1张图片




    products
    
           


 
id 产品名称 价格

启动项

package cn.how2j.springcloud;
 
import java.util.Scanner;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
 
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
 
import cn.hutool.core.convert.Convert;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.NetUtil;
import cn.hutool.core.util.NumberUtil;
 
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class ProductViewServiceRibbonApplication {
 
    public static void main(String[] args) {
        int port = 0;
        int defaultPort = 8010;
        Future future = ThreadUtil.execAsync(() ->{
                int p = 0;
                System.out.println("请于5秒钟内输入端口号, 推荐  8010  超过5秒将默认使用 " + defaultPort);
                Scanner scanner = new Scanner(System.in);
                while(true) {
                    String strPort = scanner.nextLine();
                    if(!NumberUtil.isInteger(strPort)) {
                        System.err.println("只能是数字");
                        continue;
                    }
                    else {
                        p = Convert.toInt(strPort);
                        scanner.close();
                        break;
                    }
                }
                return p;
        });
        try{
            port=future.get(5,TimeUnit.SECONDS);
        }
        catch (InterruptedException | ExecutionException | TimeoutException e){
            port = defaultPort;
        }
        if(!NetUtil.isUsableLocalPort(port)) {
            System.err.printf("端口%d被占用了,无法启动%n", port );
            System.exit(1);
        }
        new SpringApplicationBuilder(ProductViewServiceRibbonApplication.class).properties("server.port=" + port).run(args);
 
    }
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
     
}

application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: product-view-service-ribbon
  thymeleaf:
    cache: false
    prefix: classpath:/templates/
    suffix: .html
    encoding: UTF-8
    content-type: text/html
    mode: HTML5   

http://127.0.0.1:8010/products
springcloud简单搭建-视图微服务-RIBBON_第2张图片
启动eureka再启动8001端口(如果你能启动2个端口,那就都启动2个),再启动本次的你就可以看到
在这里插入图片描述
然后刷新你的8010页面,就可以看到8001或者8002相互切换的页面了。

你可能感兴趣的:(springcloud简单搭建-视图微服务-RIBBON)