Merge remote-tracking branch 'upstream/2021.x' into refresh_specific_configuration_properties
# Conflicts: # spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/resources/META-INF/additional-spring-configuration-metadata.jsonpull/2437/head
commit
9c4dc69cce
@ -0,0 +1,44 @@
|
||||
# Nacos Config Preference
|
||||
|
||||
## Project instruction
|
||||
|
||||
This project demonstrates how to use config preferences.
|
||||
|
||||
## Example
|
||||
|
||||
1. Start a Nacos server
|
||||
add configuration `test.yml`
|
||||
```yaml
|
||||
configdata:
|
||||
user:
|
||||
name: freeman
|
||||
```
|
||||
|
||||
add configuration `test2.yml`
|
||||
```yaml
|
||||
dev:
|
||||
age: 22
|
||||
```
|
||||
|
||||
2. Set configuration preference
|
||||
|
||||
Set default configuration preference
|
||||
```yaml
|
||||
spring:
|
||||
cloud:
|
||||
nacos:
|
||||
config:
|
||||
preference: remote
|
||||
```
|
||||
|
||||
Specify configuration (test 2.yml) to set configuration preference
|
||||
```yaml
|
||||
spring:
|
||||
config:
|
||||
import:
|
||||
- optional:nacos:test.yml
|
||||
- optional:nacos:test2.yml?preference=local
|
||||
```
|
||||
|
||||
3. Verify
|
||||
Access `localhost`, you should see the value of `freeman: 20`, because `name` uses the configuration center configuration first, and `age` uses the local configuration first.
|
19
spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example/src/main/java/com/alibaba/cloud/examples/SentinelDubboProviderApp.java → spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/src/main/java/com/alibaba/cloud/configpreference/examples/ConfigPreferenceApplication.java
19
spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example/src/main/java/com/alibaba/cloud/examples/SentinelDubboProviderApp.java → spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/src/main/java/com/alibaba/cloud/configpreference/examples/ConfigPreferenceApplication.java
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2013-2022 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.configpreference.examples.controller;
|
||||
|
||||
import com.alibaba.cloud.configpreference.examples.model.UserProperties;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author freeman
|
||||
*/
|
||||
@RestController
|
||||
@RefreshScope
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserProperties userProperties;
|
||||
@Value("${dev.age}")
|
||||
private int age;
|
||||
|
||||
@GetMapping
|
||||
public String getName() {
|
||||
return userProperties.getName() + ": " + age;
|
||||
}
|
||||
|
||||
}
|
41
spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-sample-api/src/main/java/com/alibaba/cloud/dubbo/service/RestService.java → spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/src/main/java/com/alibaba/cloud/configpreference/examples/model/UserProperties.java
41
spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-sample-api/src/main/java/com/alibaba/cloud/dubbo/service/RestService.java → spring-cloud-alibaba-examples/nacos-example/nacos-config-preference-example/src/main/java/com/alibaba/cloud/configpreference/examples/model/UserProperties.java
@ -0,0 +1,3 @@
|
||||
configdata:
|
||||
user:
|
||||
name: aa
|
@ -0,0 +1,31 @@
|
||||
server:
|
||||
port: 80
|
||||
spring:
|
||||
application:
|
||||
name: nacos-config-import-example
|
||||
cloud:
|
||||
nacos:
|
||||
config:
|
||||
group: DEFAULT_GROUP
|
||||
server-addr: 127.0.0.1:8848
|
||||
preference: remote
|
||||
config:
|
||||
import:
|
||||
- optional:nacos:test.yml
|
||||
- optional:nacos:test2.yml?preference=local
|
||||
profiles:
|
||||
active: dev
|
||||
logging:
|
||||
level:
|
||||
# print content
|
||||
com.alibaba.cloud.nacos.configdata: debug
|
||||
---
|
||||
spring:
|
||||
config:
|
||||
activate:
|
||||
on-profile: dev
|
||||
configdata:
|
||||
user:
|
||||
name: bb
|
||||
dev:
|
||||
age: 20
|
@ -1,145 +0,0 @@
|
||||
# Sentinel Dubbo Example
|
||||
## Project Instruction
|
||||
|
||||
This example illustrates how to use Sentinel starter to implement flow control for Spring Cloud applications.
|
||||
|
||||
[Sentinel](https://github.com/alibaba/Sentinel) is an open-source project of Alibaba. Sentinel takes "traffic flow" as the breakthrough point, and provides solutions in areas such as flow control, concurrency, circuit breaking, and load protection to protect service stability.
|
||||
|
||||
[Dubbo](http://dubbo.apache.org/) is a high-performance, java based open source RPC framework.
|
||||
|
||||
This example focus on the integration of Sentinel and Dubbo. You can see more features on [sentinel-core-example](https://github.com/alibaba/spring-cloud-alibaba/tree/master/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example).
|
||||
|
||||
## Demo
|
||||
|
||||
### Connect to Sentinel
|
||||
Before we start the demo, let's learn how to connect Sentinel with Dubbo to a Spring Cloud application.
|
||||
**Note: This section is to show you how to connect to Sentinel. The configurations have been completed in the following example, so you don't need modify the code any more.**
|
||||
|
||||
1. Add dependency spring-cloud-starter-alibaba-sentinel and dubbo-spring-boot-starter in the pom.xml file in your Spring Cloud project.
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.boot</groupId>
|
||||
<artifactId>dubbo-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
2. Configure flow control rules
|
||||
|
||||
Sentinel provide [sentinel-dubbo-adapter](https://github.com/alibaba/Sentinel/tree/master/sentinel-adapter/sentinel-dubbo-adapter) module to support dubbo. to support dubbo. sentinel-starter integrates this feature by default.
|
||||
|
||||
sentinel-dubbo-adapter will using Sentinel to handle resource by Dubbo Filter. You just need to define rules.
|
||||
|
||||
FlowRule flowRule = new FlowRule();
|
||||
flowRule.setResource("dubboResource");
|
||||
flowRule.setCount(10);
|
||||
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
|
||||
flowRule.setLimitApp("default");
|
||||
FlowRuleManager.loadRules(Collections.singletonList(flowRule));
|
||||
|
||||
### Configure and Publish Service
|
||||
|
||||
Define some configs of dubbo in `application.properties` in provider side, like protocol, config registry :
|
||||
|
||||
spring.application.name = dubbo-provider-demo
|
||||
|
||||
foo.service.version = 1.0.0
|
||||
|
||||
dubbo.scan.basePackages = com.alibaba.cloud.examples
|
||||
|
||||
dubbo.application.id = dubbo-provider-demo
|
||||
dubbo.application.name = dubbo-provider-demo
|
||||
|
||||
dubbo.protocol.id = dubbo
|
||||
dubbo.protocol.name = dubbo
|
||||
dubbo.protocol.port = 12345
|
||||
dubbo.protocol.status = server
|
||||
|
||||
dubbo.registry.id = my-registry
|
||||
dubbo.registry.address = N/A
|
||||
|
||||
|
||||
`sentinel-dubbo-api` define a service named FooService:
|
||||
|
||||
package com.alibaba.cloud.examples;
|
||||
public interface FooService {
|
||||
String hello(String name);
|
||||
}
|
||||
|
||||
Define the implement Service annotated by `@Service`:
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
||||
### Service Invocation
|
||||
|
||||
We will configure flow control rules before service invocation in consumer side.
|
||||
|
||||
`sentinel-dubbo-api` define a service named FooService:
|
||||
|
||||
package com.alibaba.cloud.examples;
|
||||
public interface FooService {
|
||||
String hello(String name);
|
||||
}
|
||||
|
||||
The resource name of this service's `hello` method is `com.alibaba.cloud.examples.FooService:hello(java.lang.String)` .
|
||||
|
||||
Configure rules:
|
||||
|
||||
FlowRule flowRule = new FlowRule();
|
||||
flowRule.setResource("com.alibaba.cloud.examples.FooService:hello(java.lang.String)");
|
||||
flowRule.setCount(10);
|
||||
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
|
||||
flowRule.setLimitApp("default");
|
||||
FlowRuleManager.loadRules(Collections.singletonList(flowRule));
|
||||
|
||||
Using the `@Reference` annotation to inject service:
|
||||
|
||||
@Reference(version = "${foo.service.version}", application = "${dubbo.application.id}",
|
||||
path = "dubbo://localhost:12345", timeout = 30000)
|
||||
private FooService fooService;
|
||||
|
||||
Because QPS is 10, we can see that flow control takes effect in this invocation:
|
||||
|
||||
FooServiceConsumer service = applicationContext.getBean(FooServiceConsumer.class);
|
||||
|
||||
for (int i = 0; i < 15; i++) {
|
||||
try {
|
||||
String message = service.hello("Jim");
|
||||
System.out.println((i + 1) + " -> Success: " + message);
|
||||
}
|
||||
catch (SentinelRpcException ex) {
|
||||
System.out.println("Blocked");
|
||||
}
|
||||
catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
### Start Application
|
||||
|
||||
Start the application in IDE or by building a fatjar.
|
||||
|
||||
Provider side:
|
||||
|
||||
1. Start in IDE: Find main class `SentinelDubboProviderApp`, and execute the main method.
|
||||
2. Build a fatjar: Execute command `mvn clean package` to build a fatjar, and run command `java -jar sentinel-dubbo-provider-example.jar` to start the application.
|
||||
|
||||
Consumer side:
|
||||
|
||||
1. Start in IDE: Find main class `SentinelDubboConsumerApp`, and execute the main method.
|
||||
2. Build a fatjar: Execute command `mvn clean package` to build a fatjar, and run command `java -jar sentinel-dubbo-consumer-example.jar` to start the application.
|
@ -1,31 +0,0 @@
|
||||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<parent>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-examples</artifactId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>sentinel-dubbo-api</artifactId>
|
||||
<name>Spring Cloud Starter Alibaba Sentinel x Dubbo - API</name>
|
||||
<description>api for sentinel dubbo example</description>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<version>${maven-deploy-plugin.version}</version>
|
||||
<configuration>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -1,26 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* @author fangjian
|
||||
*/
|
||||
public interface FooService {
|
||||
|
||||
String hello(String name);
|
||||
|
||||
}
|
@ -1,72 +0,0 @@
|
||||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<parent>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-examples</artifactId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
<artifactId>sentinel-dubbo-consumer-example</artifactId>
|
||||
<name>Spring Cloud Starter Alibaba Sentinel x Dubbo - Consumer Example</name>
|
||||
<description>Example demonstrating how to use sentinel with dubbo</description>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>sentinel-dubbo-api</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-apache-dubbo-adapter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>dubbo-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>dubbo</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<version>${maven-deploy-plugin.version}</version>
|
||||
<configuration>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -1,35 +0,0 @@
|
||||
/*
|
||||
* 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.DubboReference;
|
||||
|
||||
/**
|
||||
* @author fangjian
|
||||
*/
|
||||
public class FooServiceConsumer {
|
||||
|
||||
@DubboReference(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) {
|
||||
return fooService.hello(name);
|
||||
}
|
||||
|
||||
}
|
@ -1,74 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
|
||||
import com.alibaba.csp.sentinel.slots.block.SentinelRpcException;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
|
||||
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* @author fangjian
|
||||
*/
|
||||
@SpringBootApplication(scanBasePackages = "com.alibaba.cloud.examples")
|
||||
public class SentinelDubboConsumerApp {
|
||||
|
||||
@Bean
|
||||
public FooServiceConsumer annotationDemoServiceConsumer() {
|
||||
return new FooServiceConsumer();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
FlowRule flowRule = new FlowRule();
|
||||
flowRule.setResource(
|
||||
"com.alibaba.cloud.examples.FooService:hello(java.lang.String)");
|
||||
flowRule.setCount(10);
|
||||
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
|
||||
flowRule.setLimitApp("default");
|
||||
FlowRuleManager.loadRules(Collections.singletonList(flowRule));
|
||||
|
||||
SpringApplicationBuilder consumerBuilder = new SpringApplicationBuilder();
|
||||
ApplicationContext applicationContext = consumerBuilder
|
||||
.web(WebApplicationType.NONE).sources(SentinelDubboConsumerApp.class)
|
||||
.run(args);
|
||||
|
||||
FooServiceConsumer service = applicationContext.getBean(FooServiceConsumer.class);
|
||||
|
||||
for (int i = 0; i < 15; i++) {
|
||||
try {
|
||||
String message = service.hello("Jim");
|
||||
System.out.println((i + 1) + " -> Success: " + message);
|
||||
}
|
||||
catch (SentinelRpcException ex) {
|
||||
System.out.println("Blocked");
|
||||
}
|
||||
catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
spring.application.name = dubbo-consumer-demo
|
||||
|
||||
foo.service.version = 1.0.0
|
||||
|
||||
dubbo.application.id = dubbo-consumer-demo
|
||||
dubbo.application.name = dubbo-consumer-demo
|
||||
|
||||
dubbo.protocol.id = dubbo
|
||||
dubbo.protocol.name = dubbo
|
||||
dubbo.protocol.port = 12345
|
||||
|
@ -1,33 +0,0 @@
|
||||
/*
|
||||
* 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.DubboService;
|
||||
|
||||
/**
|
||||
* @author fangjian
|
||||
*/
|
||||
@DubboService(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;
|
||||
}
|
||||
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
spring.application.name = dubbo-provider-demo
|
||||
|
||||
foo.service.version = 1.0.0
|
||||
|
||||
dubbo.scan.basePackages = com.alibaba.cloud.examples
|
||||
|
||||
dubbo.application.id = dubbo-provider-demo
|
||||
dubbo.application.name = dubbo-provider-demo
|
||||
|
||||
dubbo.protocol.id = dubbo
|
||||
dubbo.protocol.name = dubbo
|
||||
dubbo.protocol.port = 12345
|
||||
dubbo.protocol.status = server
|
||||
|
||||
dubbo.registry.id = my-registry
|
||||
dubbo.registry.address = N/A
|
@ -1,29 +0,0 @@
|
||||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-examples</artifactId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dubbo-examples</artifactId>
|
||||
<version>${revision}</version>
|
||||
<name>Spring Cloud Alibaba Dubbo Examples</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>spring-cloud-dubbo-sample-api</module>
|
||||
<module>spring-cloud-dubbo-server-sample</module>
|
||||
<module>spring-cloud-dubbo-client-sample</module>
|
||||
<module>spring-cloud-dubbo-provider-sample</module>
|
||||
<module>spring-cloud-dubbo-consumer-sample</module>
|
||||
<module>spring-cloud-dubbo-provider-web-sample</module>
|
||||
<module>spring-cloud-dubbo-servlet-gateway-sample</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
@ -1,56 +0,0 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dubbo-examples</artifactId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-cloud-dubbo-client-sample</artifactId>
|
||||
<name>Spring Cloud Dubbo Client Sample</name>
|
||||
|
||||
<dependencies>
|
||||
<!-- Sample API -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-dubbo-sample-api</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Boot dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Dubbo Spring Cloud Starter -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-dubbo</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Cloud Nacos Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -1,50 +0,0 @@
|
||||
/*
|
||||
* 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.dubbo.bootstrap;
|
||||
|
||||
import com.alibaba.cloud.dubbo.service.EchoService;
|
||||
import org.apache.dubbo.config.annotation.DubboReference;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* Dubbo Spring Cloud Client Bootstrap.
|
||||
*
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
@EnableDiscoveryClient
|
||||
@EnableAutoConfiguration
|
||||
@RestController
|
||||
public class DubboSpringCloudClientBootstrap {
|
||||
|
||||
@DubboReference
|
||||
private EchoService echoService;
|
||||
|
||||
@GetMapping("/echo")
|
||||
public String echo(String message) {
|
||||
return echoService.echo(message);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DubboSpringCloudClientBootstrap.class);
|
||||
}
|
||||
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
dubbo:
|
||||
cloud:
|
||||
subscribed-services: spring-cloud-alibaba-dubbo-server
|
||||
protocols:
|
||||
dubbo:
|
||||
port: -1
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: spring-cloud-alibaba-dubbo-client
|
||||
main:
|
||||
allow-bean-definition-overriding: true
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
username: nacos
|
||||
password: nacos
|
||||
server-addr: 127.0.0.1:8848
|
||||
namespace: public
|
||||
|
||||
server:
|
||||
port: 8080
|
@ -1,118 +0,0 @@
|
||||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dubbo-examples</artifactId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-cloud-dubbo-consumer-sample</artifactId>
|
||||
<name>Spring Cloud Dubbo Consumer Sample</name>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Cloud Open Feign -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Retry -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.retry</groupId>
|
||||
<artifactId>spring-retry</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Sample API -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-dubbo-sample-api</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Dubbo Spring Cloud Starter -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-dubbo</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- List all Spring Cloud starters for Service Discovery -->
|
||||
|
||||
<!-- Spring Cloud Nacos Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Eureka Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Zookeeper Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.apache.zookeeper</groupId>
|
||||
<artifactId>zookeeper</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.zookeeper</groupId>
|
||||
<artifactId>zookeeper</artifactId>
|
||||
<version>3.4.14</version>
|
||||
<optional>true</optional>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.curator</groupId>
|
||||
<artifactId>curator-framework</artifactId>
|
||||
<version>${curator.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Cloud Consul Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-bootstrap</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -1,263 +0,0 @@
|
||||
/*
|
||||
* 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.dubbo.bootstrap;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.cloud.dubbo.annotation.DubboTransported;
|
||||
import com.alibaba.cloud.dubbo.service.RestService;
|
||||
import com.alibaba.cloud.dubbo.service.User;
|
||||
import com.alibaba.cloud.dubbo.service.UserService;
|
||||
import org.apache.dubbo.config.annotation.DubboReference;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
|
||||
|
||||
/**
|
||||
* Dubbo Spring Cloud Consumer Bootstrap.
|
||||
*
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
@EnableDiscoveryClient
|
||||
@EnableAutoConfiguration
|
||||
@EnableFeignClients
|
||||
@EnableScheduling
|
||||
@EnableCaching
|
||||
public class DubboSpringCloudConsumerBootstrap {
|
||||
|
||||
@DubboReference
|
||||
private UserService userService;
|
||||
|
||||
@DubboReference(version = "1.0.0", protocol = "dubbo")
|
||||
private RestService restService;
|
||||
|
||||
@Autowired
|
||||
@Lazy
|
||||
private FeignRestService feignRestService;
|
||||
|
||||
@Autowired
|
||||
@Lazy
|
||||
private DubboFeignRestService dubboFeignRestService;
|
||||
|
||||
@Value("${provider.application.name}")
|
||||
private String providerApplicationName;
|
||||
|
||||
@Autowired
|
||||
@LoadBalanced
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@Bean
|
||||
public ApplicationRunner userServiceRunner() {
|
||||
return arguments -> {
|
||||
|
||||
User user = new User();
|
||||
user.setId(1L);
|
||||
user.setName("小马哥");
|
||||
user.setAge(33);
|
||||
|
||||
// save User
|
||||
System.out.printf("UserService.save(%s) : %s\n", user,
|
||||
userService.save(user));
|
||||
|
||||
// find all Users
|
||||
System.out.printf("UserService.findAll() : %s\n", user,
|
||||
userService.findAll());
|
||||
|
||||
// remove User
|
||||
System.out.printf("UserService.remove(%d) : %s\n", user.getId(),
|
||||
userService.remove(user.getId()));
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ApplicationRunner callRunner() {
|
||||
return arguments -> {
|
||||
callAll();
|
||||
};
|
||||
}
|
||||
|
||||
private void callAll() {
|
||||
|
||||
// To call /path-variables
|
||||
callPathVariables();
|
||||
|
||||
// To call /headers
|
||||
callHeaders();
|
||||
|
||||
// To call /param
|
||||
callParam();
|
||||
|
||||
// To call /params
|
||||
callParams();
|
||||
|
||||
// To call /request/body/map
|
||||
callRequestBodyMap();
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelay = 10 * 1000L)
|
||||
public void onScheduled() {
|
||||
callAll();
|
||||
}
|
||||
|
||||
private void callPathVariables() {
|
||||
// Dubbo Service call
|
||||
System.out.println(restService.pathVariables("a", "b", "c"));
|
||||
// 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"));
|
||||
|
||||
// RestTemplate call
|
||||
System.out.println(restTemplate.getForEntity(
|
||||
"http://" + providerApplicationName + "//path-variables/{p1}/{p2}?v=c",
|
||||
String.class, "a", "b"));
|
||||
}
|
||||
|
||||
private void callHeaders() {
|
||||
// Dubbo Service call
|
||||
System.out.println(restService.headers("a", "b", 10));
|
||||
// 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));
|
||||
}
|
||||
|
||||
private void callParam() {
|
||||
// Dubbo Service call
|
||||
System.out.println(restService.param("mercyblitz"));
|
||||
// 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"));
|
||||
}
|
||||
|
||||
private void callParams() {
|
||||
// Dubbo Service call
|
||||
System.out.println(restService.params(1, "1"));
|
||||
// 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));
|
||||
|
||||
// RestTemplate call
|
||||
System.out.println(restTemplate.getForEntity(
|
||||
"http://" + providerApplicationName + "/param?param=小马哥", String.class));
|
||||
}
|
||||
|
||||
private void callRequestBodyMap() {
|
||||
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("id", 1);
|
||||
data.put("name", "小马哥");
|
||||
data.put("age", 33);
|
||||
|
||||
// Dubbo Service call
|
||||
System.out.println(restService.requestBodyMap(data, "Hello,World"));
|
||||
// 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));
|
||||
|
||||
// RestTemplate call
|
||||
System.out.println(restTemplate.postForObject(
|
||||
"http://" + providerApplicationName + "/request/body/map?param=小马哥", data,
|
||||
User.class));
|
||||
}
|
||||
|
||||
@Bean
|
||||
@LoadBalanced
|
||||
@DubboTransported
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(DubboSpringCloudConsumerBootstrap.class)
|
||||
.properties("spring.profiles.active=nacos").run(args);
|
||||
}
|
||||
|
||||
@FeignClient("${provider.application.name}")
|
||||
public interface FeignRestService {
|
||||
|
||||
@GetMapping("/param")
|
||||
String param(@RequestParam("param") String param);
|
||||
|
||||
@PostMapping("/params")
|
||||
String params(@RequestParam("b") String b, @RequestParam("a") int a);
|
||||
|
||||
@PostMapping(value = "/request/body/map", produces = APPLICATION_JSON_VALUE)
|
||||
User requestBody(@RequestParam("param") String param,
|
||||
@RequestBody Map<String, Object> data);
|
||||
|
||||
@GetMapping("/headers")
|
||||
String headers(@RequestHeader("h2") String header2,
|
||||
@RequestHeader("h") String header, @RequestParam("v") Integer value);
|
||||
|
||||
@GetMapping("/path-variables/{p1}/{p2}")
|
||||
String pathVariables(@PathVariable("p2") String path2,
|
||||
@PathVariable("p1") String path1, @RequestParam("v") String param);
|
||||
|
||||
}
|
||||
|
||||
@FeignClient("${provider.application.name}")
|
||||
@DubboTransported(protocol = "dubbo")
|
||||
public interface DubboFeignRestService {
|
||||
|
||||
@GetMapping("/param")
|
||||
String param(@RequestParam("param") String param);
|
||||
|
||||
@PostMapping("/params")
|
||||
String params(@RequestParam("b") String paramB, @RequestParam("a") int paramA);
|
||||
|
||||
@PostMapping(value = "/request/body/map", produces = APPLICATION_JSON_VALUE)
|
||||
User requestBody(@RequestParam("param") String param,
|
||||
@RequestBody Map<String, Object> data);
|
||||
|
||||
@GetMapping("/headers")
|
||||
String headers(@RequestHeader("h2") String header2,
|
||||
@RequestParam("v") Integer value, @RequestHeader("h") String header);
|
||||
|
||||
@GetMapping("/path-variables/{p1}/{p2}")
|
||||
String pathVariables(@RequestParam("v") String param,
|
||||
@PathVariable("p2") String path2, @PathVariable("p1") String path1);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
dubbo:
|
||||
cloud:
|
||||
# The subscribed services in consumer side
|
||||
subscribed-services: ${provider.application.name}
|
||||
protocols:
|
||||
dubbo:
|
||||
port: -1
|
||||
consumer:
|
||||
check: false
|
||||
|
||||
server:
|
||||
port: 0
|
||||
|
||||
provider:
|
||||
application:
|
||||
name: spring-cloud-alibaba-dubbo-provider
|
@ -1,72 +0,0 @@
|
||||
spring:
|
||||
application:
|
||||
name: spring-cloud-alibaba-dubbo-consumer
|
||||
main:
|
||||
allow-bean-definition-overriding: true
|
||||
|
||||
|
||||
# default disable all
|
||||
cloud:
|
||||
nacos:
|
||||
username: nacos
|
||||
password: nacos
|
||||
discovery:
|
||||
enabled: false
|
||||
register-enabled: false
|
||||
zookeeper:
|
||||
enabled: false
|
||||
consul:
|
||||
enabled: false
|
||||
|
||||
eureka:
|
||||
client:
|
||||
enabled: false
|
||||
|
||||
ribbon:
|
||||
nacos:
|
||||
enabled: false
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: nacos
|
||||
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
enabled: true
|
||||
register-enabled: true
|
||||
server-addr: 127.0.0.1:8848
|
||||
|
||||
ribbon:
|
||||
nacos:
|
||||
enabled: true
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: eureka
|
||||
|
||||
eureka:
|
||||
client:
|
||||
enabled: true
|
||||
service-url:
|
||||
defaultZone: http://127.0.0.1:8761/eureka/
|
||||
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: zookeeper
|
||||
cloud:
|
||||
zookeeper:
|
||||
enabled: true
|
||||
connect-string: 127.0.0.1:2181
|
||||
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: consul
|
||||
|
||||
cloud:
|
||||
consul:
|
||||
enabled: true
|
||||
host: 127.0.0.1
|
||||
port: 8500
|
@ -1,152 +0,0 @@
|
||||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dubbo-examples</artifactId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-cloud-dubbo-provider-sample</artifactId>
|
||||
<name>Spring Cloud Dubbo Provider Sample</name>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- Resolve the Dubbo REST RPC issue -->
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Resolve the Spring Cloud registration issue -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Sample API -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-dubbo-sample-api</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Dubbo Spring Cloud Starter -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-dubbo</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- List all Spring Cloud starters for Service Discovery -->
|
||||
|
||||
<!-- Spring Cloud Nacos Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Eureka Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Zookeeper Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.apache.zookeeper</groupId>
|
||||
<artifactId>zookeeper</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.zookeeper</groupId>
|
||||
<artifactId>zookeeper</artifactId>
|
||||
<version>3.4.14</version>
|
||||
<optional>true</optional>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.curator</groupId>
|
||||
<artifactId>curator-framework</artifactId>
|
||||
<version>${curator.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Cloud Consul Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- REST support dependencies -->
|
||||
<dependency>
|
||||
<groupId>io.netty</groupId>
|
||||
<artifactId>netty-all</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jboss.resteasy</groupId>
|
||||
<artifactId>resteasy-jaxrs</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jboss.resteasy</groupId>
|
||||
<artifactId>resteasy-client</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jboss.resteasy</groupId>
|
||||
<artifactId>resteasy-netty4</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jboss.resteasy</groupId>
|
||||
<artifactId>resteasy-jackson-provider</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jboss.resteasy</groupId>
|
||||
<artifactId>resteasy-jaxb-provider</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate.validator</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-bootstrap</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -1,39 +0,0 @@
|
||||
/*
|
||||
* 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.dubbo.bootstrap;
|
||||
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
/**
|
||||
* Dubbo Spring Cloud Provider Bootstrap.
|
||||
*
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
@EnableDiscoveryClient
|
||||
@EnableAutoConfiguration
|
||||
public class DubboSpringCloudProviderBootstrap {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(DubboSpringCloudProviderBootstrap.class)
|
||||
.properties("spring.profiles.active=nacos").web(WebApplicationType.NONE)
|
||||
.run(args);
|
||||
}
|
||||
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
/*
|
||||
* 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.dubbo.service;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.dubbo.config.annotation.DubboService;
|
||||
|
||||
/**
|
||||
* In-Memory {@link UserService} implementation.
|
||||
*/
|
||||
@DubboService(protocol = "dubbo")
|
||||
public class InMemoryUserService implements UserService {
|
||||
|
||||
private Map<Long, User> usersRepository = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public boolean save(User user) {
|
||||
return usersRepository.put(user.getId(), user) == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Long userId) {
|
||||
return usersRepository.remove(userId) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<User> findAll() {
|
||||
return usersRepository.values();
|
||||
}
|
||||
|
||||
}
|
@ -1,123 +0,0 @@
|
||||
/*
|
||||
* 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.dubbo.service;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.FormParam;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.HeaderParam;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
import org.apache.dubbo.config.annotation.DubboService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static com.alibaba.cloud.dubbo.util.LoggerUtils.log;
|
||||
import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON_VALUE;
|
||||
|
||||
/**
|
||||
* Default {@link RestService}.
|
||||
*
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
@DubboService(version = "1.0.0", protocol = { "dubbo", "rest" })
|
||||
@Path("/")
|
||||
public class StandardRestService implements RestService {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@Override
|
||||
@Path("param")
|
||||
@GET
|
||||
public String param(@QueryParam("param") String param) {
|
||||
log("/param", param);
|
||||
return param;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Path("params")
|
||||
@POST
|
||||
public String params(@QueryParam("a") int a, @QueryParam("b") String b) {
|
||||
log("/params", a + b);
|
||||
return a + b;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Path("headers")
|
||||
@GET
|
||||
public String headers(@HeaderParam("h") String header,
|
||||
@HeaderParam("h2") String header2, @QueryParam("v") Integer param) {
|
||||
String result = header + " , " + header2 + " , " + param;
|
||||
log("/headers", result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Path("path-variables/{p1}/{p2}")
|
||||
@GET
|
||||
public String pathVariables(@PathParam("p1") String path1,
|
||||
@PathParam("p2") String path2, @QueryParam("v") String param) {
|
||||
String result = path1 + " , " + path2 + " , " + param;
|
||||
log("/path-variables", result);
|
||||
return result;
|
||||
}
|
||||
|
||||
// @CookieParam does not support : https://github.com/OpenFeign/feign/issues/913
|
||||
// @CookieValue also does not support
|
||||
|
||||
@Override
|
||||
@Path("form")
|
||||
@POST
|
||||
public String form(@FormParam("f") String form) {
|
||||
return String.valueOf(form);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Path("request/body/map")
|
||||
@POST
|
||||
@Produces(APPLICATION_JSON_VALUE)
|
||||
public User requestBodyMap(Map<String, Object> data,
|
||||
@QueryParam("param") String param) {
|
||||
User user = new User();
|
||||
user.setId(((Integer) data.get("id")).longValue());
|
||||
user.setName((String) data.get("name"));
|
||||
user.setAge((Integer) data.get("age"));
|
||||
log("/request/body/map", param);
|
||||
return user;
|
||||
}
|
||||
|
||||
@Path("request/body/user")
|
||||
@POST
|
||||
@Override
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public Map<String, Object> requestBodyUser(User user) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("id", user.getId());
|
||||
map.put("name", user.getName());
|
||||
map.put("age", user.getAge());
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
dubbo:
|
||||
scan:
|
||||
base-packages: com.alibaba.cloud.dubbo.service
|
||||
protocols:
|
||||
dubbo:
|
||||
name: dubbo
|
||||
port: -1
|
||||
rest:
|
||||
name: rest
|
||||
port: 9090
|
||||
server: netty
|
||||
feign:
|
||||
hystrix:
|
||||
enabled: true
|
@ -1,67 +0,0 @@
|
||||
spring:
|
||||
application:
|
||||
name: spring-cloud-alibaba-dubbo-provider
|
||||
main:
|
||||
allow-bean-definition-overriding: true
|
||||
|
||||
|
||||
# default disable all
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
enabled: false
|
||||
register-enabled: false
|
||||
zookeeper:
|
||||
enabled: false
|
||||
consul:
|
||||
enabled: false
|
||||
|
||||
eureka:
|
||||
client:
|
||||
enabled: false
|
||||
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: nacos
|
||||
|
||||
cloud:
|
||||
nacos:
|
||||
username: nacos
|
||||
password: nacos
|
||||
discovery:
|
||||
enabled: true
|
||||
register-enabled: true
|
||||
server-addr: 127.0.0.1:8848
|
||||
ephemeral: false
|
||||
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: eureka
|
||||
|
||||
eureka:
|
||||
client:
|
||||
enabled: true
|
||||
service-url:
|
||||
defaultZone: http://127.0.0.1:8761/eureka/
|
||||
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: zookeeper
|
||||
cloud:
|
||||
zookeeper:
|
||||
enabled: true
|
||||
connect-string: 127.0.0.1:2181
|
||||
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: consul
|
||||
|
||||
cloud:
|
||||
consul:
|
||||
enabled: true
|
||||
host: 127.0.0.1
|
||||
port: 8500
|
@ -1,105 +0,0 @@
|
||||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dubbo-examples</artifactId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-cloud-dubbo-provider-web-sample</artifactId>
|
||||
<name>Spring Cloud Dubbo Provider Web Sample</name>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- Production Ready features -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Sample API -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-dubbo-sample-api</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Dubbo Spring Cloud Starter -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-dubbo</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- List all Spring Cloud starters for Service Discovery -->
|
||||
|
||||
<!-- Spring Cloud Nacos Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Eureka Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Zookeeper Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.apache.zookeeper</groupId>
|
||||
<artifactId>zookeeper</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.zookeeper</groupId>
|
||||
<artifactId>zookeeper</artifactId>
|
||||
<version>3.4.14</version>
|
||||
<optional>true</optional>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.curator</groupId>
|
||||
<artifactId>curator-framework</artifactId>
|
||||
<version>${curator.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Cloud Consul Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -1,37 +0,0 @@
|
||||
/*
|
||||
* 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.dubbo.bootstrap;
|
||||
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
/**
|
||||
* Dubbo Spring Cloud Provider Bootstrap.
|
||||
*
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
@EnableDiscoveryClient
|
||||
@EnableAutoConfiguration
|
||||
public class DubboSpringCloudWebProviderBootstrap {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(DubboSpringCloudWebProviderBootstrap.class)
|
||||
.properties("spring.profiles.active=nacos").run(args);
|
||||
}
|
||||
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
/*
|
||||
* 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.dubbo.service;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.dubbo.config.annotation.DubboService;
|
||||
|
||||
/**
|
||||
* In-Memory {@link UserService} implementation.
|
||||
*/
|
||||
@DubboService(protocol = "dubbo")
|
||||
public class InMemoryUserService implements UserService {
|
||||
|
||||
private Map<Long, User> usersRepository = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public boolean save(User user) {
|
||||
return usersRepository.put(user.getId(), user) == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Long userId) {
|
||||
return usersRepository.remove(userId) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<User> findAll() {
|
||||
return usersRepository.values();
|
||||
}
|
||||
|
||||
}
|
@ -1,110 +0,0 @@
|
||||
/*
|
||||
* 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.dubbo.service;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.dubbo.config.annotation.DubboService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import static com.alibaba.cloud.dubbo.util.LoggerUtils.log;
|
||||
|
||||
/**
|
||||
* Spring MVC {@link RestService}.
|
||||
*
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
@DubboService(version = "1.0.0")
|
||||
@RestController
|
||||
public class SpringRestService implements RestService {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@Override
|
||||
@GetMapping("/param")
|
||||
public String param(@RequestParam String param) {
|
||||
log("/param", param);
|
||||
return param;
|
||||
}
|
||||
|
||||
@Override
|
||||
@PostMapping("/params")
|
||||
public String params(@RequestParam int a, @RequestParam String b) {
|
||||
log("/params", a + b);
|
||||
return a + b;
|
||||
}
|
||||
|
||||
@Override
|
||||
@GetMapping("/headers")
|
||||
public String headers(@RequestHeader("h") String header,
|
||||
@RequestHeader("h2") String header2, @RequestParam("v") Integer param) {
|
||||
String result = header + " , " + header2 + " , " + param;
|
||||
log("/headers", result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@GetMapping("/path-variables/{p1}/{p2}")
|
||||
public String pathVariables(@PathVariable("p1") String path1,
|
||||
@PathVariable("p2") String path2, @RequestParam("v") String param) {
|
||||
String result = path1 + " , " + path2 + " , " + param;
|
||||
log("/path-variables", result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@PostMapping("/form")
|
||||
public String form(@RequestParam("f") String form) {
|
||||
return String.valueOf(form);
|
||||
}
|
||||
|
||||
@Override
|
||||
@PostMapping(value = "/request/body/map",
|
||||
produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
public User requestBodyMap(@RequestBody Map<String, Object> data,
|
||||
@RequestParam("param") String param) {
|
||||
User user = new User();
|
||||
user.setId(((Integer) data.get("id")).longValue());
|
||||
user.setName((String) data.get("name"));
|
||||
user.setAge((Integer) data.get("age"));
|
||||
log("/request/body/map", param);
|
||||
return user;
|
||||
}
|
||||
|
||||
@PostMapping(value = "/request/body/user",
|
||||
consumes = MediaType.APPLICATION_JSON_VALUE)
|
||||
@Override
|
||||
public Map<String, Object> requestBodyUser(@RequestBody User user) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("id", user.getId());
|
||||
map.put("name", user.getName());
|
||||
map.put("age", user.getAge());
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
dubbo:
|
||||
scan:
|
||||
base-packages: com.alibaba.cloud.dubbo.service
|
||||
protocols:
|
||||
dubbo:
|
||||
port: -1
|
||||
|
||||
feign:
|
||||
hystrix:
|
||||
enabled: true
|
||||
|
||||
server:
|
||||
port: 8080
|
||||
|
||||
management:
|
||||
endpoints:
|
||||
web:
|
||||
exposure:
|
||||
include: dubborestmetadata
|
@ -1,66 +0,0 @@
|
||||
spring:
|
||||
application:
|
||||
name: spring-cloud-alibaba-dubbo-provider
|
||||
main:
|
||||
allow-bean-definition-overriding: true
|
||||
|
||||
|
||||
# default disable all
|
||||
cloud:
|
||||
nacos:
|
||||
username: nacos
|
||||
password: nacos
|
||||
discovery:
|
||||
enabled: false
|
||||
register-enabled: false
|
||||
zookeeper:
|
||||
enabled: false
|
||||
consul:
|
||||
enabled: false
|
||||
|
||||
eureka:
|
||||
client:
|
||||
enabled: false
|
||||
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: nacos
|
||||
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
enabled: true
|
||||
register-enabled: true
|
||||
server-addr: 127.0.0.1:8848
|
||||
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: eureka
|
||||
|
||||
eureka:
|
||||
client:
|
||||
enabled: true
|
||||
service-url:
|
||||
defaultZone: http://127.0.0.1:8761/eureka/
|
||||
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: zookeeper
|
||||
cloud:
|
||||
zookeeper:
|
||||
enabled: true
|
||||
connect-string: 127.0.0.1:2181
|
||||
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: consul
|
||||
|
||||
cloud:
|
||||
consul:
|
||||
enabled: true
|
||||
host: 127.0.0.1
|
||||
port: 8500
|
@ -1,32 +0,0 @@
|
||||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dubbo-examples</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-dubbo-sample-api</artifactId>
|
||||
<name>Spring Cloud Dubbo Sample API</name>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.7.25</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Dubbo -->
|
||||
<dependency>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>dubbo</artifactId>
|
||||
<version>${dubbo.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
@ -1,26 +0,0 @@
|
||||
/*
|
||||
* 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.dubbo.service;
|
||||
|
||||
/**
|
||||
* Echo Service.
|
||||
*/
|
||||
public interface EchoService {
|
||||
|
||||
String echo(String message);
|
||||
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
/*
|
||||
* 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.dubbo.service;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* User Entity.
|
||||
*
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
public class User implements Serializable {
|
||||
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
private Integer age;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}';
|
||||
}
|
||||
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
/*
|
||||
* 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.dubbo.service;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* {@link User} Service.
|
||||
*
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
public interface UserService {
|
||||
|
||||
boolean save(User user);
|
||||
|
||||
boolean remove(Long userId);
|
||||
|
||||
Collection<User> findAll();
|
||||
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
/*
|
||||
* 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.dubbo.util;
|
||||
|
||||
import org.apache.dubbo.rpc.RpcContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Logger Utilities.
|
||||
*/
|
||||
public abstract class LoggerUtils {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(LoggerUtils.class);
|
||||
|
||||
public static void log(String url, Object result) {
|
||||
String message = String
|
||||
.format("The client[%s] uses '%s' protocol to call %s : %s",
|
||||
RpcContext.getContext().getRemoteHostName(),
|
||||
RpcContext.getContext().getUrl() == null ? "N/A"
|
||||
: RpcContext.getContext().getUrl().getProtocol(),
|
||||
url, result);
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info(message);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dubbo-examples</artifactId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-dubbo-server-sample</artifactId>
|
||||
<name>Spring Cloud Dubbo Server Sample</name>
|
||||
<version>${revision}</version>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- Sample API -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-dubbo-sample-api</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Boot dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Dubbo Spring Cloud Starter -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-dubbo</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Cloud Nacos Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -1,49 +0,0 @@
|
||||
/*
|
||||
* 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.dubbo.bootstrap;
|
||||
|
||||
import com.alibaba.cloud.dubbo.service.EchoService;
|
||||
import org.apache.dubbo.config.annotation.DubboService;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
/**
|
||||
* Dubbo Spring Cloud Server Bootstrap.
|
||||
*
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
@EnableDiscoveryClient
|
||||
@EnableAutoConfiguration
|
||||
public class DubboSpringCloudServerBootstrap {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DubboSpringCloudServerBootstrap.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@DubboService
|
||||
class EchoServiceImpl implements EchoService {
|
||||
|
||||
@Override
|
||||
public String echo(String message) {
|
||||
return "[echo] Hello, " + message;
|
||||
}
|
||||
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
dubbo:
|
||||
cloud:
|
||||
subscribed-services: ${spring.application.name}
|
||||
scan:
|
||||
base-packages: com.alibaba.cloud.dubbo.bootstrap
|
||||
protocol:
|
||||
name: dubbo
|
||||
port: -1
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: spring-cloud-alibaba-dubbo-server
|
||||
main:
|
||||
allow-bean-definition-overriding: true
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: 127.0.0.1:8848
|
@ -1,68 +0,0 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dubbo-examples</artifactId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-cloud-dubbo-servlet-gateway-sample</artifactId>
|
||||
<name>Spring Cloud Dubbo Servlet Gateway Sample</name>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-commons</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Sample API -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-dubbo-sample-api</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Dubbo Spring Cloud Starter -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-dubbo</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Cloud Nacos Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -1,41 +0,0 @@
|
||||
/*
|
||||
* 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.dubbo.bootstrap;
|
||||
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.web.servlet.ServletComponentScan;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
/**
|
||||
* Dubbo Spring Cloud Servlet Gateway Bootstrap.
|
||||
*
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
@EnableDiscoveryClient
|
||||
@EnableAutoConfiguration
|
||||
@EnableFeignClients
|
||||
@ServletComponentScan(basePackages = "com.alibaba.cloud.dubbo.gateway")
|
||||
public class DubboSpringCloudServletGatewayBootstrap {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(DubboSpringCloudServletGatewayBootstrap.class)
|
||||
.properties("spring.profiles.active=nacos").run(args);
|
||||
}
|
||||
|
||||
}
|
@ -1,212 +0,0 @@
|
||||
/*
|
||||
* 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.dubbo.gateway;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletInputStream;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.alibaba.cloud.dubbo.http.MutableHttpServerRequest;
|
||||
import com.alibaba.cloud.dubbo.metadata.DubboRestServiceMetadata;
|
||||
import com.alibaba.cloud.dubbo.metadata.RequestMetadata;
|
||||
import com.alibaba.cloud.dubbo.metadata.RestMethodMetadata;
|
||||
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
|
||||
import com.alibaba.cloud.dubbo.service.DubboGenericServiceExecutionContext;
|
||||
import com.alibaba.cloud.dubbo.service.DubboGenericServiceExecutionContextFactory;
|
||||
import com.alibaba.cloud.dubbo.service.DubboGenericServiceFactory;
|
||||
import org.apache.dubbo.rpc.service.GenericException;
|
||||
import org.apache.dubbo.rpc.service.GenericService;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.util.PathMatcher;
|
||||
import org.springframework.util.StreamUtils;
|
||||
import org.springframework.web.servlet.HttpServletBean;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
|
||||
import static com.alibaba.cloud.commons.lang.StringUtils.substringAfter;
|
||||
import static com.alibaba.cloud.commons.lang.StringUtils.substringBetween;
|
||||
import static org.springframework.web.util.UriComponentsBuilder.fromUriString;
|
||||
|
||||
@WebServlet(urlPatterns = "/dsc/*")
|
||||
public class DubboGatewayServlet extends HttpServletBean {
|
||||
|
||||
private final DubboServiceMetadataRepository repository;
|
||||
|
||||
private final DubboGenericServiceFactory serviceFactory;
|
||||
|
||||
private final DubboGenericServiceExecutionContextFactory contextFactory;
|
||||
|
||||
private final PathMatcher pathMatcher = new AntPathMatcher();
|
||||
|
||||
private final Map<String, Object> dubboTranslatedAttributes = new HashMap<>();
|
||||
|
||||
public DubboGatewayServlet(DubboServiceMetadataRepository repository,
|
||||
DubboGenericServiceFactory serviceFactory,
|
||||
DubboGenericServiceExecutionContextFactory contextFactory) {
|
||||
this.repository = repository;
|
||||
this.serviceFactory = serviceFactory;
|
||||
this.contextFactory = contextFactory;
|
||||
dubboTranslatedAttributes.put("protocol", "dubbo");
|
||||
dubboTranslatedAttributes.put("cluster", "failover");
|
||||
}
|
||||
|
||||
private String resolveServiceName(HttpServletRequest request) {
|
||||
|
||||
// /g/{app-name}/{rest-path}
|
||||
String requestURI = request.getRequestURI();
|
||||
// /g/
|
||||
String servletPath = request.getServletPath();
|
||||
|
||||
String part = substringAfter(requestURI, servletPath);
|
||||
|
||||
String serviceName = substringBetween(part, "/", "/");
|
||||
|
||||
return serviceName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void service(HttpServletRequest request, HttpServletResponse response)
|
||||
throws IOException, ServletException {
|
||||
|
||||
String serviceName = resolveServiceName(request);
|
||||
|
||||
String restPath = substringAfter(request.getRequestURI(), serviceName);
|
||||
|
||||
// 初始化 serviceName 的 REST 请求元数据
|
||||
repository.initializeMetadata(serviceName);
|
||||
// 将 HttpServletRequest 转化为 RequestMetadata
|
||||
RequestMetadata clientMetadata = buildRequestMetadata(request, restPath);
|
||||
|
||||
DubboRestServiceMetadata dubboRestServiceMetadata = repository.get(serviceName,
|
||||
clientMetadata);
|
||||
|
||||
if (dubboRestServiceMetadata == null) {
|
||||
// if DubboServiceMetadata is not found, executes next
|
||||
throw new ServletException("DubboServiceMetadata can't be found!");
|
||||
}
|
||||
|
||||
RestMethodMetadata dubboRestMethodMetadata = dubboRestServiceMetadata
|
||||
.getRestMethodMetadata();
|
||||
|
||||
GenericService genericService = serviceFactory.create(dubboRestServiceMetadata,
|
||||
dubboTranslatedAttributes);
|
||||
|
||||
// TODO: Get the Request Body from HttpServletRequest
|
||||
byte[] body = getRequestBody(request);
|
||||
|
||||
MutableHttpServerRequest httpServerRequest = new MutableHttpServerRequest(
|
||||
new HttpRequestAdapter(request), body);
|
||||
|
||||
DubboGenericServiceExecutionContext context = contextFactory
|
||||
.create(dubboRestMethodMetadata, httpServerRequest);
|
||||
|
||||
Object result = null;
|
||||
GenericException exception = null;
|
||||
|
||||
try {
|
||||
result = genericService.$invoke(context.getMethodName(),
|
||||
context.getParameterTypes(), context.getParameters());
|
||||
}
|
||||
catch (GenericException e) {
|
||||
exception = e;
|
||||
}
|
||||
response.getWriter().println(result);
|
||||
}
|
||||
|
||||
private byte[] getRequestBody(HttpServletRequest request) throws IOException {
|
||||
ServletInputStream inputStream = request.getInputStream();
|
||||
return StreamUtils.copyToByteArray(inputStream);
|
||||
}
|
||||
|
||||
private RequestMetadata buildRequestMetadata(HttpServletRequest request,
|
||||
String restPath) {
|
||||
UriComponents uriComponents = fromUriString(request.getRequestURI()).build(true);
|
||||
RequestMetadata requestMetadata = new RequestMetadata();
|
||||
requestMetadata.setPath(restPath);
|
||||
requestMetadata.setMethod(request.getMethod());
|
||||
requestMetadata.setParams(getParams(request));
|
||||
requestMetadata.setHeaders(getHeaders(request));
|
||||
return requestMetadata;
|
||||
}
|
||||
|
||||
private Map<String, List<String>> getHeaders(HttpServletRequest request) {
|
||||
Map<String, List<String>> map = new LinkedHashMap<>();
|
||||
Enumeration<String> headerNames = request.getHeaderNames();
|
||||
while (headerNames.hasMoreElements()) {
|
||||
String headerName = headerNames.nextElement();
|
||||
Enumeration<String> headerValues = request.getHeaders(headerName);
|
||||
map.put(headerName, Collections.list(headerValues));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
private Map<String, List<String>> getParams(HttpServletRequest request) {
|
||||
Map<String, List<String>> map = new LinkedHashMap<>();
|
||||
for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
|
||||
map.put(entry.getKey(), Arrays.asList(entry.getValue()));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
private final static class HttpRequestAdapter implements HttpRequest {
|
||||
|
||||
private final HttpServletRequest request;
|
||||
|
||||
private HttpRequestAdapter(HttpServletRequest request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMethodValue() {
|
||||
return request.getMethod();
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getURI() {
|
||||
try {
|
||||
return new URI(request.getRequestURL().toString() + "?"
|
||||
+ request.getQueryString());
|
||||
}
|
||||
catch (URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
return new HttpHeaders();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
dubbo:
|
||||
registry:
|
||||
# The Spring Cloud Dubbo's registry extension
|
||||
address: spring-cloud://localhost
|
||||
# The traditional Dubbo's registry
|
||||
# address: zookeeper://127.0.0.1:2181
|
||||
server:
|
||||
port: 0
|
||||
|
||||
provider:
|
||||
application:
|
||||
name: spring-cloud-alibaba-dubbo-web-provider
|
@ -1,70 +0,0 @@
|
||||
spring:
|
||||
application:
|
||||
name: spring-cloud-alibaba-dubbo-servlet-gateway
|
||||
main:
|
||||
allow-bean-definition-overriding: true
|
||||
|
||||
|
||||
# default disable all
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
enabled: false
|
||||
register-enabled: false
|
||||
zookeeper:
|
||||
enabled: false
|
||||
consul:
|
||||
enabled: false
|
||||
|
||||
eureka:
|
||||
client:
|
||||
enabled: false
|
||||
|
||||
ribbon:
|
||||
nacos:
|
||||
enabled: false
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: nacos
|
||||
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
enabled: true
|
||||
register-enabled: true
|
||||
server-addr: 127.0.0.1:8848
|
||||
|
||||
ribbon:
|
||||
nacos:
|
||||
enabled: true
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: eureka
|
||||
|
||||
eureka:
|
||||
client:
|
||||
enabled: true
|
||||
service-url:
|
||||
defaultZone: http://127.0.0.1:8761/eureka/
|
||||
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: zookeeper
|
||||
cloud:
|
||||
zookeeper:
|
||||
enabled: true
|
||||
connect-string: 127.0.0.1:2181
|
||||
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: consul
|
||||
|
||||
cloud:
|
||||
consul:
|
||||
enabled: true
|
||||
host: 127.0.0.1
|
||||
port: 8500
|
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2013-2022 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.nacos.configdata;
|
||||
|
||||
/**
|
||||
* Config preference.
|
||||
* <p>
|
||||
* When configured profile specific configuration, local config will override the remote,
|
||||
* because the local config is <strong>profile specific</strong>, it has higher priority.
|
||||
* <p>
|
||||
* So give remote config a chance to "win", we treat remote config as profile specific, it
|
||||
* should be included after profile specific sibling imports. Finally, it will override
|
||||
* the local profile specific config.
|
||||
*
|
||||
* @author freeman
|
||||
* @since 2021.0.1.1
|
||||
*/
|
||||
public enum ConfigPreference {
|
||||
/**
|
||||
* Prefer local configuration.
|
||||
*/
|
||||
LOCAL,
|
||||
/**
|
||||
* Prefer remote configuration.
|
||||
*/
|
||||
REMOTE
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue