springcloud整合feign实现服务负载均衡,断路器

1.项目目录:

#yyds干货盘点# springcloud整合feign实现服务负载均衡,断路器_spring

创建项目feign作为父类

2.代码实现:

父类依赖

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cxh</groupId>
<artifactId>feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>feign</name>
<description>Demo project for Spring Boot</description>
<packaging>pom</packaging>
<properties>
<java.version>8</java.version>
<spring-cloud-alibaba-dependencies.version>2021.1</spring-cloud-alibaba-dependencies.version>
<spring-cloud-dependencies.version>2021.0.0</spring-cloud-dependencies.version>
</properties>

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

创建module项目feign-client

添加依赖

<parent>
<groupId>com.cxh</groupId>
<artifactId>feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cxh</groupId>
<artifactId>feign-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>feign-client</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</artifactId>
</dependency>

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

<!--服务注册-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>

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

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
</dependencies>

yml配置

server:
port: 8001

spring:
application:
name: feign-client #服务名
profiles:
active: dev #环境设置
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos服务注册

控制器

@Slf4j
@RestController
public class ClientController {

@Value("${server.port}")
private String port;

@GetMapping("/add")
public String add(Integer a, Integer b){
Integer c = a + b;
log.info("端口计算:" + port + ":" + c);
return "端口计算:" + port + ":" + c;
}
}

启动器添加注解

@SpringBootApplication
@EnableDiscoveryClient
public class FeignClientApplication {

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



}

创建module项目feign-client2,这个跟feign-client是一样的,只是yml配置改了个端口,其他代码一样,用于测试负载均衡

server:
port: 8002

spring:
application:
name: feign-client #服务名
profiles:
active: dev #环境设置
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos服务注册

创建module项目feign-consumer

添加依赖

<parent>
<groupId>com.cxh</groupId>
<artifactId>feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cxh</groupId>
<artifactId>feign-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>feign-consumer</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</artifactId>
</dependency>

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

<!--服务注册-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.2.1.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</exclusion>
</exclusions>
</dependency>

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

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>

</dependencies>

yml配置

server:
port: 8003

spring:
application:
name: feign-consumer #服务名
profiles:
active: dev #环境设置
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos服务注册

main:
allow-bean-definition-overriding: true

#开启熔断器
feign:
circuitbreaker:
enabled: true

控制器

@Slf4j
@RestController
public class ConsumerController {

@Autowired
private ClientService clientService;

@GetMapping("/add")
public String add() {
return clientService.add(10, 20);
}



}
@FeignClient(value = "feign-client", fallback = ClientServiceHystrix.class)
public interface ClientService {

@GetMapping("/add")
String add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);

}
@Component
public class ClientServiceHystrix implements ClientService {
@Override
public String add(Integer a, Integer b) {
return "调用失败";
}
}

启动器添加注解

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignConsumerApplication {

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

}

3.实现效果:

启动nacos后,再启动feign-client, feign-client2, feign-consumer项目,打开浏览器​​http://localhost:8003/add​

第一次请求:

端口计算:8001:30

第二次请求:

端口计算:8002:30

关闭feign-client, feign-client2项目后第三请求: 

调用失败