Refactor/nacos-discovery-example_for2.2.x (#2940)
* Refactor nacos discovery examples.pull/2943/head
parent
1df238ad25
commit
39db289c17
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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.configuration;
|
||||
|
||||
import com.alibaba.cloud.examples.feign.EchoClient;
|
||||
import com.alibaba.cloud.examples.feign.EchoClientFallback;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* Configuration for Feign.
|
||||
*
|
||||
* @author fangjian0423, MieAh
|
||||
*/
|
||||
public class FeignConfiguration {
|
||||
|
||||
@Bean
|
||||
public EchoClient echoClientFallback() {
|
||||
return new EchoClientFallback();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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.configuration;
|
||||
|
||||
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
|
||||
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* Load balancing and sentinel configuration for RestTemplate.
|
||||
*
|
||||
* @author fangjian0423, MieAh
|
||||
*/
|
||||
@Configuration
|
||||
public class RestTemplateConfiguration {
|
||||
|
||||
@LoadBalanced
|
||||
@Bean
|
||||
@SentinelRestTemplate(urlCleanerClass = UrlCleaner.class, urlCleaner = "clean")
|
||||
public RestTemplate urlCleanedRestTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
@LoadBalanced
|
||||
@Bean
|
||||
@SentinelRestTemplate
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
}
|
20
spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/UrlCleaner.java → spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/configuration/UrlCleaner.java
20
spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/UrlCleaner.java → spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/configuration/UrlCleaner.java
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* 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.feign;
|
||||
|
||||
import com.alibaba.cloud.examples.configuration.FeignConfiguration;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
/**
|
||||
* Provide the external exposure interface of the service calling client.
|
||||
*
|
||||
* @author fangjian0423, MieAh
|
||||
*/
|
||||
@FeignClient(name = "service-provider", fallback = EchoClientFallback.class,
|
||||
configuration = FeignConfiguration.class)
|
||||
public interface EchoClient {
|
||||
|
||||
/**
|
||||
* Call the echo method of the remote provider or roll back when the service is blown.
|
||||
* @param str str
|
||||
* @return {@link String}
|
||||
*/
|
||||
@GetMapping("/echo/{str}")
|
||||
String echo(@PathVariable("str") String str);
|
||||
|
||||
/**
|
||||
* Call the divide method of the remote provider or roll back when the service is
|
||||
* blown.
|
||||
* @param a a
|
||||
* @param b b
|
||||
* @return {@link String}
|
||||
*/
|
||||
@GetMapping("/divide")
|
||||
String divide(@RequestParam("a") Integer a, @RequestParam("b") Integer b);
|
||||
|
||||
/**
|
||||
* Test that the default method calls the remote method is still a remote call.
|
||||
* @param a a
|
||||
* @return {@link String}
|
||||
*/
|
||||
default String divide(Integer a) {
|
||||
return divide(a, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the notFound method of the remote provider or roll back when the service is
|
||||
* blown.
|
||||
* @return {@link String}
|
||||
*/
|
||||
@GetMapping("/notFound")
|
||||
String notFound();
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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.feign;
|
||||
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
/**
|
||||
* When the service is blown, the fallback operation is performed.
|
||||
*
|
||||
* @author fangjian0423, MieAh
|
||||
*/
|
||||
public class EchoClientFallback implements EchoClient {
|
||||
|
||||
@Override
|
||||
public String echo(@PathVariable("str") String str) {
|
||||
return "echo fallback";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String divide(@RequestParam Integer a, @RequestParam Integer b) {
|
||||
return "divide fallback";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String notFound() {
|
||||
return "notFound fallback";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* 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.List;
|
||||
import java.util.Random;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.loadbalancer.reactive.DefaultResponse;
|
||||
import org.springframework.cloud.client.loadbalancer.reactive.EmptyResponse;
|
||||
import org.springframework.cloud.client.loadbalancer.reactive.Request;
|
||||
import org.springframework.cloud.client.loadbalancer.reactive.Response;
|
||||
import org.springframework.cloud.loadbalancer.core.NoopServiceInstanceListSupplier;
|
||||
import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
|
||||
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
|
||||
|
||||
/**
|
||||
* Self-defined randomLoadBalancer.
|
||||
*
|
||||
* @author fangjian0423, MieAh
|
||||
*/
|
||||
public class RandomLoadBalancer implements ReactorServiceInstanceLoadBalancer {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(RandomLoadBalancer.class);
|
||||
|
||||
private ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider;
|
||||
|
||||
private final String serviceId;
|
||||
|
||||
private final Random random;
|
||||
|
||||
public RandomLoadBalancer(
|
||||
ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider,
|
||||
String serviceId) {
|
||||
this.serviceInstanceListSupplierProvider = serviceInstanceListSupplierProvider;
|
||||
this.serviceId = serviceId;
|
||||
this.random = new Random();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Response<ServiceInstance>> choose(Request request) {
|
||||
log.info("random spring cloud loadbalancer active -.-");
|
||||
ServiceInstanceListSupplier supplier = serviceInstanceListSupplierProvider
|
||||
.getIfAvailable(NoopServiceInstanceListSupplier::new);
|
||||
return supplier.get().next().map(this::getInstanceResponse);
|
||||
}
|
||||
|
||||
private Response<ServiceInstance> getInstanceResponse(
|
||||
List<ServiceInstance> instances) {
|
||||
if (instances.isEmpty()) {
|
||||
return new EmptyResponse();
|
||||
}
|
||||
ServiceInstance instance = instances.get(random.nextInt(instances.size()));
|
||||
|
||||
return new DefaultResponse(instance);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,115 @@
|
||||
/*
|
||||
* 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 javax.annotation.Resource;
|
||||
|
||||
import com.alibaba.cloud.examples.feign.EchoClient;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* Example of remote invocation of service fusing and custom load balancing.
|
||||
*
|
||||
* @author fangjian0423, MieAh
|
||||
*/
|
||||
@RestController
|
||||
public class TestController {
|
||||
|
||||
@Resource
|
||||
private RestTemplate urlCleanedRestTemplate;
|
||||
|
||||
@Resource
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@Resource
|
||||
private EchoClient echoClient;
|
||||
|
||||
@Resource
|
||||
private DiscoveryClient discoveryClient;
|
||||
|
||||
@Value("${spring.cloud.loadbalancer.zone:null}")
|
||||
private String zone;
|
||||
|
||||
private static final String SERVICE_PROVIDER_ADDRESS = "http://service-provider";
|
||||
|
||||
@GetMapping("/echo-rest/{str}")
|
||||
public String rest(@PathVariable String str) {
|
||||
return urlCleanedRestTemplate
|
||||
.getForObject(SERVICE_PROVIDER_ADDRESS + "/echo/" + str, String.class);
|
||||
}
|
||||
|
||||
@GetMapping("/zone")
|
||||
public String zone() {
|
||||
return "consumer zone " + zone + "\n" + urlCleanedRestTemplate
|
||||
.getForObject(SERVICE_PROVIDER_ADDRESS + "/zone", String.class);
|
||||
}
|
||||
|
||||
@GetMapping("/echo-feign/{str}")
|
||||
public String feign(@PathVariable String str) {
|
||||
return echoClient.echo(str);
|
||||
}
|
||||
|
||||
@GetMapping("/index")
|
||||
public String index() {
|
||||
return restTemplate.getForObject(SERVICE_PROVIDER_ADDRESS, String.class);
|
||||
}
|
||||
|
||||
@GetMapping("/test")
|
||||
public String test() {
|
||||
return restTemplate.getForObject(SERVICE_PROVIDER_ADDRESS + "/test",
|
||||
String.class);
|
||||
}
|
||||
|
||||
@GetMapping("/sleep")
|
||||
public String sleep() {
|
||||
return restTemplate.getForObject(SERVICE_PROVIDER_ADDRESS + "/sleep",
|
||||
String.class);
|
||||
}
|
||||
|
||||
@GetMapping("/notFound-feign")
|
||||
public String notFound() {
|
||||
return echoClient.notFound();
|
||||
}
|
||||
|
||||
@GetMapping("/divide-feign")
|
||||
public String divide(@RequestParam Integer a, @RequestParam Integer b) {
|
||||
return echoClient.divide(a, b);
|
||||
}
|
||||
|
||||
@GetMapping("/divide-feign2")
|
||||
public String divide(@RequestParam Integer a) {
|
||||
return echoClient.divide(a);
|
||||
}
|
||||
|
||||
@GetMapping("/services/{service}")
|
||||
public Object client(@PathVariable String service) {
|
||||
return discoveryClient.getInstances(service);
|
||||
}
|
||||
|
||||
@GetMapping("/services")
|
||||
public Object services() {
|
||||
return discoveryClient.getServices();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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.config;
|
||||
|
||||
import com.alibaba.cloud.examples.feign.EchoClient;
|
||||
import com.alibaba.cloud.examples.feign.EchoClientFallback;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* Configuration for Feign.
|
||||
*
|
||||
* @author fangjian0423, MieAh
|
||||
*/
|
||||
public class FeignConfiguration {
|
||||
|
||||
@Bean
|
||||
public EchoClient echoClientFallback() {
|
||||
return new EchoClientFallback();
|
||||
}
|
||||
|
||||
}
|
8
spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/MyLoadBalancerConfiguration.java → spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/config/MyLoadBalancerConfiguration.java
8
spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/MyLoadBalancerConfiguration.java → spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/config/MyLoadBalancerConfiguration.java
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* 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.config;
|
||||
|
||||
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Configuration for Self-defined randomLoadBalancer.
|
||||
*
|
||||
* @author fangjian0423, MieAh
|
||||
*/
|
||||
@Configuration
|
||||
@LoadBalancerClient(value = "service-provider",
|
||||
configuration = MyLoadBalancerConfiguration.class)
|
||||
public class MySCLBConfiguration {
|
||||
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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.config;
|
||||
|
||||
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
|
||||
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* Load balancing and sentinel configuration for RestTemplate.
|
||||
*
|
||||
* @author fangjian0423, MieAh
|
||||
*/
|
||||
@Configuration
|
||||
public class RestTemplateConfiguration {
|
||||
|
||||
@LoadBalanced
|
||||
@Bean
|
||||
@SentinelRestTemplate(urlCleanerClass = UrlCleaner.class, urlCleaner = "clean")
|
||||
public RestTemplate urlCleanedRestTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
@LoadBalanced
|
||||
@Bean
|
||||
@SentinelRestTemplate
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
}
|
20
spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/UrlCleaner.java → spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/config/UrlCleaner.java
20
spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/UrlCleaner.java → spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/config/UrlCleaner.java
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* 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.feign;
|
||||
|
||||
import com.alibaba.cloud.examples.config.FeignConfiguration;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
/**
|
||||
* Provide the external exposure interface of the service calling client.
|
||||
*
|
||||
* @author fangjian0423, MieAh
|
||||
*/
|
||||
@FeignClient(name = "service-provider", fallback = EchoClientFallback.class,
|
||||
configuration = FeignConfiguration.class)
|
||||
public interface EchoClient {
|
||||
|
||||
/**
|
||||
* Call the echo method of the remote provider or roll back when the service is blown.
|
||||
* @param str str
|
||||
* @return {@link String}
|
||||
*/
|
||||
@GetMapping("/echo/{str}")
|
||||
String echo(@PathVariable("str") String str);
|
||||
|
||||
/**
|
||||
* Call the divide method of the remote provider or roll back when the service is
|
||||
* blown.
|
||||
* @param a a
|
||||
* @param b b
|
||||
* @return {@link String}
|
||||
*/
|
||||
@GetMapping("/divide")
|
||||
String divide(@RequestParam("a") Integer a, @RequestParam("b") Integer b);
|
||||
|
||||
/**
|
||||
* Test that the default method calls the remote method is still a remote call.
|
||||
* @param a a
|
||||
* @return {@link String}
|
||||
*/
|
||||
default String divide(Integer a) {
|
||||
return divide(a, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the notFound method of the remote provider or roll back when the service is
|
||||
* blown.
|
||||
* @return {@link String}
|
||||
*/
|
||||
@GetMapping("/notFound")
|
||||
String notFound();
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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.feign;
|
||||
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
/**
|
||||
* When the service is blown, the fallback operation is performed.
|
||||
*
|
||||
* @author fangjian0423, MieAh
|
||||
*/
|
||||
public class EchoClientFallback implements EchoClient {
|
||||
|
||||
@Override
|
||||
public String echo(@PathVariable("str") String str) {
|
||||
return "echo fallback";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String divide(@RequestParam Integer a, @RequestParam Integer b) {
|
||||
return "divide fallback";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String notFound() {
|
||||
return "notFound fallback";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
/*
|
||||
* 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.Map;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* Provide interfaces to consumers.
|
||||
*
|
||||
* @author fangjian0423, MieAh
|
||||
*/
|
||||
@RestController
|
||||
public class EchoController {
|
||||
|
||||
@Resource
|
||||
private NacosDiscoveryProperties nacosDiscoveryProperties;
|
||||
|
||||
@GetMapping("/")
|
||||
public ResponseEntity<String> index() {
|
||||
return new ResponseEntity<>("index error", HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
@GetMapping("/test")
|
||||
public ResponseEntity<String> test() {
|
||||
return new ResponseEntity<>("error", HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
@GetMapping("/sleep")
|
||||
public String sleep() {
|
||||
try {
|
||||
Thread.sleep(1000L);
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "ok";
|
||||
}
|
||||
|
||||
@GetMapping("/echo/{string}")
|
||||
public String echo(@PathVariable String string) {
|
||||
return "hello Nacos Discovery " + string;
|
||||
}
|
||||
|
||||
@GetMapping("/divide")
|
||||
public String divide(@RequestParam Integer a, @RequestParam Integer b) {
|
||||
if (b == 0) {
|
||||
return String.valueOf(0);
|
||||
}
|
||||
else {
|
||||
return String.valueOf(a / b);
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/zone")
|
||||
public String zone() {
|
||||
Map<String, String> metadata = nacosDiscoveryProperties.getMetadata();
|
||||
return "provider zone " + metadata.get("zone");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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 javax.annotation.Resource;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
/**
|
||||
* Example of responsive discovery client.
|
||||
*
|
||||
* @author fangjian0423, MieAh
|
||||
*/
|
||||
@RestController
|
||||
public class MyController {
|
||||
|
||||
@Resource
|
||||
private ReactiveDiscoveryClient reactiveDiscoveryClient;
|
||||
|
||||
@Resource
|
||||
private WebClient.Builder webClientBuilder;
|
||||
|
||||
@GetMapping("/all-services")
|
||||
public Flux<String> allServices() {
|
||||
return reactiveDiscoveryClient.getInstances("service-provider")
|
||||
.map(serviceInstance -> serviceInstance.getHost() + ":"
|
||||
+ serviceInstance.getPort());
|
||||
}
|
||||
|
||||
@GetMapping("/service-call/{name}")
|
||||
public Mono<String> serviceCall(@PathVariable("name") String name) {
|
||||
return webClientBuilder.build().get().uri("http://service-provider/echo/" + name)
|
||||
.retrieve().bodyToMono(String.class);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
/**
|
||||
* Configuration for web client.
|
||||
*
|
||||
* @author fangjian0423, MieAh
|
||||
*/
|
||||
public class WebClientConfiguration {
|
||||
|
||||
@Bean
|
||||
@LoadBalanced
|
||||
public WebClient.Builder webClient() {
|
||||
return WebClient.builder();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* 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.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* Provide a service interface to the gateway for forwarding calls.
|
||||
*
|
||||
* @author MieAh
|
||||
*/
|
||||
@RestController
|
||||
public class EchoController {
|
||||
|
||||
@GetMapping("/echo/{string}")
|
||||
public String echo(@PathVariable String string) {
|
||||
return "hello Nacos Discovery " + string;
|
||||
}
|
||||
|
||||
@GetMapping("/divide")
|
||||
public String divide(@RequestParam Integer a, @RequestParam Integer b) {
|
||||
return String.valueOf(a / b);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue