Producer & Consumer
需要与Eureka结合使用
Producer
一、pom文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>peter.test</groupId>
<artifactId>producer1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>producer1</name>
<description>Demo project for Spring Eureka Producer</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>10</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties> <dependencies>
<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-eureka-server</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency> </dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
二、application.yml文件
---
spring:
application:
name: spring-cloud-producer
profiles: producer1
server:
port: 9010
eureka:
instance:
hostname: peterhost1
#ip-address: 127.0.0.1
#prefer-ip-address: true
client:
service-url:
defaultZone: http://peterhost1:9000/eureka/
三、启动注解与Rest Controller
@SpringBootApplication
@EnableDiscoveryClient
@ComponentScan({"peter.test.producer1","Service"})
public class Producer1Application { public static void main(String[] args) {
SpringApplication.run(Producer1Application.class, args);
}
} @RestController
class TestController{ @RequestMapping("/test")
public String index(){
return "peter host.";
} @RequestMapping("/test2")
public String index(@RequestParam String name) {
return "hello "+name+",this is first messge";
} }
Consumer
一、pom文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>peter.test</groupId>
<artifactId>consumer1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>consumer1</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>10</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties> <dependencies>
<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-eureka-server</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-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency> <!--
Monitor
--> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix-dashboard -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> <!--
turbine
-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-netflix-turbine -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-turbine</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-turbine -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency> </dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
二、application.yml文件
---
spring:
application:
name: spring-cloud-consumer1
profiles: consumer1
server:
port: 9021
feign:
hystrix:
enabled:true
eureka:
instance:
hostname: peterhost1
client:
service-url:
defaultZone: http://peterhost1:9000/eureka/
三、FeignClient请求Producer
@FeignClient(name= "spring-cloud-producer",fallback =Producer1ServiceHelloHystrix.class)
@Component
public interface Producer1Service { @RequestMapping(value = "/hello")
public String hello(@RequestParam(value = "name") String name); }
name="spring-cloud-producer" 指明producer在Eureka中注册的服务名称
四、Hystrix断路器实现
@Component
public class Producer1ServiceHelloHystrix implements Producer1Service{ @Override
public String hello(String name) {
return "hello" +name+", this messge from hystrix. ";
}
}
五、Consumer对外提供的Controller
@RestController
public class ConsumerController { @Autowired
Producer1Service producer1Service; @RequestMapping("/hello/{name}")
public String index(@PathVariable("name") String name) {
System.out.println("log begin\r\n");
String remoteResult="Air";
System.out.println(producer1Service.toString()); try
{
if(producer1Service==null) {
remoteResult="Null remote service."; System.out.println("Null remote service.\r\n");
}
else
remoteResult=producer1Service.hello(name);
}
catch (Exception ex){
remoteResult+="\r\n"+ex.getMessage()+ex.getCause()+ex.getStackTrace().toString();
} return remoteResult;
} @Autowired
private DiscoveryClient discoveryClient; @RequestMapping("/service-instances/{applicationName}")
public List<ServiceInstance> serviceInstancesByApplicationName(
@PathVariable String applicationName) {
return this.discoveryClient.getInstances(applicationName);
} }
六、运行
负载均衡
新建一个Producer的项目,为了区分把Controller中返回的信息做一些调整即可
一、application.yml文件
---
spring:
application:
name: spring-cloud-producer
profiles: producer2
server:
port: 9011
eureka:
instance:
hostname: peterhost2
#ip-address: 127.0.0.1
#prefer-ip-address: true
client:
service-url:
defaultZone: http://peterhost2:9001/eureka/
spring.application.name要和前面一个producer的一样,此处是spring-cloud-producer
二、运行
新的Producer注册至Eureka会有一定的延迟
Producer & Consumer的更多相关文章
- .net IO异步和Producer/Consumer队列实现一分钟n次http请求
简介 最近工作中有一个需求:要求发送http请求到某站点获取相应的数据,但对方网站限制了请求的次数:一分钟最多200次请求. 搜索之后,在stackoverflow网站查到一个类似的问题..但里面用到 ...
- C# Producer Consumer (生产者消费者模式)demo
第一套代码将producer Consumer的逻辑写到from类里了,方便在demo的显示界面动态显示模拟生产和消费的过程. 第二套代码将producer Consumer的逻辑单独写到一个 ...
- Kafka 学习笔记之 Kafka0.11之producer/consumer(Scala)
Kafka0.11之producer/consumer(Scala): KafkaConsumer: import java.util.Properties import org.apache.kaf ...
- Kafka Producer Consumer
Producer API org.apache.kafka.clients.producer.KafkaProducer props.put("bootstrap.servers" ...
- asyncio标准库7 Producer/consumer
使用asyncio.Queue import asyncio import random async def produce(queue, n): for x in range(1, n + 1): ...
- RocketMQ学习笔记(6)----RocketMQ的Client的使用 Producer/Consumer
1. 添加依赖 pom.xml如下: <dependency> <groupId>org.apache.rocketmq</groupId> <artifa ...
- Kafka 学习笔记之 Producer/Consumer (Scala)
既然Kafka使用Scala写的,最近也在慢慢学习Scala的语法,虽然还比较生疏,但是还是想尝试下用Scala实现Producer和Consumer,并且用HashPartitioner实现消息根据 ...
- kafka producer consumer demo(三)
我们在前面把集群搭建起来了,也设置了kafka broker的配置,下面我们用代码来实现一下客户端向kafka发送消息,consumer端从kafka消费数据.大家先不要着急着了解 各种参数的配置,先 ...
- Python实现:生产者消费者模型(Producer Consumer Model)
#!/usr/bin/env python #encoding:utf8 from Queue import Queue import random,threading,time #生产者类 clas ...
随机推荐
- CentOS常用软件安装方法
软件包介绍 源码包(脚本安装包) 二进制包(RPM包,系统默认包) 源码包 优点 开源,如果有足够的能力,可以修改源代码 编译安装,更加适合自己的系统,稳定高效 缺点 安装步骤较多,容易出错 编译过程 ...
- NodeJS什么都能做,为什么还要JAVA?
这张图看起来简单而且很好理解,但没尝试过,会有很多疑问. SPA模式中,后端已供了所需的数据接口,view前端已经可以控制,为什么要多加NodeJS这一层? 多加一层,性能怎么样? 多加一层,前端的工 ...
- Objective-C的Runtime System
[0] Outline -- [1] 版本和平台 -- [2] 与Runtime System交互 -- [3] 方法的动态决议 -- [4] 消息转发 -- [5] 类型编码 -- [6 ...
- module.exports 和 export default
CommonJS模块规范和ES6模块规范完全是两种不同的概念 CommonJS模块规范 Node应用由模块组成,采用CommonJS模块规范. 根据这个规范,每个文件就是一个模块,有自己的作用域.在一 ...
- AT2160 へんなコンパス / Manhattan Compass
传送门 乍一看像是一个计算几何,然后想到了BFS,但是苦于无奈\(O(n^2)\)不会优化 然后以下参考zjq_shadow大佬的思路 显然发现曼哈顿距离很麻烦,除了暴力枚举貌似没什么很好的办法 考虑 ...
- Maven下把父项目下的子项目导出到myeclipse中
第一种在父项目下已有子项目:右击空白------import 第二步Maven4MyEclipse-----------Existing Maven Projects 第三部选择父项目下面的子项目 ...
- github:当你想要使用VSCODE开心提交代码时,出现Git:git@github.com:Permission denied(publickey)解决方案
当你想要使用VSCODE开心提交代码时,出现Git:git@github.com:Permission denied(publickey)弹框 图片: 原因:电脑公钥(publickey)未添加至gi ...
- python模块之datetime方法详细介绍
datetime Python提供了许多内置模块用于操作时间日期,如calendar,time,datetime,这篇文章主要是对datetime进行汇总,datetime模块的借口实现原则更加直观, ...
- 醉盏的第一篇博客-关于title的换行处理
在处理title的时候,有时候我们想要换行,标签元素是不可以的,下面有两种特殊字符来实现 <!DOCTYPE ><html> <head runat="serv ...
- Python 的execfile用法
可以直接执行脚本 而import是将脚本导入另一个文件里,可以看 http://docs.python.org/2/library/functions.html 例如一个Python文件 a.py: ...