-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
增加分别基于springboot-v2和spring-v3的spring-cloud样例 #48
- Loading branch information
Showing
42 changed files
with
1,070 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
tmp/ | ||
.vscode/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
# r-nacos spring-cloud-v2 说明 | ||
|
||
本样例基于spring-boot:2.7.18和spring-cloud:2021.0.9,支持jdk1.8版本。 | ||
|
||
|
||
|
||
## 使用方式 | ||
|
||
1. 启动r-nacos | ||
2. 切换到spring-cloud-v2样例目录 | ||
3. 本地打包 `mvn package` (如果有问题可以用 `mvn install` ) | ||
4. 运行service: `java -jar demo-service/target/demo-service-1.0-SNAPSHOT.jar` | ||
5. 运行api: `java -jar demo-api/target/demo-api-1.0-SNAPSHOT.jar` | ||
6. 访问api: `curl "http://127.0.0.1:20895/hi?name=r-nacos"` | ||
|
||
```sh | ||
curl "http://127.0.0.1:20895/hi?name=r-nacos" | ||
|
||
[springcloud-demo-service]: Hello, r-nacos | ||
``` | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
target/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<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"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.rnacos.demo</groupId> | ||
<artifactId>springcloud-demo-parent</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>demo-api</artifactId> | ||
<name>demo-api</name> | ||
<packaging>jar</packaging> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.rnacos.demo</groupId> | ||
<artifactId>demo-common</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<!-- 添加 Nacos 支持 --> | ||
<dependency> | ||
<groupId>com.alibaba.cloud</groupId> | ||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-openfeign</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-loadbalancer</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.junit.vintage</groupId> | ||
<artifactId>junit-vintage-engine</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.1</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
<encoding>UTF-8</encoding> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
<version>${spring-boot.version}</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>repackage</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
16 changes: 16 additions & 0 deletions
16
sdk-examples/java/spring-cloud-v2/demo-api/src/main/java/com/rnacos/demo/App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.rnacos.demo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.cloud.openfeign.EnableFeignClients; | ||
|
||
/** | ||
* | ||
*/ | ||
@SpringBootApplication | ||
@EnableFeignClients // 启用 OpenFeign | ||
public class App { | ||
public static void main(String[] args) { | ||
SpringApplication.run(App.class, args); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...java/spring-cloud-v2/demo-api/src/main/java/com/rnacos/demo/client/DemoServiceClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.rnacos.demo.client; | ||
|
||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
|
||
import com.rnacos.demo.Result; | ||
import com.rnacos.demo.foo.SayHelloRequest; | ||
import com.rnacos.demo.foo.SayHelloResponse; | ||
|
||
@FeignClient("springcloud-demo-service") | ||
public interface DemoServiceClient { | ||
|
||
@RequestMapping(value = "/foo/sayHello", method = RequestMethod.POST) | ||
Result<SayHelloResponse> sayHello(@RequestBody SayHelloRequest request); | ||
} |
41 changes: 41 additions & 0 deletions
41
...es/java/spring-cloud-v2/demo-api/src/main/java/com/rnacos/demo/foo/api/FooController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.rnacos.demo.foo.api; | ||
|
||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.beans.factory.annotation.Value; | ||
|
||
import com.rnacos.demo.Result; | ||
import com.rnacos.demo.client.DemoServiceClient; | ||
import com.rnacos.demo.foo.SayHelloRequest; | ||
import com.rnacos.demo.foo.SayHelloResponse; | ||
|
||
import javax.annotation.Resource; | ||
|
||
@Component | ||
@RestController | ||
public class FooController { | ||
@Value("${spring.application.name}") | ||
private String serviceName; | ||
|
||
@Resource | ||
private DemoServiceClient demoServiceClient; | ||
|
||
@GetMapping("/hi") | ||
public String hello(@RequestParam("name") String name) { | ||
if (name == null) { | ||
name = "default"; | ||
} | ||
SayHelloRequest request = new SayHelloRequest(); | ||
request.setName(name); | ||
request.setFromAppName(serviceName); | ||
Result<SayHelloResponse> sayHelloResult = demoServiceClient.sayHello(request); | ||
if (!sayHelloResult.isSuccess()) { | ||
return String.format("[ERROR]: %s", sayHelloResult.getMsg()); | ||
} | ||
SayHelloResponse sayHelloResponse = sayHelloResult.getData(); | ||
return String.format("[%s]: %s", sayHelloResponse.getResponseFromAppName(), | ||
sayHelloResponse.getResponseValue()); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
sdk-examples/java/spring-cloud-v2/demo-api/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
server: | ||
port: 20895 | ||
spring: | ||
application: | ||
name: springcloud-demo-api | ||
cloud: | ||
nacos: | ||
discovery: | ||
username: nacos # nacos 登录用户名 | ||
password: nacos # 密码 | ||
server-addr: 127.0.0.1:8848 # nacos 服务端地址 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
target/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<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"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.rnacos.demo</groupId> | ||
<artifactId>springcloud-demo-parent</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>demo-common</artifactId> | ||
<name>demo-common</name> | ||
<packaging>jar</packaging> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>3.8.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
</build> | ||
</project> |
54 changes: 54 additions & 0 deletions
54
sdk-examples/java/spring-cloud-v2/demo-common/src/main/java/com/rnacos/demo/Result.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.rnacos.demo; | ||
|
||
import java.io.Serializable; | ||
|
||
public class Result<T> implements Serializable { | ||
private boolean success; | ||
private T data; | ||
private String msg; | ||
|
||
public Result() { | ||
|
||
} | ||
|
||
public Result(T data) { | ||
this.success = true; | ||
this.data = data; | ||
} | ||
|
||
public static <T> Result<T> of(T data) { | ||
return new Result<>(data); | ||
} | ||
|
||
public static <T> Result<T> error(String msg) { | ||
Result<T> result = new Result<>(); | ||
result.success = false; | ||
result.msg = msg; | ||
return result; | ||
} | ||
|
||
public boolean isSuccess() { | ||
return success; | ||
} | ||
|
||
public void setSuccess(boolean success) { | ||
this.success = success; | ||
} | ||
|
||
public String getMsg() { | ||
return msg; | ||
} | ||
|
||
public void setMsg(String msg) { | ||
this.msg = msg; | ||
} | ||
|
||
public T getData() { | ||
return data; | ||
} | ||
|
||
public void setData(T data) { | ||
this.data = data; | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
...mples/java/spring-cloud-v2/demo-common/src/main/java/com/rnacos/demo/foo/DemoService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.rnacos.demo.foo; | ||
|
||
public interface DemoService { | ||
String sayHello(String name); | ||
} |
24 changes: 24 additions & 0 deletions
24
...s/java/spring-cloud-v2/demo-common/src/main/java/com/rnacos/demo/foo/SayHelloRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.rnacos.demo.foo; | ||
|
||
import java.io.Serializable; | ||
|
||
public class SayHelloRequest implements Serializable { | ||
private String fromAppName; | ||
private String name; | ||
|
||
public String getFromAppName() { | ||
return fromAppName; | ||
} | ||
|
||
public void setFromAppName(String fromAppName) { | ||
this.fromAppName = fromAppName; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
.../java/spring-cloud-v2/demo-common/src/main/java/com/rnacos/demo/foo/SayHelloResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.rnacos.demo.foo; | ||
|
||
import java.io.Serializable; | ||
|
||
public class SayHelloResponse implements Serializable { | ||
private String responseValue; | ||
private String responseFromAppName; | ||
|
||
public String getResponseValue() { | ||
return responseValue; | ||
} | ||
|
||
public void setResponseValue(String responseValue) { | ||
this.responseValue = responseValue; | ||
} | ||
|
||
public String getResponseFromAppName() { | ||
return responseFromAppName; | ||
} | ||
|
||
public void setResponseFromAppName(String responseFromAppName) { | ||
this.responseFromAppName = responseFromAppName; | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...examples/java/spring-cloud-v2/demo-common/src/main/java/com/rnacos/demo/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package com.rnacos.demo; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
target/ | ||
|
Oops, something went wrong.