• springcloud(4)-视图微服务feign


    1.添加微服务

    image-20201113114646477

    2.pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <parent>
        <groupId>com.zhangdemo</groupId>
        <artifactId>springcloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
      </parent>
      <artifactId>product-view-service-feign</artifactId>
    
      <dependencies>
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    
        <!--支持feign-->
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    
      </dependencies>
    
    </project>
    

    3.ProductClientFeign

    public interface ProductClientFeign {
    
        @GetMapping("/products")
        public List<Product> listProdcuts();
    }
    

    4.Product

    package com.zhangdemo.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;
        }
    
    }
    

    5.ProductService

    @Service
    public class ProductService {
        @Autowired
        ProductClientFeign productClientFeign;
        public List<Product> listProducts(){
            return productClientFeign.listProdcuts();
    
        }
    }
    

    6.ProductController

    @Controller
    public class ProductController {
    
        @Autowired
        ProductService productService;
    
        @RequestMapping("/products")
        public Object products(Model m) {
            List<Product> ps = productService.listProducts();
            m.addAttribute("ps", ps);
            return "products";
        }
    }
    

    7.ProductViewServiceFeignApplication

    @SpringBootApplication
    @EnableEurekaClient
    @EnableDiscoveryClient
    @EnableFeignClients             //feign支持
    public class ProductViewServiceFeignApplication {
    
        public static void main(String[] args) {
            int port = 0;
            int defaultPort = 8012;
            Future<Integer> future = ThreadUtil.execAsync(() ->{
                int p = 0;
                System.out.println("请于5秒钟内输入端口号, 推荐  8012 、 8013  或者  8014,超过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(ProductViewServiceFeignApplication.class).properties("server.port=" + port).run(args);
    
        }
    }
    

    8.products.html

    <!DOCTYPE HTML>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>products</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <style>
    
            table {
                border-collapse:collapse;
                400px;
                margin:20px auto;
            }
            td,th{
                border:1px solid gray;
            }
    
        </style>
    </head>
    <body>
    
    <div class="workingArea">
        <table>
            <thead>
            <tr>
                <th>id</th>
                <th>产品名称</th>
                <th>价格</th>
            </tr>
            </thead>
            <tbody>
            <tr th:each="p: ${ps}">
                <td th:text="${p.id}"></td>
                <td th:text="${p.name}"></td>
                <td th:text="${p.price}"></td>
            </tr>
            </tbody>
        </table>
    </div>
    
    </body>
    
    </html>
    

    9.application.yml

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

    10.运行

    image-20201113134553957

  • 相关阅读:
    示波器测量电源的纹波
    hdoj 2717 Catch That Cow
    hdoj 1548 A strange lift
    hdoj 4586 Play the Dice
    zoj 2095 Divisor Summation
    hdoj 4704 Sum
    router-link传参
    字体自适应
    横向滚动div
    vue路由
  • 原文地址:https://www.cnblogs.com/NaoDaiYouDianDa/p/13985339.html
Copyright © 2020-2023  润新知