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!

 

转载于:https://www.cnblogs.com/alittlesmile/p/10893969.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值