天天看點

SpringCloud學習筆記(九、配置用戶端)

目的

配置用戶端,使其能夠得到使得其可以從配置伺服器上擷取版本資訊。

改造視圖微服務-feign

直接将前面的一個視圖微服務進行改造。

pom.xml

添加spring-cloud-starter-config 用于通路配置伺服器

<?xml version="1.0" encoding="UTF-8"?>

<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">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>edu.hpu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>product-view-service-feign</artifactId>


    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>  <!--對feign方式的支援-->
        </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>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
    </dependencies>


</project>
      
bootstrap.yml

與 Spring Cloud Config (配置)相關的屬性必須配置在 bootstrap.yml 中,config 部分内容才能被正确加載。因為 config 的相關配置會先application.yml,而 bootstrap.yml 的加載也是先于 application.yml。

spring:
  cloud:
    config:
      label: master
      profile: dev
      discovery:
        enabled:  true
        serviceId:  config-server
  client:
    serviceUrl:
      defaultZone:  http://localhost:8761/eureka/      

application.yml

變化不大,把注冊中心交給了bootstrap.yml,

spring:
  application:
    name:  product-view-service-feign
  thymeleaf:
    cache: false
    prefix: classpath:/templates/
    suffix: .html
    encoding: UTF-8
    content-type: text/html
    mode: HTML5
  zipkin:
    base-url: http://localhost:9411      

ProductController

用@Value擷取配置中version的值,并放入Model,

package edu.hpu.springcloud.controller;

import edu.hpu.springcloud.pojo.Product;
import edu.hpu.springcloud.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

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

product.html

product.html添加對version的顯示,

<!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;
            width: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>
        <tr>
            <td align="center" colspan="3">
                <p th:text="${version}" >LaughterYoung springcloud version unknown</p>
            </td>
        </tr>
    </table>
</div>

</body>

</html>      

跑一下,通路

依次運作注冊中心、配置服務、資料服務、視圖服務,通路:

http://localhost:8012/products
SpringCloud學習筆記(九、配置用戶端)

錯誤

1、version注入失敗

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'version' in value "${version}"      

1.1、先去看看bootstrap.yml中配置serviceId和注冊的伺服器名字是否一緻,這個沒有問題。

1.2、檢查pom.xml包中是否添加了config jar包,沒有問題。

1.3、修改gitee中項目配置檔案名字,原名product-view-service-dev.properties 改為product-view-service-feign.properties

SpringCloud學習筆記(九、配置用戶端)

OK,解決了,懵逼來,懵逼去。

參考:

【1】、

http://how2j.cn/k/springcloud/springcloud-config-client/2048.html

【2】、

https://windmt.com/2018/04/19/spring-cloud-7-config-sample/