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

以2.2.1.BUILD-SNAPSHOT 为基准,适配Greenwich版本的Spring Cloud

This commit is contained in:
theonefx
2020-04-03 14:14:47 +08:00
parent e963a39bd4
commit 9444117f59
654 changed files with 7971 additions and 17442 deletions

View File

@@ -1,11 +1,28 @@
/*
* 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.IOException;
import java.io.StringReader;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.listener.Listener;
import org.springframework.beans.factory.annotation.Autowired;
@@ -14,7 +31,9 @@ import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -24,9 +43,57 @@ import org.springframework.web.bind.annotation.RestController;
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public UserConfig userConfig() {
return new UserConfig();
}
}
@ConfigurationProperties(prefix = "user")
class UserConfig {
private int age;
private String name;
private Map<String, Object> map;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
@Override
public String toString() {
return "UserConfig{" + "age=" + age + ", name='" + name + '\'' + ", map=" + map
+ '}';
}
}
@Component
@@ -39,14 +106,14 @@ class SampleRunner implements ApplicationRunner {
int userAge;
@Autowired
private NacosConfigProperties nacosConfigProperties;
private NacosConfigManager nacosConfigManager;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(
String.format("Initial username=%s, userAge=%d", userName, userAge));
nacosConfigProperties.configServiceInstance().addListener(
nacosConfigManager.getConfigService().addListener(
"nacos-config-example.properties", "DEFAULT_GROUP", new Listener() {
/**
@@ -55,9 +122,8 @@ class SampleRunner implements ApplicationRunner {
* For example, config data in Nacos is:
*
* user.name=Nacos user.age=25
*
* @param configInfo latest config data for specific dataId in Nacos
* server
* server
*/
@Override
public void receiveConfigInfo(String configInfo) {
@@ -77,20 +143,34 @@ class SampleRunner implements ApplicationRunner {
}
});
}
}
@RestController
@RefreshScope
class SampleController {
@Autowired
UserConfig userConfig;
@Autowired
private NacosConfigManager nacosConfigManager;
@Value("${user.name}")
String userName;
@Value("${user.age:25}")
int age;
Integer age;
@RequestMapping("/user")
public String simple() {
return "Hello Nacos Config!" + "Hello " + userName + " " + age + "!";
return "Hello Nacos Config!" + "Hello " + userName + " " + age + " [UserConfig]: "
+ userConfig + "!" + nacosConfigManager.getConfigService();
}
}
@RequestMapping("/bool")
public boolean bool() {
return (Boolean) (userConfig.getMap().get("2"));
}
}

View File

@@ -1,2 +1,3 @@
server.port=18084
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

View File

@@ -1,8 +1,25 @@
spring.application.name=nacos-config-example
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
# nacos auth
#nacos certification information
spring.cloud.nacos.username=nacos
spring.cloud.nacos.password=nacos
spring.cloud.nacos.config.shared-data-ids=base-common.properties,common.properties
spring.cloud.nacos.config.refreshable-dataids=common.properties
#spring.cloud.nacos.config.refreshable-dataids=common.properties
#spring.cloud.nacos.config.shared-data-ids=common.properties,base-common.properties
spring.cloud.nacos.config.shared-configs[0]= common333.properties
spring.cloud.nacos.config.shared-configs[1].data-id= common111.properties
spring.cloud.nacos.config.shared-configs[1].group= GROUP_APP1
spring.cloud.nacos.config.shared-configs[1].refresh= true
spring.cloud.nacos.config.shared-configs[2]= common222.properties
#spring.cloud.nacos.config.ext-config[0]=ext.properties
spring.cloud.nacos.config.extension-configs[0].data-id= extension1.properties
spring.cloud.nacos.config.extension-configs[0].refresh= true
spring.cloud.nacos.config.extension-configs[1]= extension2.properties
spring.cloud.nacos.config.extension-configs[2].data-id= extension3.json
#spring.cloud.nacos.config.refresh-enabled=true

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 com.alibaba.cloud.examples.ConsumerApplication.EchoService;
@@ -19,7 +35,7 @@ import org.springframework.web.client.RestTemplate;
* @author xiaojing
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableDiscoveryClient(autoRegister = true)
@EnableFeignClients
public class ConsumerApplication {
@@ -41,32 +57,37 @@ public class ConsumerApplication {
SpringApplication.run(ConsumerApplication.class, args);
}
@FeignClient(contextId = "tt", name = "service-provider", fallback = EchoServiceFallback.class, configuration = FeignConfiguration.class)
@FeignClient(name = "service-provider", fallback = EchoServiceFallback.class, configuration = FeignConfiguration.class)
public interface EchoService {
@GetMapping(value = "/echo/{str}")
@GetMapping("/echo/{str}")
String echo(@PathVariable("str") String str);
@GetMapping(value = "/divide")
@GetMapping("/divide")
String divide(@RequestParam("a") Integer a, @RequestParam("b") Integer b);
default String divide(Integer a) {
return divide(a, 0);
}
@GetMapping(value = "/notFound")
@GetMapping("/notFound")
String notFound();
}
}
class FeignConfiguration {
@Bean
public EchoServiceFallback echoServiceFallback() {
return new EchoServiceFallback();
}
}
class EchoServiceFallback implements EchoService {
@Override
public String echo(@PathVariable("str") String str) {
return "echo fallback";
@@ -81,4 +102,5 @@ class EchoServiceFallback implements EchoService {
public String notFound() {
return "notFound fallback";
}
}

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 com.alibaba.cloud.examples.ConsumerApplication.EchoService;
@@ -43,54 +59,55 @@ public class TestController {
// });
// }
@GetMapping(value = "/echo-rest/{str}")
@GetMapping("/echo-rest/{str}")
public String rest(@PathVariable String str) {
return restTemplate.getForObject("http://service-provider/echo/" + str,
String.class);
}
@GetMapping(value = "/index")
@GetMapping("/index")
public String index() {
return restTemplate1.getForObject("http://service-provider", String.class);
}
@GetMapping(value = "/test")
@GetMapping("/test")
public String test() {
return restTemplate1.getForObject("http://service-provider/test", String.class);
}
@GetMapping(value = "/sleep")
@GetMapping("/sleep")
public String sleep() {
return restTemplate1.getForObject("http://service-provider/sleep", String.class);
}
@GetMapping(value = "/notFound-feign")
@GetMapping("/notFound-feign")
public String notFound() {
return echoService.notFound();
}
@GetMapping(value = "/divide-feign")
@GetMapping("/divide-feign")
public String divide(@RequestParam Integer a, @RequestParam Integer b) {
return echoService.divide(a, b);
}
@GetMapping(value = "/divide-feign2")
@GetMapping("/divide-feign2")
public String divide(@RequestParam Integer a) {
return echoService.divide(a);
}
@GetMapping(value = "/echo-feign/{str}")
@GetMapping("/echo-feign/{str}")
public String feign(@PathVariable String str) {
return echoService.echo(str);
}
@GetMapping(value = "/services/{service}")
@GetMapping("/services/{service}")
public Object client(@PathVariable String service) {
return discoveryClient.getInstances(service);
}
@GetMapping(value = "/services")
@GetMapping("/services")
public Object services() {
return discoveryClient.getServices();
}
}

View File

@@ -1,6 +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;
public class UrlCleaner {
public static String clean(String url) {
System.out.println("enter urlCleaner");
if (url.matches(".*/echo/.*")) {
@@ -9,4 +26,5 @@ public class UrlCleaner {
}
return url;
}
}

View File

@@ -1,9 +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.server-addr=127.0.0.1:8848
# nacos auth
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.username=nacos
spring.cloud.nacos.password=nacos
@@ -18,4 +17,4 @@ spring.cloud.sentinel.datasource.ds1.file.rule-type=flow
spring.cloud.sentinel.datasource.ds2.file.file=classpath: degraderule.json
spring.cloud.sentinel.datasource.ds2.file.data-type=json
spring.cloud.sentinel.datasource.ds2.file.rule-type=degrade
spring.cloud.sentinel.datasource.ds2.file.rule-type=degrade

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.SpringApplication;
@@ -13,7 +29,7 @@ import org.springframework.web.bind.annotation.RestController;
/**
* @author xiaojing
*/
@EnableDiscoveryClient(autoRegister = true)
@EnableDiscoveryClient
@SpringBootApplication
public class ProviderApplication {
@@ -24,17 +40,17 @@ public class ProviderApplication {
@RestController
class EchoController {
@GetMapping(value = "/")
@GetMapping("/")
public ResponseEntity index() {
return new ResponseEntity("index error", HttpStatus.INTERNAL_SERVER_ERROR);
}
@GetMapping(value = "/test")
@GetMapping("/test")
public ResponseEntity test() {
return new ResponseEntity("error", HttpStatus.INTERNAL_SERVER_ERROR);
}
@GetMapping(value = "/sleep")
@GetMapping("/sleep")
public String sleep() {
try {
Thread.sleep(1000L);
@@ -45,14 +61,16 @@ public class ProviderApplication {
return "ok";
}
@GetMapping(value = "/echo/{string}")
@GetMapping("/echo/{string}")
public String echo(@PathVariable String string) {
return "hello Nacos Discovery " + string;
}
@GetMapping(value = "/divide")
@GetMapping("/divide")
public String divide(@RequestParam Integer a, @RequestParam Integer b) {
return String.valueOf(a / b);
}
}
}

View File

@@ -1,8 +1,9 @@
server.port=18082
spring.application.name=service-provider
spring.cloud.nacos.server-addr=127.0.0.1:8848
# nacos auth
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.username=nacos
spring.cloud.nacos.password=nacos
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

View File

@@ -10,7 +10,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-discovery-spring-cloud-config-client</artifactId>
<artifactId>nacos-discovery-spring-cloud-config-client-example</artifactId>
<packaging>jar</packaging>
<description>Example demonstrating how to use nacos discovery</description>

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 the original author or authors.
* 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.
@@ -26,7 +26,7 @@ public class GetConfigController {
@Value("${config}")
private String config;
@GetMapping(value = "/config")
@GetMapping("/config")
public String getConfig() {
return config;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 the original author or authors.
* 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.
@@ -31,4 +31,4 @@ public class SpringCloudConfigClientApplication {
SpringApplication.run(SpringCloudConfigClientApplication.class, args);
}
}
}

View File

@@ -10,7 +10,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-discovery-with-spring-cloud-config-example</artifactId>
<artifactId>nacos-discovery-spring-cloud-config-server-example</artifactId>
<packaging>jar</packaging>
<description>Example demonstrating how to use nacos discovery</description>

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 the original author or authors.
* 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.
@@ -33,4 +33,4 @@ public class SpringCloudConfigServerApplication {
SpringApplication.run(SpringCloudConfigServerApplication.class, args);
}
}
}

View File

@@ -6,10 +6,10 @@ spring:
name: configserver
cloud:
nacos:
discovery:
server-addr: localhost:8848
username: nacos
password: nacos
discovery:
server-addr: localhost:8848
config:
server:
git:

View File

@@ -19,7 +19,7 @@
<modules>
<module>nacos-discovery-consumer-example</module>
<module>nacos-discovery-provider-example</module>
<module>nacos-discovery-spring-cloud-config-server</module>
<module>nacos-discovery-spring-cloud-config-client</module>
<module>nacos-discovery-spring-cloud-config-server-example</module>
<module>nacos-discovery-spring-cloud-config-client-example</module>
</modules>
</project>

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.SpringApplication;

View File

@@ -1,10 +1,10 @@
server.port=18085
spring.application.name=service-gateway
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.username=nacos
spring.cloud.nacos.password=nacos
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
management.endpoints.web.exposure.include=*
# spring cloud route config
@@ -12,4 +12,4 @@ spring.cloud.gateway.routes[0].id=nacos-route
spring.cloud.gateway.routes[0].uri=lb://service-gateway-provider
spring.cloud.gateway.routes[0].predicates[0].name=Path
spring.cloud.gateway.routes[0].predicates[0].args[pattern]=/nacos/**
spring.cloud.gateway.routes[0].filters[0]=StripPrefix=1
spring.cloud.gateway.routes[0].filters[0]=StripPrefix=1

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.SpringApplication;
@@ -21,14 +37,17 @@ public class ProviderApplication {
@RestController
class EchoController {
@GetMapping(value = "/echo/{string}")
@GetMapping("/echo/{string}")
public String echo(@PathVariable String string) {
return "hello Nacos Discovery " + string;
}
@GetMapping(value = "/divide")
@GetMapping("/divide")
public String divide(@RequestParam Integer a, @RequestParam Integer b) {
return String.valueOf(a / b);
}
}
}

View File

@@ -5,4 +5,4 @@ spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.username=nacos
spring.cloud.nacos.password=nacos
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.include=*