Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

添加 nacos sample #393

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
| dubbo | dubbo + grpc | 模块使用 grpc | [samples/dubbo-samples/rpc/grpc](https://github.com/sofastack/sofa-serverless/tree/master/samples/dubbo-samples/rpc/grpc) |
| sofaboot | sofarpc/tomcat | 基座调用模块、中台模式 | [samples/sofaboot-samples/dynamic-stock](https://github.com/sofastack/sofa-serverless/tree/master/samples/sofaboot-samples/dynamic-stock) |
| springboot3 | springboot3 | springboot3 | [samples/sofaboot3-samples](https://github.com/sofastack/sofa-serverless/tree/master/samples/sofaboot3-samples) | |
### 注意请在编译器单独导入对应 samples 工程,例如 sofa-serverless/samples/springboot-samples 工程,否则会出现 BizRuntieContext Not found 的报错。
### 注意请在编译器单独导入对应 samples 工程,例如 sofa-serverless/samples/springboot-samples 工程,否则会出现 BizRuntimeContext Not found 的报错。
![biz runtime context not found](bizruntimecontext_not_found.png)
原因:samples 工程与 sofa-serverless-runtime 在一个工程目录里,会优先使用本地的 sofa-serverless-runtime,而不是 maven 依赖的 sofa-serverless-runtime,导致找不到 BizRuntimeContext 类。
41 changes: 41 additions & 0 deletions samples/springboot-samples/config/nacos/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 实验内容


1. 不同模块注册并读取不同的 dataId
2. 不同模块使用相同 dataId

## 实验任务

### 前置准备
nacos采用标准的 CS 分布式架构, 在实验前需要先将 server 端启动。
启动步骤参考: [Nacos 快速开始](https://nacos.io/zh-cn/docs/quick-start.html)

下面我们将启动一个基座 base 和两个模块 biz1 和 biz2, 模块与其对应的 dataId 映射如下
- base模块: base
- biz1模块: biz
- biz2模块: biz

> 由于 nacos 依赖 prometheus 做指标采集, 由于其内部机制, io.prometheus:simpleclient 该 GA 不能做包与类下沉基座, 否则模块会启动不了


### 不同模块注册并读取不同的 dataId

启动 base、biz1、biz2 模块后, 验证 dataId的读取逻辑
- base: 调用 *curl http://localhost:8090/config/get* 返回false
- biz1: 调用 *curl http://localhost:8090/biz1/config/get* 返回false
- biz2: 调用 *curl http://localhost:8090/biz2/config/get* 返回false

执行 *curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=base&group=DEFAULT_GROUP&content=useLocalCache=true"* 将 dataId base 的值修改为 true

再次调用 *curl http://localhost:8090/config/get* 发现值已经范围 true, 说明基座的 dataId已经修改成功且生效。而调用 biz 查询依旧是 false

### 不同模块使用相同 dataId

在上述基础上, 由于模块 biz1 和 biz2 都使用 "biz" 作为 dataId, 期望修改该 dataId 的值后, biz1 和 biz2 读取的值同时发生变化。

执行 *curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=biz&group=DEFAULT_GROUP&content=useLocalCache=true"* 将 dataId biz 的值修改为 true

- biz1: 调用 *curl http://localhost:8090/biz1/config/get* 返回 true
- biz2: 调用 *curl http://localhost:8090/biz2/config/get* 返回 true

发现, 共享同 dataId 的两个模块都读取到了修改的值。
77 changes: 77 additions & 0 deletions samples/springboot-samples/config/nacos/base/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.alipay.sofa.config</groupId>
<artifactId>nacos</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<artifactId>base-nacos</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.alipay.sofa.serverless</groupId>
<artifactId>sofa-serverless-base-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>web-ark-plugin</artifactId>
</dependency>

<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>5.1.0</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.alipay.sofa.config.nacos;

import com.alibaba.nacos.api.config.annotation.NacosValue;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
* @author mingmen
* @date 2023/12/11
*/
@Controller
@RequestMapping("config")
public class ConfigController {

@NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
private boolean useLocalCache;

@RequestMapping(value = "/get", method = GET)
@ResponseBody
public boolean get() {
return useLocalCache;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.alipay.sofa.config.nacos;

import java.util.List;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
* @author mingmen
* @date 2023/12/11
*/
@Controller
@RequestMapping("discovery")
public class DiscoveryController {

@NacosInjected
private NamingService namingService;

@RequestMapping(value = "/get", method = GET)
@ResponseBody
public List<Instance> get(@RequestParam String serviceName) throws NacosException {
return namingService.getAllInstances(serviceName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.alipay.sofa.config.nacos;

import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@EnableConfigurationProperties
@NacosPropertySource(dataId = "base", autoRefreshed = true)
@SpringBootApplication
public class NacosApplication {

public static void main(String[] args) {

SpringApplication.run(NacosApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
spring.application.name=base-nacos
server.port=8090

nacos.config.server-addr=127.0.0.1:8848
nacos.discovery.server-addr=127.0.0.1:8848
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.alipay.sofa.config.nacos;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class NacosApplicationTests {

@Test
void contextLoads() {
}

}
90 changes: 90 additions & 0 deletions samples/springboot-samples/config/nacos/biz1/conf/ark/rules.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
excludeArtifactIds=commons-collections
excludeArtifactIds=commons-httpclient
excludeArtifactIds=commons-io
excludeArtifactIds=commons-lang
excludeGroupIds=aopalliance*
excludeGroupIds=asm*
excludeGroupIds=cglib*
excludeGroupIds=com.alibaba.common.lang*
excludeGroupIds=com.alibaba.common.resourcebundle*
excludeGroupIds=com.alibaba.tbase*
excludeGroupIds=com.alipay*
excludeGroupIds=com.antcloud.antvip*
excludeGroupIds=com.caucho.hessian*
excludeGroupIds=com.ctc.wstx*
excludeGroupIds=com.fasterxml*
excludeGroupIds=com.google.code*
excludeGroupIds=com.google.common*
excludeGroupIds=com.google.gson*
excludeGroupIds=com.google.guava*
excludeGroupIds=com.google.j2objc*
excludeGroupIds=com.google.http-client*
excludeGroupIds=com.google.inject*
excludeGroupIds=com.google.protobuf*
excludeGroupIds=com.ibatis*
excludeGroupIds=com.iwallet.biz*
excludeGroupIds=com.lmax*
excludeGroupIds=com.taobao.config*
excludeGroupIds=com.taobao.hsf*
excludeGroupIds=com.taobao.notify*
excludeGroupIds=com.taobao.remoting*
excludeGroupIds=com.taobao.tair*
excludeGroupIds=groovy*
excludeGroupIds=io.fury*
excludeGroupIds=io.grpc*
excludeGroupIds=io.mosn.layotto*
excludeGroupIds=io.netty*
excludeGroupIds=io.openmessaging*
excludeGroupIds=javax*
excludeGroupIds=javax.el*
excludeGroupIds=javax.script*
excludeGroupIds=javax.servlet*
excludeGroupIds=javax.validation*
excludeGroupIds=loccs-bcprov*
excludeGroupIds=log4j*
excludeGroupIds=mysql*
excludeGroupIds=net.sf.acegisecurity*
excludeGroupIds=net.sf.cglib*
excludeGroupIds=netty*
excludeGroupIds=ognl*
excludeGroupIds=org.aopalliance*
excludeGroupIds=org.apache*
excludeGroupIds=org.aspectj*
excludeGroupIds=org.codehaus*
excludeGroupIds=org.codehaus.groovy*
excludeGroupIds=org.codehaus.xfire*
excludeGroupIds=org.dom4j*
excludeGroupIds=org.hibernate.validator*
excludeGroupIds=org.junit*
excludeGroupIds=org.mvel2*
excludeGroupIds=org.mybatis*
excludeGroupIds=org.mybatis.spring*
excludeGroupIds=org.mybatis.spring.boot.autoconfigure*
excludeGroupIds=org.projectlombok*
excludeGroupIds=org.quartz*
excludeGroupIds=org.reflections*
excludeGroupIds=org.slf4j*
excludeGroupIds=org.springframework*
excludeGroupIds=org.yaml*
excludeGroupIds=xerces*
excludeGroupIds=xml-apis*
excludeGroupIds=xpp3*
excludeGroupIds=jakarta*
excludeGroupIds=org.latencyutils*
excludeGroupIds=org.hdrhistogram*
excludeGroupIds=io.micrometer*
excludeGroupIds=io.micrometer*
excludeGroupIds=ch.qos.logback*
excludeGroupIds=com.squareup.okhttp3*
excludeGroupIds=com.squareup.okhttp*
excludeGroupIds=net.sf.ehcache*
excludeGroupIds=redis.clients*
excludeGroupIds=net.bytebuddy*
excludeGroupIds=com.taobao.text*
excludeGroupIds=net.java.dev.jna*
excludeGroupIds=com.google.errorprone*
excludeGroupIds=com.github.oshi*
excludeGroupIds=org.checkerframework*


excludeArtifactIds=fastjson
Loading