• spring cloud:feign-hystrix


    producer

    1. File-->new spring starter project

    2.add 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-web</artifactId>
            </dependency>

    3.Edit application.yml

    server:
      port: 8005
    spring:
      application:
        name: producer
        
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/

    4.program

    package com.smile;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    @SpringBootApplication
    @EnableEurekaClient
    public class ProducerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ProducerApplication.class, args);
        }
    
    }
    package com.smile.controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class ProducerController {
        
        @RequestMapping("/getHello")
        public String getGetHello(@RequestParam String name) {
            return "hello "+name;
        }
    }

    5.Run 

    visit:  http://localhost:8005/getHello?name=smile

    consumer

    1. File-->new spring starter project

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

    3.Edit application.yml

    server:
      port: 8006
    spring:
      application:
        name: consumer
        
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/

    4.program

    package com.smile;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    @SpringBootApplication
    @EnableEurekaClient
    @EnableFeignClients
    public class ConsumerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConsumerApplication.class, args);
        }
    
    }
    package com.smile.controller;
    
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.smile.remote.HelloService;
    
    @RestController
    public class ConsumerController {
        
        @Autowired
        HelloService helloService;
        
        @RequestMapping("/hello/{name}")
        public String helol(@PathVariable("name") String name){
            return helloService.getHello(name);
        }
    }
    package com.smile.remote;
    
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    @FeignClient(name = "producer")
    public interface HelloService {
        
        @RequestMapping("/getHello")
        public String getHello(@RequestParam String name);
    }

    5.Run

    负载均衡

    new -->srping starter project -->producer-1

    package com.smile.controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class ProducerController {
        
        @RequestMapping("/getHello")
        public String getGetHello(@RequestParam String name) {
            return "hello "+name+",this is producer 1";
        }
    }

    修改port

    server:
      port: 8004
    spring:
      application:
        name: producer
        
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/

    其它均与producer一致

    打包启动后,在eureka就会发现两个服务提供者,如下图:

    然后在浏览器再次输入:http://localhost:9001/hello/wang 进行测试:

    第一次返回结果:hello wang

    第二次返回结果:hello wang,this is producer 1

    不断的进行测试下去会发现两种结果交替出现,说明两个服务中心自动提供了服务均衡负载的功能。如果我们将服务提供者的数量在提高为N个,测试结果一样,请求会自动轮询到每个服务端来处理。

    hystrix

    1.new file-->spring starter project-->consumer-feign-hystrix

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

    3 application.yml

    server:
      port: 8007
    spring:
      application:
        name: consumer-feign-hystrix
        
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/
          
          
    feign:
      hystrix:
        enabled: true

    4.program

    package com.smile;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.cloud.netflix.hystrix.EnableHystrix;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    @SpringBootApplication
    @EnableEurekaClient
    @EnableFeignClients
    @EnableHystrix
    public class ConsumerFeignHystrixApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConsumerFeignHystrixApplication.class, args);
        }
    
    }
    package com.smile.controller;
    
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.smile.remote.HelloService;
    
    @RestController
    public class ConsumerController {
        
        @Autowired
        HelloService helloService;
        
        @RequestMapping("/hello/{name}")
        public String helol(@PathVariable("name") String name){
            return helloService.getHello(name);
        }
    }
    package com.smile.remote;
    
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    @FeignClient(name = "producer",fallback = HelloServiceHystrix.class)
    public interface HelloService {
        
        @RequestMapping("/getHello")
        public String getHello(@RequestParam String name);
    }
    package com.smile.remote;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class HelloServiceHystrix implements HelloService{
    
        @Override
        public String getHello(String name) {
            return "hello "+name+",this is hystrix!";
        }
    
    }

    5.test

    visit http://localhost:8007/hello/wang 以下两种结果交替出现,项目正常

    hello wang

     hello wang,this is producer 1

    停止 producer 和 producer-1 再次访问

    hello wang,this is hystrix!

  • 相关阅读:
    LeetCode-Read N Characters Given Read4 II
    LeetCode-One Edit Distance
    LeetCode-Palindrome Permutation II
    LeetCode- Longest Absolute File Path
    LeetCode-Strobogrammatic Number II
    LeetCode-Strobogrammatic Number
    LeetCode-Flatten 2D Vector
    LeetCode-Shortest Word Distance III
    LeetCode-Shortest Word Distance II
    Cookie/Session
  • 原文地址:https://www.cnblogs.com/alittlesmile/p/10893969.html
Copyright © 2020-2023  润新知