当前位置:   article > 正文

Spring Cloud(七)Feign实战(ribbon负载均衡)_feign+ribbon实战

feign+ribbon实战

前期准备

1、创建一个Eureka Server工程,项目名称:eureka-server

2、创建一个Eureka Client 服务提供者 工程,项目名称:eureka-client1

工程eureka-server和eureka-client1 创建请参考

Spring Cloud(三)Eureka实战

1、启动Eureka 服务提供者、Eureka注册中心

启动eureka-server、eureka-client1

PS:eureka-client1 在idea中通过多实例启动

在这里插入图片描述

查看 Eureka 控制台

在这里插入图片描述

2、创建并启动Feign服务消费者

项目名称:service-feign

<?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.yi</groupId>
    <artifactId>serice-feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>serice-feign</name>
    <description>serice-feign</description>

    <parent>
        <groupId>com.yi</groupId>
        <artifactId>finchley</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependencies>
        <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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

</project>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8765
spring:
  application:
    name: service-feign
feign:
  hystrix:
    enabled: true

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
package com.yi.sericefeign;

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.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class SericeFeignApplication {

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

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

@EnableFeignClients:开启Feign的功能

@FeignClient(value = "service-client",fallback = HelloWorldServiceImpl.class)
public interface HelloWorldService {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    String helloWorld(@RequestParam(value = "name") String name);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

@ FeignClient(“服务名”):指定调用哪个服务,通过服务名称

@RestController
public class HelloWorldController {

    @Autowired
    HelloWorldService helloWorldService;

    @GetMapping(value = "/helloWorld")
    public String helloWorld(@RequestParam String name) {
        System.out.println("...............service-feign");
        return helloWorldService.helloWorld(name);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

访问:http://localhost:8765/helloWorld?name=yi

轮询出现,与前章节ribbon一致

hello yi ,port is 8762
hello yi ,port is 8763
  • 1
  • 2

git项目源码:https://github.com/yilei111/-Spring-Cloud-Finchley

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/76488
推荐阅读
相关标签
  

闽ICP备14008679号