Spring Cloud 純乾貨,從入門到實戰

陳彥斌發表於2020-11-10

導讀

  之前寫過一篇SpringCloud從入門到精通的點我直達,微服務基礎知識點我直達,今天我們使用Spring Cloud模擬一個電商專案。分別有以下2個服務,商品、訂單。下面我們開始叭

技術棧

  1. SpringBoot整合SpringCloud
  2. 通訊方式:http restful
  3. 註冊中心:eruka
  4. 斷路器:hystrix
  5. 閘道器:zuul

商品服務

功能點

  • 商品列表
  • 商品詳情

訂單服務

功能點

  • 我的訂單
  • 下單介面

搭建Eureka Server

建立專案

專案結構

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.18.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ybchen</groupId>
    <artifactId>eureka_server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka_server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR6</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

啟動類上加註解

application.properties

# 服務埠號
server.port=8761
# eureka主機名
eureka.instance.hostname=localhost
# 指定當前主機是否需要向註冊中心註冊(不用,因為當前主機是Server,不是Client)
eureka.client.register-with-eureka=false
# 指定當前主機是否需要獲取註冊資訊(不用,因為當前主機是Server,不是Client)
eureka.client.fetch-registry=false
# 註冊中心地址
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

啟動服務並檢視監控臺

搭建Eureka Client商品服務

建立專案

專案結構

pom.xml

Spring Cloud 純乾貨,從入門到實戰
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.18.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ybchen</groupId>
    <artifactId>product_service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>product_service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR6</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
View Code

application.properties

# 服務埠號
server.port=8771
# 服務名稱
spring.application.name=product_service
# 將服務註冊到註冊中心,eureka_service的地址
eureka.client.service-url.defaultZone:http://localhost:8761/eureka/

ProductController.java

package com.ybchen.product_service.controller;

import com.ybchen.product_service.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName:ProductController
 * @Description:商品
 * @Author:chenyb
 * @Date:2020/11/1 8:42 下午
 * @Versiion:1.0
 */
@RestController
@RequestMapping("/api/v1/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 商品列表
     *
     * @return
     */
    @PostMapping("list")
    public Object list() {
        return productService.listProduct();
    }

    /**
     * 根據id查詢商品
     *
     * @param id
     * @return
     */
    @GetMapping("findById")
    public Object findById(@RequestParam("id") int id) {
        return productService.findById(id);
    }
}

Product.java

Spring Cloud 純乾貨,從入門到實戰
package com.ybchen.product_service.domain;

import java.io.Serializable;

/**
 * @ClassName:Product
 * @Description:商品實體類
 * @Author:chenyb
 * @Date:2020/11/1 8:43 下午
 * @Versiion:1.0
 */
public class Product implements Serializable {
    /**
     * 內碼
     */
    private String id;
    /**
     * 商品名稱
     */
    private String name;
    /**
     * 價格,分為單位
     */
    private int price;
    /**
     * 庫存
     */
    private int store;

    public String getId() {
        return id;
    }

    public void setId(String 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 int getStore() {
        return store;
    }

    public void setStore(int store) {
        this.store = store;
    }

    public Product() {

    }

    public Product(String id, String name, int price, int store) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.store = store;
    }

    @Override
    public String toString() {
        return "product{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", store=" + store +
                '}';
    }
}
View Code

ProductService.java

package com.ybchen.product_service.service;

import com.ybchen.product_service.domain.Product;

import java.util.List;

/**
 * @ClassName:ProductService
 * @Description:商品service
 * @Author:chenyb
 * @Date:2020/11/1 8:45 下午
 * @Versiion:1.0
 */
public interface ProductService {
    /**
     * 商品列表
     * @return
     */
    List<Product> listProduct();

    /**
     * 根據id查詢商品
     * @param id
     * @return
     */
    Product findById(int id);
}

ProductServiceImpl.java

package com.ybchen.product_service.service.impl;

import com.ybchen.product_service.domain.Product;
import com.ybchen.product_service.service.ProductService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName:ProductServiceImpl
 * @Description:ProductService實現類
 * @Author:chenyb
 * @Date:2020/11/1 8:47 下午
 * @Versiion:1.0
 */
@Service
public class ProductServiceImpl implements ProductService {
    //初始化記憶體商品資料。模擬資料庫中儲存的商品
    private static final Map<Integer, Product> daoMap = new HashMap<>();

    @Value("${server.port}")
    private String port;
    static {
        for (int i = 0; i < 5; i++) {
            daoMap.put(i, new Product(i + "", "iphone_" + i, 1000 * i, 10));
        }
    }

    @Override
    public List<Product> listProduct() {
        Collection<Product> values = daoMap.values();
        return new ArrayList<>(values);
    }

    @Override
    public Product findById(int id) {
        Product product = daoMap.get(id);
        product.setName(product.getName()+"_"+port);
        return product;
    }
}

ProductServiceApplication.java

package com.ybchen.product_service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProductServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProductServiceApplication.class, args);
    }

}

啟動並檢視監控臺

  啟動2個服務,並檢視監控臺

搭建Eureka Client訂單服務

建立專案

專案結構

 

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.18.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ybchen</groupId>
    <artifactId>order_service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>order_service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR6</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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-netflix-ribbon</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.properties

# 服務埠號
server.port=8781
# 服務名稱
spring.application.name=order-service
# 將服務註冊到註冊中心,eureka_service的地址
eureka.client.service-url.defaultZone:http://localhost:8761/eureka/

OrderServiceApplication.java

  啟動類新增Ribbon註解

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
package com.ybchen.order_service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class OrderServiceApplication {
    /**
     * 負載均衡Ribbon
     * @return
     */
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(OrderServiceApplication.class, args);
    }

}

OrderController.java

package com.ybchen.order_service.controller;

import com.ybchen.order_service.service.ProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/v1/order")
public class OrderController {
    @Autowired
    private ProductOrderService productOrderService;
    @RequestMapping("save")
    public Object save(@RequestParam("user_id")int userId,@RequestParam("product_id")int productId){
        return productOrderService.save(userId,productId);
    }
}

ProductOrder.java

Spring Cloud 純乾貨,從入門到實戰
package com.ybchen.order_service.domain;

import java.util.Date;

/**
 * 商品訂單實體類
 */
public class ProductOrder {
    /**
     * 主鍵
     */
    private int id;
    /**
     * 商品名稱
     */
    private String productName;
    /**
     * 訂單流水號
     */
    private String tradeNo;
    /**
     * 價格,以分位單位
     */
    private int price;
    /**
     * 建立時間
     */
    private Date createTime;
    /**
     * 使用者id
     */
    private String userId;
    /**
     * 使用者名稱稱
     */
    private String userName;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getTradeNo() {
        return tradeNo;
    }

    public void setTradeNo(String tradeNo) {
        this.tradeNo = tradeNo;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Override
    public String toString() {
        return "ProductOrder{" +
                "id=" + id +
                ", productName='" + productName + '\'' +
                ", tradeNo='" + tradeNo + '\'' +
                ", price=" + price +
                ", createTime=" + createTime +
                ", userId='" + userId + '\'' +
                ", userName='" + userName + '\'' +
                '}';
    }
}
View Code

ProductOrderService.java

package com.ybchen.order_service.service;

import com.ybchen.order_service.domain.ProductOrder;

public interface ProductOrderService {
    ProductOrder save(int userId, int productId);
}

ProductOrderServiceImpl.java

package com.ybchen.order_service.service.impl;

import com.ybchen.order_service.domain.ProductOrder;
import com.ybchen.order_service.service.ProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName:ProductOrderServiceImpl
 * @Description:產品訂單實現類
 * @Author:chenyb
 * @Date:2020/11/2 11:34 下午
 * @Versiion:1.0
 */
@Service
public class ProductOrderServiceImpl implements ProductOrderService {
    @Autowired
    private RestTemplate restTemplate;
    /**
     * 下單介面
     * @param userId 使用者id
     * @param productId 產品id
     * @return
     */
    @Override
    public ProductOrder save(int userId, int productId) {
        Object obj=productId;
        //get方式
        Object forObject = restTemplate.getForObject("http://product-service/api/v1/product/findById?id=" + productId, Object.class);
        //post方式
//        Map<String,String> map=new HashMap<>();
//        map.put("id","1");
//        String s = restTemplate.postForObject("http://product-service/api/v1/product/test", map, String.class);
//        System.out.println(s);
        System.out.println(forObject);
        //獲取商品詳情
        ProductOrder productOrder=new ProductOrder();
        productOrder.setTradeNo(UUID.randomUUID().toString());
        productOrder.setCreateTime(new Date());
        productOrder.setUserId(userId+"");
        return productOrder;
    }
}

測試負載均衡

feign實戰

簡介

  改造訂單服務,呼叫商品服務獲取商品資訊

官網例子

點我直達

改造訂單服務

新增feign依賴

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.18.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ybchen</groupId>
    <artifactId>order_service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>order_service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR6</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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-netflix-ribbon</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--openfeign依賴-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

啟動類上新增註解

  啟動類上新增:@EnableFeignClients

 

新增一個介面

 

ProductClient.java

package com.ybchen.order_service.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 商品服務客戶端
 */
// name=商品服務的服務名==========》spring.application.name=product-service
@FeignClient(name = "product-service")
@RequestMapping("/api/v1/product")
public interface ProductClient {

    @GetMapping("findById")
    String findById(@RequestParam("id") int id);
}

修改ProductOrderServiceImpl.java

原先

package com.ybchen.order_service.service.impl;

import com.ybchen.order_service.domain.ProductOrder;
import com.ybchen.order_service.service.ProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName:ProductOrderServiceImpl
 * @Description:產品訂單實現類
 * @Author:chenyb
 * @Date:2020/11/2 11:34 下午
 * @Versiion:1.0
 */
@Service
public class ProductOrderServiceImpl implements ProductOrderService {
    @Autowired
    private RestTemplate restTemplate;
    /**
     * 下單介面
     * @param userId 使用者id
     * @param productId 產品id
     * @return
     */
    @Override
    public ProductOrder save(int userId, int productId) {
        Object obj=productId;
        //get方式
        Object forObject = restTemplate.getForObject("http://product-service/api/v1/product/findById?id=" + productId, Object.class);
        //post方式
//        Map<String,String> map=new HashMap<>();
//        map.put("id","1");
//        String s = restTemplate.postForObject("http://product-service/api/v1/product/test", map, String.class);
//        System.out.println(s);
        System.out.println(forObject);
        //獲取商品詳情
        ProductOrder productOrder=new ProductOrder();
        productOrder.setTradeNo(UUID.randomUUID().toString());
        productOrder.setCreateTime(new Date());
        productOrder.setUserId(userId+"");
        return productOrder;
    }
}

修改為

 

package com.ybchen.order_service.service.impl;

import com.ybchen.order_service.domain.ProductOrder;
import com.ybchen.order_service.service.ProductClient;
import com.ybchen.order_service.service.ProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * @ClassName:ProductOrderServiceImpl
 * @Description:產品訂單實現類
 * @Author:chenyb
 * @Date:2020/11/2 11:34 下午
 * @Versiion:1.0
 */
@Service
public class ProductOrderServiceImpl implements ProductOrderService {
    @Autowired
    private ProductClient productClient;

    /**
     * 下單介面
     *
     * @param userId    使用者id
     * @param productId 產品id
     * @return
     */
    @Override
    public ProductOrder save(int userId, int productId) {
        //-----------呼叫商品服務開始------------
        String byId = productClient.findById(productId);
        System.out.println(byId);
        //-----------呼叫商品服務結束------------
        //獲取商品詳情
        ProductOrder productOrder = new ProductOrder();
        productOrder.setTradeNo(UUID.randomUUID().toString());
        productOrder.setCreateTime(new Date());
        productOrder.setUserId(userId + "");
        return productOrder;
    }
}

測試商品服務

補充(設定服務呼叫超時時間)

  預設連線10秒,讀取60秒,但是由於hystrix預設是1秒超時

官網案例,點我直達

application.properties

# 設定連線和讀取超時時間
feign.client.config.default.connect-timeout=5000
feign.client.config.default.read-timeout=1100

服務降級熔斷(Hystrix)

為什麼要用?

  在一個分散式系統裡,一個服務依賴多個服務,可能存在某個服務呼叫失敗,比如超時、異常等,如何能保證在一個依賴出問題的情況下,不會導致整體服務故障,可以通過Hystrix來解決。

官網例子

  點我直達

修改訂單服務

新增依賴

        <!--hystrix依賴-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
Spring Cloud 純乾貨,從入門到實戰
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.18.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ybchen</groupId>
    <artifactId>order_service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>order_service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR6</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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-netflix-ribbon</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--openfeign依賴-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--hystrix依賴-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
View Code

啟動類加註解

  @EnableCircuitBreaker

修改控制層

  新增註解,@HystrixCommand,並定義回撥方法,返回值、入參必須一致!!!!

入參、返回值,不一致會報錯

feign結合Hystrix

修改訂單服務

開啟hystrix

 

# 開啟hystrix
feign.hystrix.enabled=true

ProductClient.java

 

package com.ybchen.order_service.service;

import com.ybchen.order_service.fallback.ProductClientFallBack;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

/**
 * 商品服務客戶端
 */
// name=商品服務的服務名==========》spring.application.name=product-service
@FeignClient(name = "product-service",fallback = ProductClientFallBack.class)
//@RequestMapping("/api/v1/product")
public interface ProductClient {

    @GetMapping("/api/v1/product/findById")
    String findById(@RequestParam("id") int id);
}

ProductClientFallBack.java

 

package com.ybchen.order_service.fallback;

import com.ybchen.order_service.service.ProductClient;
import org.springframework.stereotype.Component;

/**
 * 針對商品服務,做降級處理
 */
@Component
public class ProductClientFallBack implements ProductClient {
    @Override
    public String findById(int id) {
        System.out.println("商品服務被降級了~~~~~~~");
        return null;
    }
}

驗證商品服務熔斷

  為什麼對商品服務做了熔斷,還返回這個結果呢,那是因為service實現類,內部發生了錯誤

 

熔斷降級服務報警通知(重要)

  下面寫一些虛擬碼,比如:xxx微服務掛了,然後通過簡訊、郵件的方式,通知相應的開發人員,緊急處理事故等。

修改訂單服務

修改hystrix超時時間

禁用超時時間(不推薦)

hystrix.command.default.execution.timeout.enabled=false

設定超時時間(推薦)

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=4000

原始碼位置講解

  通過這種方法,還可以設定更多的hystrix預設值

斷路器Dashboard監控儀表盤

修改訂單服務

新增依賴

        <!--hystrix儀表盤-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
Spring Cloud 純乾貨,從入門到實戰
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.18.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ybchen</groupId>
    <artifactId>order_service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>order_service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR6</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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-netflix-ribbon</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--openfeign依賴-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--hystrix依賴-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <!--hystrix儀表盤-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
pom.xml

啟動類上加註解

  新增:@EnableHystrixDashboard

 

修改配置檔案

# 暴露全部的監控資訊
management.endpoints.web.exposure.include=*

訪問儀表盤

http://127.0.0.1:8781/hystrix

http://127.0.0.1:8781/actuator/hystrix.stream

  儀表盤實際工作中用處不大(仁者見仁智者見智),純屬學習用,具體引數,請自行百度,只要把微服務熔斷/降級報警通知處理好,比啥都好?

微服務閘道器Zuul

建立專案

專案結構

 

pom.xml

Spring Cloud 純乾貨,從入門到實戰
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.11.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ybchen</groupId>
    <artifactId>api-gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>api-gateway</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
    </properties>

    <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-netflix-zuul</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
pom.xml

application.properties

server.port=8800
# 服務名稱
spring.application.name=api-gateway
# 將服務註冊到註冊中心,eureka_service的地址
eureka.client.service-url.defaultZone:http://localhost:8761/eureka/

預設訪問規則

  http://gateway:port/service-id/**

  比如,原先下單地址:127.0.0.1:8781/api/v1/order/save?user_id=1&product_id=1

  現在下單地址:127.0.0.1:8800/order-service/api/v1/order/save?user_id=1&product_id=1

自定義路由規則

  新增application.properties資訊

 

# 自定義路由規則,語法:zuul.routes.服務名=自定義路由
zuul.routes.order-service=/apigate/**
# 不讓預設的服務對外暴露介面,語法:zuul.ignored-patterns=服務名
zuul.ignored-patterns=/order-service/**
# 忽略所有服務
# zuul.ignored-patterns=*

處理http請求頭為空的問題

   預設zuul過濾3個值("Cookie", "Set-Cookie", "Authorization"),解決版本,設定為不過濾

原始碼解讀

新增屬性

# 處理http請求頭為空的問題
zuul.sensitive-headers=

自定義Zuul過濾器之登入鑑權

改造api-gateway專案

新建LoginFilter類

  新建該類,並繼承ZuulFilter,重寫裡面的方法

 

package com.ybchen.apigateway.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.PrintWriter;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;

/**
 * @ClassName:LoginFilter
 * @Description:登入過濾器
 * @Author:chenyb
 * @Date:2020/11/8 11:16 下午
 * @Versiion:1.0
 */
@Component //讓Spring掃描到
public class LoginFilter extends ZuulFilter {
    /**
     * 過濾型別,有以下型別
     * 1、pre
     * 2、route
     * 3、post
     * 4、error
     *
     * @return
     */
    @Override
    public String filterType() {
        return PRE_TYPE;
    }

    /**
     * 過濾器順序,越小越先執行
     *
     * @return
     */
    @Override
    public int filterOrder() {
        return 4;
    }

    /**
     * 過濾器是否生效
     *
     * @return
     */
    @Override
    public boolean shouldFilter() {
        //1、獲取上下文
        RequestContext currentContext = RequestContext.getCurrentContext();
        //2、獲取HttpServletRequest
        HttpServletRequest request = currentContext.getRequest();
        //3、拿到請求路徑,判斷是否進行攔截
        //System.out.println(request.getRequestURL()); //http://127.0.0.1:8800/apigate/order/api/v1/order/save
        //System.out.println(request.getRequestURI()); ///apigate/order/api/v1/order/save
        String url = request.getRequestURI();
        System.out.println("請求路徑url=========>" + url);
        if ((url == null ? "" : url.toLowerCase()).startsWith("/apigate/order")) {
            return true;
        }
        return false;
    }

    /**
     * 過濾器邏輯,業務邏輯
     *
     * @return
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException {
        System.out.println("請求被攔截啦==============");
        //JWT方式
        //1、獲取上下文
        RequestContext currentContext = RequestContext.getCurrentContext();
        //2、獲取HttpServletRequest
        HttpServletRequest request = currentContext.getRequest();
        //3、拿到token
        String token = request.getHeader("token"); //請求頭拿token
        if (token == null || "".equals(token)) {
            token = request.getParameter("token"); //get方式拿token
        }
        //登入校驗邏輯,這裡推薦JWT方式,做登入鑑權
        if (token == null || "".equals(token)) {
            //4、不讓繼續往下走
            currentContext.setSendZuulResponse(false);
            //5、設定狀態碼,401,Unauthorized
            currentContext.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
            sendJsonMessage(currentContext.getResponse(),"使用者未登入");
        }
        return null;
    }
    /**
     * 響應json資料給前端
     *
     * @param response
     * @param obj
     */
    private void sendJsonMessage(HttpServletResponse response, Object obj) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            response.setContentType("application/json;charset=utf-8");
            PrintWriter writer = response.getWriter();
            writer.print(objectMapper.writeValueAsString(obj));
            writer.close();
            writer.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

使用者登入鑑權測試

補充

  登入鑑權,推薦使用JWT方式,下面我提供我之前的一個專案,JWT的工具類,和攔截器的部分關鍵程式碼

Spring Cloud 純乾貨,從入門到實戰
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.7.0</version>
        </dependency>
pom.xml依賴
Spring Cloud 純乾貨,從入門到實戰
package net.ybclass.online_ybclass.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import net.ybclass.online_ybclass.model.entity.User;

import java.util.Date;

/**
 * JWT工具類
 * 注意點:
 * 1、生成的token,是可以通過base64進行解密出銘文資訊
 * 2、base64進行解密出明文資訊,修改再進行編碼,則會解密失敗
 * 3、無法作廢已頒佈的token,除非改金鑰
 */
public class JWTUtils {
    /**
     * 過期時間,一週
     */
    static final long EXPIRE = 60000 * 60 * 24 * 7;
    /**
     * 加密金鑰
     */
    private static final String SECRET = "ybclass.net168";
    /**
     * 令牌字首
     */
    private static final String TOKEN_PREFIX = "ybclass";
    /**
     * 主題
     */
    private static final String SUBJECT = "ybclass";

    /**
     * 根據使用者資訊,生成令牌
     *
     * @param user
     * @return
     */
    public static String geneJsonWebToken(User user) {
        String token = Jwts.builder().setSubject(SUBJECT)
                .claim("head_img", user.getHeadImg())
                .claim("id", user.getId())
                .claim("name", user.getName())
                .setIssuedAt(new Date()) //令牌頒佈時間
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRE)) //過期時間
                .signWith(SignatureAlgorithm.HS256, SECRET) //加密方式
                .compact();
        token = TOKEN_PREFIX + token;
        return token;
    }

    /**
     * 校驗token方法
     *
     * @param token
     * @return
     */
    public static Claims checkJWT(String token) {
        try {
            final Claims claims = Jwts.parser()
                    .setSigningKey(SECRET)
                    .parseClaimsJws(token.replace(TOKEN_PREFIX, ""))
                    .getBody();
            return claims;
        } catch (Exception e) {
            return null;
        }
    }
}
JWTUtils.java
Spring Cloud 純乾貨,從入門到實戰
try {
            String accesToken = request.getHeader("token");
            if (accesToken == null) {
                accesToken = request.getParameter("token");
            }
            if (StringUtils.isNoneBlank(accesToken)) {
                Claims claims = JWTUtils.checkJWT(accesToken);
                if (claims == null) {
                    sendJsonMessage(response, JsonData.buildError("登陸過期,請重新登陸"));
                    //告訴登陸過期,重新登陸
                    return false;
                }
                Integer id = (Integer) claims.get("id");
                String name = (String) claims.get("name");
                request.setAttribute("user_id", id);
                request.setAttribute("name", name);
                return true;
            }
        } catch (Exception e) {
        }
        //登陸失敗
        sendJsonMessage(response, JsonData.buildError("登陸過期,請重新登陸"));
        return false;



=================
        User user = userMapper.findByPhoneAndPwd(phone, CommonUtils.MD5(pwd));
        return user == null ? null : JWTUtils.geneJsonWebToken(user);
攔截器部分程式碼

閘道器Zuul介面限流

  採用谷歌guava框架,閘道器限流

改造api-gateway專案

建立OrderRatelimiterFilter

  然後繼承ZuulFilter,並使用springcloud繼承的guava技術,只針對訂單介面限流!!!

 

package com.ybchen.apigateway.filter;

import com.google.common.util.concurrent.RateLimiter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletRequest;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;

/**
 * @ClassName:OrderRateLimiterFilter
 * @Description:訂單介面限流
 * @Author:chenyb
 * @Date:2020/11/9 11:32 下午
 * @Versiion:1.0
 */
public class OrderRateLimiterFilter extends ZuulFilter {
    //限流令牌,每秒建立多少令牌,注意:springcloud 預設整合guava
    private static final RateLimiter RATE_LIMITER = RateLimiter.create(1000);

    @Override
    public String filterType() {
        return PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return -4;
    }

    @Override
    public boolean shouldFilter() {
        //1、獲取上下文
        RequestContext currentContext = RequestContext.getCurrentContext();
        //2、獲取HttpServletRequest
        HttpServletRequest request = currentContext.getRequest();
        String url = request.getRequestURI();
        System.out.println("限流請求路徑url=========>" + url);
        //只對訂單介面限流
        if ((url == null ? "" : url.toLowerCase()).startsWith("/apigate/order")) {
            return true;
        }
        return false;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        if (!RATE_LIMITER.tryAcquire()) {
            currentContext.setSendZuulResponse(false);
            currentContext.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value());
        }
        return null;
    }
}

Zuul叢集

  技術棧:nginx+lvs+keepalive

案例原始碼下載

連結: https://pan.baidu.com/s/1bNIh-8nSCMcU7FjVVzlBnA  密碼: 4wf9

 

相關文章