【转载】Consul和Openfeign实现客户端负载均衡

声明:

转载文章署名及贴出原文链接

—————————————————————————————————————————————

知乎-zachary1979

原文链接:

通过consul和openfeign实现客户端负载均衡 - 知乎一、构建消费端 1.1 定义消费接口(feign客户端) @Component //Feign客户端 @FeignClient("spring-cloud-consul-producer") public interface ConsumerFeignClient { @GetMapping("/helloConsul…https://zhuanlan.zhihu.com/p/150022428

 

 

 

———————————————————————————————————————————

——

一、构建消费端

1.1 定义消费接口(feign客户端)

@Component
//Feign客户端
@FeignClient("spring-cloud-consul-producer")
public interface ConsumerFeignClient {
	 @GetMapping("/helloConsul")
	 public String getConsulHello();
}

1.2 在控制器中使用

@RestController
public class ConsumerHelloController {
	@Autowired
	private ConsumerFeignClient consumerFeignClient;
	@GetMapping("/consumer/hello")
    public String consumerHello() {
        return consumerFeignClient.getConsulHello();
    }
}

1.3 消费端也注册到consul

 cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        tags: dev
        serviceName: spring-cloud-consul-producer
        healthCheckPath: /actuator/health
        healthCheckInterval: 15s
        healthCheckUrl: http://127.0.0.1:8080/actuator/health
        register: true

1.4 在pom.xml中加入依赖

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-consul-discovery</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
		</dependency>

二、启动consul

C:\myinfor\javaproj>consul agent -dev
==> Starting Consul agent...
           Version: 'v1.7.4'
           Node ID: '2de2c666-48d6-9656-ca7e-52f94e16f043'
         Node name: 'LAPTOP-22U0U9OO'
        Datacenter: 'dc1' (Segment: '<all>')
            Server: true (Bootstrap: false)
       Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, gRPC: 8502, DNS: 8600)
      Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
           Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false, Auto-Encrypt-TLS: false

==> Log data will now stream in as it occurs:

三、启动两个服务提供者和上面得消费者

四、消费端默认通过轮询得负载策略访问服务提供者

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值