Skip to content

Commit

Permalink
增加分别基于springboot-v2和spring-v3的spring-cloud样例 #48
Browse files Browse the repository at this point in the history
  • Loading branch information
heqingpan committed Aug 27, 2024
1 parent f11ac65 commit 1594abe
Show file tree
Hide file tree
Showing 42 changed files with 1,070 additions and 0 deletions.
3 changes: 3 additions & 0 deletions sdk-examples/java/spring-cloud-v2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tmp/
.vscode/

24 changes: 24 additions & 0 deletions sdk-examples/java/spring-cloud-v2/README.md
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
```



2 changes: 2 additions & 0 deletions sdk-examples/java/spring-cloud-v2/demo-api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/

83 changes: 83 additions & 0 deletions sdk-examples/java/spring-cloud-v2/demo-api/pom.xml
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>
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);
}
}
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);
}
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());
}
}
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 服务端地址

2 changes: 2 additions & 0 deletions sdk-examples/java/spring-cloud-v2/demo-common/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/

30 changes: 30 additions & 0 deletions sdk-examples/java/spring-cloud-v2/demo-common/pom.xml
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>
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;
}

}
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);
}
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;
}
}
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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package com.rnacos.demo;
2 changes: 2 additions & 0 deletions sdk-examples/java/spring-cloud-v2/demo-service/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/

Loading

0 comments on commit 1594abe

Please sign in to comment.