1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

update examples

This commit is contained in:
fangjian0423 2019-10-31 16:14:33 +08:00
parent f1d522be50
commit 591713b738
22 changed files with 270 additions and 48 deletions

View File

@ -2,7 +2,6 @@ package com.alibaba.cloud.examples;
import com.alibaba.cloud.examples.ConsumerApplication.EchoService;
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

View File

@ -0,0 +1,28 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.examples;
public class UrlCleaner {
public static String clean(String url) {
System.out.println("enter urlCleaner");
if (url.matches(".*/echo/.*")) {
System.out.println("change url");
url = url.replaceAll("/echo/.*", "/echo/{str}");
}
return url;
}
}

View File

@ -1,7 +1,8 @@
spring.application.name=service-consumer
server.port=18083
management.endpoints.web.exposure.include=*
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
#spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.server-addr=127.0.0.1:8848
feign.sentinel.enabled=true

View File

@ -19,7 +19,7 @@ import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class OssApplication {
public static final String BUCKET_NAME = "spring-cloud-alibaba-test";
public static final String BUCKET_NAME = "spring-cloud-alibaba";
public static void main(String[] args) throws URISyntaxException {
SpringApplication.run(OssApplication.class, args);

View File

@ -1,5 +1,23 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.examples;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import com.aliyun.oss.OSS;
@ -10,12 +28,13 @@ import org.apache.commons.codec.CharEncoding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* OSS Controller
* OSS Controller.
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
@ -25,8 +44,11 @@ public class OssController {
@Autowired
private OSS ossClient;
@Value("classpath:/oss-test.json")
private Resource localFile;
@Value("oss://" + OssApplication.BUCKET_NAME + "/oss-test.json")
private Resource file;
private Resource remoteFile;
@GetMapping("/upload")
public String upload() {
@ -45,7 +67,7 @@ public class OssController {
public String fileResource() {
try {
return "get file resource success. content: " + StreamUtils.copyToString(
file.getInputStream(), Charset.forName(CharEncoding.UTF_8));
remoteFile.getInputStream(), Charset.forName(CharEncoding.UTF_8));
}
catch (Exception e) {
e.printStackTrace();
@ -67,4 +89,20 @@ public class OssController {
}
}
@GetMapping("/upload2")
public String uploadWithOutputStream() {
try {
try (OutputStream outputStream = ((WritableResource) this.remoteFile)
.getOutputStream();
InputStream inputStream = localFile.getInputStream()) {
StreamUtils.copy(inputStream, outputStream);
}
}
catch (Exception ex) {
ex.printStackTrace();
return "upload with outputStream failed";
}
return "upload success";
}
}

View File

@ -1,6 +1,7 @@
package com.alibaba.cloud.examples;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;
@ -11,8 +12,8 @@ import org.springframework.stereotype.Service;
public class ReceiveService {
@StreamListener("input1")
public void receiveInput1(String receiveMsg) {
System.out.println("input1 receive: " + receiveMsg);
public void receiveInput1(Message message) {
System.out.println("input1 receive: " + message.getPayload() + ", foo header: " + message.getHeaders().get("foo"));
}
@StreamListener("input2")

View File

@ -4,6 +4,7 @@ spring.cloud.stream.bindings.input1.destination=test-topic
spring.cloud.stream.bindings.input1.content-type=text/plain
spring.cloud.stream.bindings.input1.group=test-group1
spring.cloud.stream.rocketmq.bindings.input1.consumer.orderly=true
spring.cloud.stream.rocketmq.bindings.input1.consumer.trustedPackages=com.alibaba.cloud
spring.cloud.stream.bindings.input2.destination=test-topic
spring.cloud.stream.bindings.input2.content-type=text/plain

View File

@ -32,13 +32,6 @@
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-kafka</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,31 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.examples;
import org.springframework.cloud.stream.binder.PartitionSelectorStrategy;
/**
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
public class MyPartitionSelectorStrategy implements PartitionSelectorStrategy {
@Override
public int selectPartition(Object key, int partitionCount) {
System.out
.println("partition key: " + key + ", partitionCount: " + partitionCount);
return 0;
}
}

View File

@ -9,8 +9,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
/**
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
@ -28,6 +29,9 @@ public class RocketMQProduceApplication {
@Output("output3")
MessageChannel output3();
@Output("output4")
MessageChannel output4();
}
public static void main(String[] args) {
@ -44,6 +48,11 @@ public class RocketMQProduceApplication {
return new CustomRunner("output3");
}
@Bean
public CustomRunner customRunner3() {
return new CustomRunner("output4");
}
@Bean
public CustomRunnerWithTransactional customRunnerWithTransactional() {
return new CustomRunnerWithTransactional();
@ -88,6 +97,15 @@ public class RocketMQProduceApplication {
.send(MessageBuilder.withPayload(msgContent).build());
}
}
else if (this.bindingName.equals("output4")) {
int count = 5;
for (int index = 1; index <= count; index++) {
String msgContent = "partitionMsg-" + index;
Message message = MessageBuilder.withPayload(msgContent)
.setHeader("myPartitionKey", "myPartitionKey").build();
mySource.output4().send(message);
}
}
}
}

View File

@ -5,6 +5,7 @@ import java.util.stream.Stream;
import com.alibaba.cloud.examples.RocketMQProduceApplication.MySource;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.rocketmq.common.message.MessageConst;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
@ -24,6 +25,8 @@ public class SenderService {
@Autowired
private MySource source;
private ObjectMapper objectMapper = new ObjectMapper();
public void send(String msg) throws Exception {
source.output1().send(MessageBuilder.withPayload(msg).build());
}
@ -38,6 +41,7 @@ public class SenderService {
public <T> void sendObject(T msg, String tag) throws Exception {
Message message = MessageBuilder.withPayload(msg)
.setHeader(MessageConst.PROPERTY_TAGS, tag)
.setHeader("foo", new Foo(1, "bar"))
.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
.build();
source.output1().send(message);

View File

@ -6,6 +6,7 @@ spring.cloud.stream.bindings.output1.destination=test-topic
spring.cloud.stream.bindings.output1.content-type=application/json
spring.cloud.stream.rocketmq.bindings.output1.producer.group=binder-group
spring.cloud.stream.rocketmq.bindings.output1.producer.sync=true
spring.cloud.stream.rocketmq.bindings.output1.producer.sendMessageTimeout=30000
spring.cloud.stream.bindings.output2.destination=TransactionTopic
spring.cloud.stream.bindings.output2.content-type=application/json
@ -16,6 +17,13 @@ spring.cloud.stream.bindings.output3.destination=pull-topic
spring.cloud.stream.bindings.output3.content-type=text/plain
spring.cloud.stream.rocketmq.bindings.output3.producer.group=pull-binder-group
spring.cloud.stream.bindings.output4.destination=partition-topic
spring.cloud.stream.bindings.output4.content-type=text/plain
spring.cloud.stream.bindings.output4.producer.partition-key-expression=headers['myPartitionKey']
spring.cloud.stream.bindings.output4.producer.partition-selector-class=com.alibaba.cloud.examples.MyPartitionSelectorStrategy
spring.cloud.stream.bindings.output4.producer.partition-count=8
spring.cloud.stream.rocketmq.bindings.output4.producer.group=partition-binder-group
spring.application.name=rocketmq-produce-example
server.port=28081

View File

@ -1,3 +1,19 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.examples;
/**

View File

@ -1,3 +1,19 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.examples;
import org.apache.dubbo.config.annotation.Reference;
@ -7,7 +23,9 @@ import org.apache.dubbo.config.annotation.Reference;
*/
public class FooServiceConsumer {
@Reference(version = "${foo.service.version}", application = "${dubbo.application.id}", url = "dubbo://localhost:12345", timeout = 30000)
@Reference(version = "${foo.service.version}",
application = "${dubbo.application.id}",
url = "dubbo://localhost:12345?version=1.0.0", timeout = 30000)
private FooService fooService;
public String hello(String name) {

View File

@ -1,3 +1,19 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.examples;
import java.util.Collections;

View File

@ -1,3 +1,19 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.examples;
import org.apache.dubbo.config.annotation.Service;
@ -5,11 +21,13 @@ import org.apache.dubbo.config.annotation.Service;
/**
* @author fangjian
*/
@Service(version = "${foo.service.version}", application = "${dubbo.application.id}", protocol = "${dubbo.protocol.id}", registry = "${dubbo.registry.id}")
@Service(version = "${foo.service.version}", application = "${dubbo.application.id}",
protocol = "${dubbo.protocol.id}", registry = "${dubbo.registry.id}")
public class FooServiceImpl implements FooService {
@Override
public String hello(String name) {
return "hello, " + name;
}
}

View File

@ -1,3 +1,19 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.examples;
import org.springframework.boot.WebApplicationType;

View File

@ -31,8 +31,8 @@ spring:
ruleType: gw-api-group
transport:
dashboard: localhost:8080
filter:
enabled: true
# filter:
# enabled: true
scg.fallback:
mode: redirect
redirect: http://www.taobao.com

View File

@ -13,8 +13,8 @@ spring:
ruleType: gw-api-group
transport:
dashboard: localhost:8080
filter:
enabled: false
# filter:
# enabled: true
management.endpoints.web.exposure.include: "*"

View File

@ -30,12 +30,15 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
@ -52,6 +55,8 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;
@EnableDiscoveryClient
@EnableAutoConfiguration
@EnableFeignClients
@EnableScheduling
@EnableCaching
public class DubboSpringCloudConsumerBootstrap {
@Reference
@ -147,6 +152,11 @@ public class DubboSpringCloudConsumerBootstrap {
@Bean
public ApplicationRunner callRunner() {
return arguments -> {
callAll();
};
}
private void callAll() {
// To call /path-variables
callPathVariables();
@ -162,8 +172,11 @@ public class DubboSpringCloudConsumerBootstrap {
// To call /request/body/map
callRequestBodyMap();
}
};
@Scheduled(fixedDelay = 10 * 1000L)
public void onScheduled() {
callAll();
}
private void callPathVariables() {
@ -172,7 +185,7 @@ public class DubboSpringCloudConsumerBootstrap {
// Spring Cloud Open Feign REST Call (Dubbo Transported)
System.out.println(dubboFeignRestService.pathVariables("c", "b", "a"));
// Spring Cloud Open Feign REST Call
System.out.println(feignRestService.pathVariables("b", "a", "c"));
// System.out.println(feignRestService.pathVariables("b", "a", "c"));
// RestTemplate call
System.out.println(restTemplate.getForEntity(
@ -186,7 +199,7 @@ public class DubboSpringCloudConsumerBootstrap {
// Spring Cloud Open Feign REST Call (Dubbo Transported)
System.out.println(dubboFeignRestService.headers("b", 10, "a"));
// Spring Cloud Open Feign REST Call
System.out.println(feignRestService.headers("b", "a", 10));
// System.out.println(feignRestService.headers("b", "a", 10));
}
private void callParam() {
@ -195,7 +208,7 @@ public class DubboSpringCloudConsumerBootstrap {
// Spring Cloud Open Feign REST Call (Dubbo Transported)
System.out.println(dubboFeignRestService.param("mercyblitz"));
// Spring Cloud Open Feign REST Call
System.out.println(feignRestService.param("mercyblitz"));
// System.out.println(feignRestService.param("mercyblitz"));
}
private void callParams() {
@ -204,7 +217,7 @@ public class DubboSpringCloudConsumerBootstrap {
// Spring Cloud Open Feign REST Call (Dubbo Transported)
System.out.println(dubboFeignRestService.params("1", 1));
// Spring Cloud Open Feign REST Call
System.out.println(feignRestService.params("1", 1));
// System.out.println(feignRestService.params("1", 1));
// RestTemplate call
System.out.println(restTemplate.getForEntity(
@ -223,7 +236,7 @@ public class DubboSpringCloudConsumerBootstrap {
// Spring Cloud Open Feign REST Call (Dubbo Transported)
System.out.println(dubboFeignRestService.requestBody("Hello,World", data));
// Spring Cloud Open Feign REST Call
System.out.println(feignRestService.requestBody("Hello,World", data));
// System.out.println(feignRestService.requestBody("Hello,World", data));
// RestTemplate call
System.out.println(restTemplate.postForObject(

View File

@ -2,3 +2,4 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.alibaba.cloud.sentinel.gateway.zuul.SentinelZuulAutoConfiguration,\
com.alibaba.cloud.sentinel.gateway.scg.SentinelSCGAutoConfiguration,\
com.alibaba.cloud.sentinel.gateway.SentinelGatewayAutoConfiguration
org.springframework.boot.env.EnvironmentPostProcessor=com.alibaba.cloud.sentinel.gateway.GatewayEnvironmentPostProcessor