SpringCloud微服务实现生产者消费者+ribbon负载均衡
Posted 关耳er
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringCloud微服务实现生产者消费者+ribbon负载均衡相关的知识,希望对你有一定的参考价值。
一、生产者springcloud_eureka_provider
(1)目录展示
(2)导入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--eureka依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependencyManagement> <dependencies> <!--springCloud依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
(3)配置文件application.yml
##应用名称 spring: application: name: eureka-provider ##声明当前eurekaservice的端口号 server: port: 8890 ##配置eureka eureka: client: service-url: defaultZone: http://localhost:8888/eureka
(4)IDoSomeService
package com.zn.service; public interface IDoSomeService { public String doSome(); }
(5)IDoSomeServiceImpl
package com.zn.service.impl; import com.zn.service.IDoSomeService; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class IDoSomeServiceImpl implements IDoSomeService { @RequestMapping("/doSome") @Override public String doSome() { System.out.println("服务提供者!"); return "eureka到达页面hhhh"; } }
(6)启动类
二、生产者springcloud_eureka_consumer
(1)目录展示
(2)导入依赖
<!--eureka依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependencyManagement> <dependencies> <!--springCloud依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
(3)配置文件application.yml
##应用名称 spring: application: name: eureka-consumer ##声明当前eurekaservice的端口号 server: port: 8891 ##配置eureka eureka: client: service-url: defaultZone: http://localhost:8888/eureka
(4)IDoSomeController
package com.zn.controller; import com.zn.service.IDoSomeService; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; @RestController public class IDoSomeController { @Resource RestTemplate restTemplate; /*@Resource IDoSomeService iDoSomeService; */ @RequestMapping("/doSome") public String doSome(){ System.out.println("ConsumerController"); return restTemplate.getForObject("http://eureka-provider/doSome",String.class); //return iDoSomeService.doSome(); } }
(5)StartEurekaConsumer启动类
package com.zn; import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.RandomRule; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication public class StartEurekaConsumer { public static void main(String[] args) { SpringApplication.run(StartEurekaConsumer.class,args); } @Bean @LoadBalanced //实现负载均衡 RestTemplate restTemplate(){ return new RestTemplate(); } }
(6)效果
三、ribbon负载均衡
(1)导入依赖
<!--feign依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
(2)IDoSomeService
package com.zn.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; @FeignClient("eureka-provider") public interface IDoSomeService { @RequestMapping("/doSome") public String doSome(); }
(3)启动类StartEurekaConsumer
package com.zn.controller; import com.zn.service.IDoSomeService; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; @RestController public class IDoSomeController { //@Resource //RestTemplate restTemplate; @Resource IDoSomeService iDoSomeService; @RequestMapping("/doSome") public String doSome(){ System.out.println("ConsumerController"); //return restTemplate.getForObject("http://eureka-provider/doSome",String.class); return iDoSomeService.doSome(); } }
(4)效果
以上是关于SpringCloud微服务实现生产者消费者+ribbon负载均衡的主要内容,如果未能解决你的问题,请参考以下文章
SpringCloud系列十一:SpringCloudStream(SpringCloudStream 简介创建消息生产者创建消息消费者自定义消息通道分组与持久化设置 RoutingKey)(代码片段
微服务实战:落地微服务架构到直销系统(将生产者与消费者接入消息总线)
微服务实践之负载均衡(Spring Cloud Load Balancer)-SpringCloud(2020.0.x)-2