Skip to content

Commit 6db0d8f

Browse files
committed
to #63
1 parent ab45094 commit 6db0d8f

File tree

6 files changed

+40
-30
lines changed

6 files changed

+40
-30
lines changed

mse-simple-demo/A/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@
6868
<version>2.7.7</version>
6969
</dependency>
7070

71+
<dependency>
72+
<groupId>com.spring4all</groupId>
73+
<artifactId>swagger-spring-boot-starter</artifactId>
74+
<version>1.7.0.RELEASE</version>
75+
</dependency>
76+
7177

7278
</dependencies>
7379

mse-simple-demo/A/src/main/java/com/alibabacloud/mse/demo/AApplication.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,18 @@
1-
/*
2-
* Copyright (C) 2018 the original author or authors.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
161

172
package com.alibabacloud.mse.demo;
183

194
import java.io.File;
205
import java.io.FileReader;
216
import java.io.IOException;
227
import java.util.Enumeration;
8+
import java.util.List;
239
import java.util.Properties;
2410

2511
import javax.servlet.http.HttpServletRequest;
2612

2713
import com.alibabacloud.mse.demo.service.HelloServiceB;
14+
import io.swagger.annotations.ApiOperation;
15+
import io.swagger.annotations.ApiParam;
2816
import org.apache.dubbo.config.annotation.Reference;
2917
import org.springframework.beans.factory.annotation.Autowired;
3018
import org.springframework.boot.SpringApplication;
@@ -91,6 +79,14 @@ public String dubbo(HttpServletRequest request) {
9179
return "A"+SERVICE_TAG+"[" + request.getLocalAddr() + "]" + " -> " +
9280
helloServiceB.hello("A");
9381
}
82+
83+
@GetMapping("swagger-demo")
84+
@ApiOperation(value = "这是一个演示swagger的接口 ", tags = {"首页操作页面"})
85+
public String swagger(@ApiParam(name = "name", value = "我是姓名", required = true) String name,
86+
@ApiParam(name = "age", value = "我是年龄", required = true)int age,
87+
@ApiParam(name = "aliware-products", value = "我是购买阿里云原生产品列表", required = true) List<String> aliwareProducts) {
88+
return "hello swagger";
89+
}
9490
}
9591

9692
public static String SERVICE_TAG="";

mse-simple-demo/A/src/main/java/com/alibabacloud/mse/demo/service/MqConsumer.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
11
package com.alibabacloud.mse.demo.service;
22

3-
//import com.pingpongx.ocean.context.BusinessContext;
43
import java.nio.charset.StandardCharsets;
54
import java.util.List;
5+
6+
import com.alibabacloud.mse.demo.AApplication;
67
import lombok.SneakyThrows;
78
import lombok.extern.slf4j.Slf4j;
89
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
910
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
1011
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
1112
import org.apache.rocketmq.common.message.MessageExt;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.cloud.commons.util.InetUtils;
1215
import org.springframework.stereotype.Component;
16+
import org.springframework.web.client.RestTemplate;
1317

14-
/**
15-
* @author zhujy
16-
*/
1718
@Slf4j
1819
@Component
1920
public class MqConsumer implements MessageListenerConcurrently {
2021

22+
@Autowired
23+
RestTemplate restTemplate;
24+
25+
@Autowired
26+
InetUtils inetUtils;
27+
2128
@SneakyThrows
2229
@Override
2330
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
2431
try {
2532
MessageExt messageExt = list.get(0);
2633
String topic = messageExt.getTopic();
27-
String tags = messageExt.getTags();
2834
String messageString = new String(messageExt.getBody(), StandardCharsets.UTF_8);
35+
String result = "A"+ AApplication.SERVICE_TAG+"[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]" + " -> " +
36+
restTemplate.getForObject("http://sc-B/b", String.class);
2937

30-
log.info("topic:{},tags:{},messageString:{}", topic, tags, messageString);
38+
log.info("topic:{},producer:{},invoke result:{}", topic, messageString, result);
3139

3240
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
3341
} finally {

mse-simple-demo/B/src/main/java/com/alibabacloud/mse/demo/BApplication.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@
3232
import java.io.IOException;
3333
import java.util.Properties;
3434

35-
/**
36-
* @author <a href="mailto:[email protected]">Jim</a>
37-
*/
3835
@SpringBootApplication
3936
public class BApplication {
4037

mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/CApplication.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@
3232
import java.io.IOException;
3333
import java.util.Properties;
3434

35-
/**
36-
* @author <a href="mailto:[email protected]">Jim</a>
37-
*/
3835
@SpringBootApplication
3936
public class CApplication {
4037

mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/service/HelloServiceCImpl.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
import lombok.RequiredArgsConstructor;
44
import lombok.extern.slf4j.Slf4j;
5+
import org.apache.commons.lang3.StringUtils;
56
import org.apache.dubbo.config.annotation.DubboService;
7+
import org.apache.dubbo.rpc.RpcContext;
68
import org.apache.rocketmq.client.producer.DefaultMQProducer;
79
import org.apache.rocketmq.common.message.Message;
810
import org.springframework.beans.factory.annotation.Autowired;
911
import org.springframework.beans.factory.annotation.Value;
1012
import org.springframework.cloud.commons.util.InetUtils;
1113

1214
import java.nio.charset.StandardCharsets;
15+
1316
import static com.alibabacloud.mse.demo.CApplication.SERVICE_TAG;
1417

1518
@Slf4j
@@ -25,19 +28,22 @@ public class HelloServiceCImpl implements HelloServiceC {
2528
@Value("${rocketmq.consumer.topic}")
2629
private String topic;
2730

28-
29-
3031
@Override
3132
public String hello(String name) {
3233

3334
String value = "C" + SERVICE_TAG + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]";
35+
String invokerTag="";
36+
String userData = RpcContext.getContext().getAttachment("__microservice_tag__");
37+
if (!StringUtils.isEmpty(userData)) {
38+
invokerTag = StringUtils.substringBefore(userData,",").split("\"")[3];
39+
}
3440

3541
try {
3642
Message msg = new Message();
3743
msg.setTopic(topic);
38-
msg.setTags(SERVICE_TAG);
3944
msg.setBody(value.getBytes(StandardCharsets.UTF_8));
4045
producer.send(msg);
46+
log.info("topic:{},messageString:{},__microservice_tag__:{}", topic, value, StringUtils.trimToNull(invokerTag));
4147
} catch (Exception e) {
4248
log.error("error:", e);
4349
}

0 commit comments

Comments
 (0)