总述

之前搞过SpringCloud项目,但版本是netflix维护的伦敦地铁站名称的版本。现在想做一下笔记,并尝试最新版本,看有什么变动没。结果还真有一堆坑。此项目集成了eureka、feign、ribbon、hystrix、zuul五大组件。

一.项目结构

项目包含两个eureka集成,两个服务提供者producer,两个服务消费者consumer,一个服务路由zuul
在这里插入图片描述

二、父类项目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>
    <groupId>com.ykq</groupId>
    <artifactId>springclouddemo</artifactId>
    <version>1.0.0</version>
    <name>springclouddemo</name>
    <description>Demo project for Spring Cloud</description>
    <packaging>pom</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <java.version>1.8</java.version>
        <springboot.version>2.5.7</springboot.version>
        <springcloud.version>3.0.4</springcloud.version>
        <springcloud.netflix.version>2.2.10.RELEASE</springcloud.netflix.version>
    </properties>
    <modules>
        <module>springcloud-eureka-server1</module>
        <module>springcloud-eureka-server2</module>
        <module>springcloud-consumer1</module>
        <module>springcloud-consumer2</module>
        <module>springcloud-producer1</module>
        <module>springcloud-producer2</module>
        <module>springcloud-zuul</module>
    </modules>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>${springboot.version}</version>
                <type>pom</type>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>${springboot.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter</artifactId>
                <version>${springcloud.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-netflix-eureka-client</artifactId>
                <version>${springcloud.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-loadbalancer</artifactId>
                <version>${springcloud.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
                <version>${springcloud.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
                <version>${springcloud.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
                <version>${springcloud.netflix.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
                <version>${springcloud.netflix.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
                <version>${springcloud.netflix.version}</version>
            </dependency>

            <dependency>
                <groupId>io.github.openfeign</groupId>
                <artifactId>feign-hystrix</artifactId>
                <version>10.12</version>
            </dependency>

            <dependency>
                <groupId>com.netflix.eureka</groupId>
                <artifactId>eureka-client</artifactId>
                <version>1.10.16</version>
            </dependency>

            <dependency>
                <groupId>com.netflix.ribbon</groupId>
                <artifactId>ribbon-eureka</artifactId>
                <version>2.2.5</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

三、eureka搭建

下面展示两个eureka的文件

1.springcloud-eureka-server1
(1)eureka1的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>com.ykq</groupId>
        <artifactId>springclouddemo</artifactId>
        <version>1.0.0</version>
    </parent>
    <groupId>com.ykq</groupId>
    <artifactId>springcloud-eureka-server1</artifactId>
    <version>1.0.0</version>
    <name>springcloud-eureka-server1</name>
    <description>Demo project for Spring Cloud</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <type>pom</type>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>gson</artifactId>
                    <groupId>com.google.code.gson</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.0.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <classifier>SpringcloudEurekaServerApplication1</classifier>
                    <mainClass>com.ykq.SpringcloudEurekaServerApplication1</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
(2)eureka1的application.properties
spring.application.name=springcloud-eureka-server
server.port=10001
# 不向eureka注册
eureka.client.register-with-eureka=false
# 不从eureka获取注册信息
eureka.client.fetch-registry=false
# 向eureka注册的地址,后面服务名称必须为eureka
eureka.client.serviceUrl.defaultZone=http://localhost:10002/eureka/
(3)eureka1的启动类
package com.ykq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class SpringCloudEurekaServerApplication1 {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudEurekaServerApplication1.class, args);
    }
}
2.springcloud-eureka-server2
(1)eureka2的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>com.ykq</groupId>
        <artifactId>springclouddemo</artifactId>
        <version>1.0.0</version>
    </parent>
    <groupId>com.ykq</groupId>
    <artifactId>springcloud-eureka-server2</artifactId>
    <version>1.0.0</version>
    <name>springcloud-eureka-server2</name>
    <description>Demo project for Spring Cloud</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <type>pom</type>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>gson</artifactId>
                    <groupId>com.google.code.gson</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.0.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <classifier>SpringcloudEurekaServerApplication2</classifier>
                    <mainClass>com.ykq.SpringcloudEurekaServerApplication2</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
(2)eureka2的application.properties
spring.application.name=springcloud-eureka-server
server.port=10002
# 不向eureka注册
eureka.client.register-with-eureka=false
# 不从eureka获取注册信息
eureka.client.fetch-registry=false
# 向eureka注册的地址,后面服务名称必须为eureka
eureka.client.serviceUrl.defaultZone=http://localhost:10001/eureka/
(3)eureka2的启动类
package com.ykq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class SpringCloudEurekaServerApplication2 {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudEurekaServerApplication2.class, args);
    }
}

四、producer搭建

1.springcloud-producer1
(1)producer1的项目结构

在这里插入图片描述

(2)producer1的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>com.ykq</groupId>
        <artifactId>springclouddemo</artifactId>
        <version>1.0.0</version>
    </parent>
    <groupId>com.ykq</groupId>
    <artifactId>springcloud-producer1</artifactId>
    <version>1.0.0</version>
    <name>springcloud-producer1</name>
    <description>Demo project for Spring Cloud</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <type>pom</type>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-eureka-client</artifactId>
        </dependency>

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

        <dependency>
            <groupId>com.netflix.eureka</groupId>
            <artifactId>eureka-client</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>gson</artifactId>
                    <groupId>com.google.code.gson</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.0.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <classifier>SpringcloudProducerApplication1</classifier>
                    <mainClass>com.ykq.SpringcloudProducerApplication1</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
(3)producer1的application.properties
spring.application.name=springcloud-producer
server.port=10003
eureka.client.serviceUrl.defaultZone=http://localhost:10001/eureka/,http://localhost:10002/eureka/

eureka.instance.prefer-ip-address=true
(4)producer1的启动类
package com.ykq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;

@EnableHystrix
@EnableDiscoveryClient
@SpringBootApplication
public class SpringCloudProducerApplication1 {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudProducerApplication1.class, args);
    }
}
(5)producer1的HelloController1
package com.ykq.hello;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: kqyin
 * @date: 2021/11/24 14:49
 * @Description: hello world
 */
@RestController
public class HelloController1 {

    @RequestMapping(value = "/hello")
    public String hello() {
        return "hello world, I am producer1";
    }
}
(6)producer1的ProducerController1
package com.ykq.producer;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: kqyin
 * @date: 2021/12/2 9:56
 * @Description:
 */
@RestController
public class ProducerController1 {
    @RequestMapping("/producerSupport")
    public String producer() {
        return "Hello, this is producer1 at your service!";
    }
}
(7)producer1的TestController1
package com.ykq.test;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: kqyin
 * @date: 2021/11/25 13:57
 * @Description:
 */
@RestController
public class TestController1 {

    @HystrixCommand(fallbackMethod = "hystrixFallBack")
    @RequestMapping(value = "/test")
    public String hystrix() {
        throw new RuntimeException();
    }

    public String hystrixFallBack() {
        return "I am hystrixFallBack1";
    }
}
2.springcloud-producer2
(1)producer2的项目结构

在这里插入图片描述

(2)producer2的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>com.ykq</groupId>
        <artifactId>springclouddemo</artifactId>
        <version>1.0.0</version>
    </parent>
    <groupId>com.ykq</groupId>
    <artifactId>springcloud-producer2</artifactId>
    <version>1.0.0</version>
    <name>springcloud-producer2</name>
    <description>Demo project for Spring Cloud</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <type>pom</type>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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


        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-eureka-client</artifactId>
        </dependency>

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

        <dependency>
            <groupId>com.netflix.eureka</groupId>
            <artifactId>eureka-client</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>gson</artifactId>
                    <groupId>com.google.code.gson</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.0.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <classifier>SpringcloudProducerApplication2</classifier>
                    <mainClass>com.ykq.SpringcloudProducerApplication2</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
(3)producer2的application.properties
spring.application.name=springcloud-producer
server.port=10004
eureka.client.serviceUrl.defaultZone=http://localhost:10001/eureka/,http://localhost:10002/eureka/

eureka.instance.prefer-ip-address=true
(4)producer2的启动类
package com.ykq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;

@EnableHystrix
@EnableDiscoveryClient
@SpringBootApplication
public class SpringCloudProducerApplication2 {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudProducerApplication2.class, args);
    }
}
(5)producer2的HelloController2
package com.ykq.hello;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: kqyin
 * @date: 2021/11/24 14:49
 * @Description: hello world
 */
@RestController
public class HelloController2 {

    @RequestMapping(value = "/hello")
    public String hello() {
        return "hello world, I am producer2";
    }
}
(6)producer2的ProducerController2
package com.ykq.producer;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: kqyin
 * @date: 2021/12/2 9:56
 * @Description:
 */
@RestController
public class ProducerController2 {
    @RequestMapping("/producerSupport")
    public String producer() {
        return "Hello, this is producer2 at your service!";
    }
}
(7)producer2的TestController2
package com.ykq.test;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: kqyin
 * @date: 2021/11/25 13:57
 * @Description:
 */
@RestController
public class TestController2 {

    @HystrixCommand(fallbackMethod = "hystrixFallBack")
    @RequestMapping(value = "/test")
    public String hystrix() {
        throw new RuntimeException();
    }

    public String hystrixFallBack() {
        return "I am hystrixFallBack2";
    }
}

五、consumer搭建

1.springcloud-consumer1
(1)consumer1的项目结构

在这里插入图片描述

(2)consumer1的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springclouddemo</artifactId>
        <groupId>com.ykq</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ykq</groupId>
    <artifactId>springcloud-consumer1</artifactId>
    <version>1.0.0</version>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <type>pom</type>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-eureka-client</artifactId>
        </dependency>

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

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

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

        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-hystrix</artifactId>
        </dependency>

        <dependency>
            <groupId>com.netflix.eureka</groupId>
            <artifactId>eureka-client</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>gson</artifactId>
                    <groupId>com.google.code.gson</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.0.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <classifier>SpringCloudConsumerApplication1</classifier>
                    <mainClass>com.ykq.SpringCloudConsumerApplication1</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
(3)consumer1的application.properties
spring.application.name=springcloud-consumer
server.port=10005
eureka.client.serviceUrl.defaultZone=http://localhost:10001/eureka/,http://localhost:10002/eureka/
eureka.instance.appname=springcloud-consumer
server.servlet.context-path=/springcloud-consumer
# 开启hystrix断路器,老版本为feign。hystrix.enable==true
feign.circuitbreaker.enabled=true
#允许存在多个Feign调用相同Service的接口
spring.main.allow-bean-definition-overriding=true

eureka.instance.prefer-ip-address=true
(4)consumer1的启动类
package com.ykq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author: kqyin
 * @date: 2021/11/24 15:00
 * @Description:
 */
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class SpringCloudConsumerApplication1 {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudConsumerApplication1.class, args);
    }
}
(5)consumer1的HelloController
package com.ykq.hello;

import com.ykq.api.HelloFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: kqyin
 * @date: 2021/11/24 15:04
 * @Description:
 */
@RestController
public class HelloController {
    @Autowired
    private HelloFeign helloFeign;

    @RequestMapping(value = "/hello")
    public String hello() {
        return helloFeign.hello();
    }
}
(6)consumer1的HelloFeign
package com.ykq.api;

import com.ykq.api.hystrix.HelloFeignHystrix;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author: kqyin
 * @date: 2021/11/24 15:06
 * @Description:
 */
@FeignClient(name= "springcloud-producer", fallback= HelloFeignHystrix.class)
public interface HelloFeign {
    @RequestMapping(value = "/hello")
    String hello();
}
(7)consumer1的HelloFeignHystrix
package com.ykq.api.hystrix;

import com.ykq.api.HelloFeign;
import org.springframework.stereotype.Component;

/**
 * @author: kqyin
 * @date: 2021/11/25 10:00
 * @Description: helloFeign断路器
 */
@Component
public class HelloFeignHystrix implements HelloFeign {
    public String hello() {
        return "hello world, I am feign hystrix1";
    }
}
(8)consumer1的ConsumerController
package com.ykq.consumer;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: kqyin
 * @date: 2021/12/2 9:52
 * @Description:
 */
@RestController
public class ConsumerController {
    @RequestMapping("/consumerSupport")
    public String consumer(){
        return "Hello, this is consumer1 at your service!";
    }
}
(9)consumer1的TestController
package com.ykq.test;

import com.ykq.api.TestFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: kqyin
 * @date: 2021/11/25 14:08
 * @Description:
 */
@RestController
public class TestController {
    @Autowired
    private TestFeign testFeign;

    @RequestMapping("/test")
    public String test() {
        return testFeign.test();
    }
}
(10)consumer的TestFeign
package com.ykq.api;

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

/**
 * @author: kqyin
 * @date: 2021/11/25 14:08
 * @Description:
 */
@FeignClient(name= "springcloud-producer")
public interface TestFeign {
    @RequestMapping(value = "/test")
    String test();
}
2.springcloud-consumer2
(1)consumer2的项目结构

在这里插入图片描述

(2)consumer2的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springclouddemo</artifactId>
        <groupId>com.ykq</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ykq</groupId>
    <artifactId>springcloud-consumer2</artifactId>
    <version>1.0.0</version>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <type>pom</type>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</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-actuator</artifactId>
            <version>2.5.7</version>
        </dependency>-->

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-eureka-client</artifactId>
        </dependency>

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

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

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

        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-hystrix</artifactId>
        </dependency>

        <dependency>
            <groupId>com.netflix.eureka</groupId>
            <artifactId>eureka-client</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>gson</artifactId>
                    <groupId>com.google.code.gson</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.0.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <classifier>SpringCloudConsumerApplication2</classifier>
                    <mainClass>com.ykq.SpringCloudConsumerApplication2</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
(3)consumer2的application.properties
spring.application.name=springcloud-consumer
server.port=10006
eureka.client.serviceUrl.defaultZone=http://localhost:10001/eureka/,http://localhost:10002/eureka/
eureka.instance.appname=springcloud-consumer
server.servlet.context-path=/springcloud-consumer
# 开启hystrix断路器,老版本为feign。hystrix.enable==true
feign.circuitbreaker.enabled=true
#允许存在多个Feign调用相同Service的接口
spring.main.allow-bean-definition-overriding=true

eureka.instance.prefer-ip-address=true
(4)consumer2的启动类
package com.ykq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author: kqyin
 * @date: 2021/11/24 15:00
 * @Description:
 */
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class SpringCloudConsumerApplication2 {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudConsumerApplication2.class, args);
    }
}
(5)consumer2的HelloController
package com.ykq.hello;

import com.ykq.api.HelloFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: kqyin
 * @date: 2021/11/24 15:04
 * @Description:
 */
@RestController
public class HelloController {
    @Autowired
    private HelloFeign helloFeign;

    @RequestMapping(value = "/hello")
    public String hello() {
        return helloFeign.hello();
    }
}
(6)consumer2的HelloFeign
package com.ykq.api;

import com.ykq.api.hystrix.HelloFeignHystrix;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author: kqyin
 * @date: 2021/11/24 15:06
 * @Description:
 */
@FeignClient(name= "springcloud-producer", fallback= HelloFeignHystrix.class)
public interface HelloFeign {
    @RequestMapping(value = "/hello")
    String hello();
}
(7)consumer2的HelloFeignHystrix
package com.ykq.api.hystrix;

import com.ykq.api.HelloFeign;
import org.springframework.stereotype.Component;

/**
 * @author: kqyin
 * @date: 2021/11/25 10:00
 * @Description: helloFeign断路器
 */
@Component
public class HelloFeignHystrix implements HelloFeign {
    public String hello() {
        return "hello world, I am feign hystrix2";
    }
}
(8)consumer2的ConsumerController
package com.ykq.consumer;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: kqyin
 * @date: 2021/12/2 9:52
 * @Description:
 */
@RestController
public class ConsumerController {
    @RequestMapping("/consumerSupport")
    public String consumer(){
        return "Hello, this is consumer2 at your service!";
    }
}
(9)consumer2的TestController
package com.ykq.test;

import com.ykq.api.TestFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: kqyin
 * @date: 2021/11/25 14:08
 * @Description:
 */
@RestController
public class TestController {
    @Autowired
    private TestFeign testFeign;

    @RequestMapping("/test")
    public String test() {
        return testFeign.test();
    }
}
(10)consumer2的TestFeign
package com.ykq.api;

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

/**
 * @author: kqyin
 * @date: 2021/11/25 14:08
 * @Description:
 */
@FeignClient(name= "springcloud-producer")
public interface TestFeign {
    @RequestMapping(value = "/test")
    String test();
}

六、zuul搭建

1.springcloud-zuul
(1)zuul的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>
        <artifactId>springclouddemo</artifactId>
        <groupId>com.ykq</groupId>
        <version>1.0.0</version>
    </parent>
    <groupId>com.ykq</groupId>
    <artifactId>springcloud-zuul</artifactId>
    <version>1.0.0</version>
    <name>springcloud-zuul</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <type>pom</type>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-eureka-client</artifactId>
        </dependency>

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

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

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

        <dependency>
            <groupId>com.netflix.eureka</groupId>
            <artifactId>eureka-client</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>gson</artifactId>
                    <groupId>com.google.code.gson</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.netflix.ribbon</groupId>
            <artifactId>ribbon-eureka</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.0.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <classifier>SpringCloudZuulApplication</classifier>
                    <mainClass>com.ykq.SpringCloudZuulApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
(2)zuul的application.properties
spring.application.name=springcloud-zuul
server.port=10007

eureka.client.fetch-registry=true
eureka.client.serviceUrl.defaultZone=http://localhost:10001/eureka/,http://localhost:10002/eureka/
eureka.instance.prefer-ip-address=true
#server.servlet.context-path=/zuul

zuul.routes.springcloud-consumer.path=/springcloud-consumer/**
zuul.routes.springcloud-consumer.serviceId=springcloud-consumer
#zuul.routes.springcloud-consumer.url=http://127.0.0.1:10005/
zuul.routes.springcloud-consumer.strip-prefix=false

springcloud-consumer.ribbon.NIWSServerListClassName=com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList
springcloud-consumer.ribbon.ServerListUpdaterClassName=com.netflix.niws.loadbalancer.EurekaNotificationServerListUpdater
# refresh every minute
springcloud-consumer.ribbon.ServerListRefreshInterval=60000


zuul.routes.springcloud-producer.path=/springcloud-producer/**
# 只添加serviceId是不行的,需要指定NIWSServerListClassName和ServerListUpdaterClassName
#zuul.routes.producer.service-id=springcloud-producer
# 单一url没法实现负载
#zuul.routes.springcloud-producer.url=http://127.0.0.1:10003/
# 指定负载url
springcloud-producer.ribbon.listOfServers=http://127.0.0.1:10003/,http://127.0.0.1:10004/


zuul.routes.baidu.path=/json/**
zuul.routes.baidu.url=http://www.json.cn/

# 设置超时时间
zuul.host.connect-timeout-millis=3000
zuul.host.connection-request-timeout-millis=3000

# 强制加载,不设置会进行懒加载。spring 第一次请求会非常慢
zuul.ribbon.eager-load.enabled=true
# ribbon 从eureka拉取数据
ribbon.eureka.enabled=true
(3)zuul的启动类
package com.ykq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@EnableZuulProxy
@EnableDiscoveryClient
@EnableEurekaClient
@SpringBootApplication
public class SpringCloudZuulApplication {

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

}
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐