All Projects → sofastack-guides → kc-sofastack-demo

sofastack-guides / kc-sofastack-demo

Licence: Apache-2.0 license
SOFAStack Demo for SOFAStack Cloud Native Workshop on KubeCon China 2019

Programming Languages

java
68154 projects - #9 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to kc-sofastack-demo

Kc Sofastack Dynamic Demo
SofaStack Dynamic Module Demo
Stars: ✭ 15 (-59.46%)
Mutual labels:  workshop, sofastack
react-workshop
Practical Introduction to React (with Redux & React Router v4)
Stars: ✭ 17 (-54.05%)
Mutual labels:  workshop
hacking-games-with-verbs
Whether it is running or collecting, shooting or trading, games are driven by verbs. In this workshop we pick out the verbs that make up a game and explore how these verbs express certain messages and values. Then we hack the game with new verbs, so that it communicates our own messages and values.
Stars: ✭ 16 (-56.76%)
Mutual labels:  workshop
workshop-sustainable-productivity
Workshop: the basics of stress, productivity, flow, focus, meditation and sustainability.
Stars: ✭ 20 (-45.95%)
Mutual labels:  workshop
opentelemetry-js-api
OpenTelemetry Javascript API
Stars: ✭ 75 (+102.7%)
Mutual labels:  cloud-native
pwa-workshop
Workshop: Turning a regular website into a Progressive Web Application
Stars: ✭ 41 (+10.81%)
Mutual labels:  workshop
pandas twitter
Analyzing Trump's tweets using Python (Pandas + Twitter workshop)
Stars: ✭ 81 (+118.92%)
Mutual labels:  workshop
opssight-connector
OpsSight Connector documentation
Stars: ✭ 15 (-59.46%)
Mutual labels:  cloud-native
newtonjs-graph
Cloud Architecture Graphs for Humans
Stars: ✭ 85 (+129.73%)
Mutual labels:  cloud-native
dapr-workshop
Workshop that teaches how to apply Dapr to an existing .NET, Java or Python based microservices application.
Stars: ✭ 118 (+218.92%)
Mutual labels:  workshop
cloud-native-pipelines
Cloud-native pipelines leveraging Concourse, Pivotal Build Service and Spinnaker to deploy apps
Stars: ✭ 15 (-59.46%)
Mutual labels:  cloud-native
Annif-tutorial
Instructions, exercises and example data sets for Annif hands-on tutorial
Stars: ✭ 21 (-43.24%)
Mutual labels:  workshop
bsidessf-2020-workshop
Materials for a live workshop at BSidesSF on deployment-level Kubernetes security controls
Stars: ✭ 38 (+2.7%)
Mutual labels:  workshop
streams-workshop
A workshop on Node.js Streams
Stars: ✭ 176 (+375.68%)
Mutual labels:  workshop
apisix-website
Apache APISIX Website
Stars: ✭ 81 (+118.92%)
Mutual labels:  cloud-native
workshop-edsl-in-typescript
Code template for workshop "Building eDSLs in functional TypeScript"
Stars: ✭ 49 (+32.43%)
Mutual labels:  workshop
insightedge
InsightEdge Core
Stars: ✭ 22 (-40.54%)
Mutual labels:  cloud-native
kubernetes-reading-notes
Kubernetes源码阅读笔记
Stars: ✭ 96 (+159.46%)
Mutual labels:  cloud-native
CSSclasses
The homepage for CSSclasses, a free CSS workshop series
Stars: ✭ 19 (-48.65%)
Mutual labels:  workshop
ocibuilder
A tool to build OCI compliant images
Stars: ✭ 63 (+70.27%)
Mutual labels:  cloud-native

使用 SOFAStack 快速构建微服务

前置条件

注意:您需要自行部署后端环境依赖,并修改示例中的服务依赖地址即可使用。

实验内容

本实验基于 SOFAStack 快速构建一个微服务,主要包括以下几个部分:

  • 使用 SOFABoot + SOFARPC 发布服务
  • 使用 SOFABoot + SOFARPC 调用服务
  • 通过 ZipKin 查看 SOFATracer 上报的 Tracer 信息
  • 通过 SOFALookout 查看上报的 Metrics 信息

架构图

pic

任务

1、任务准备

从 github 上将 demo 工程克隆到本地

git clone https://github.com/sofastack-guides/kc-sofastack-demo.git

然后将工程导入到 IDEA 或者 eclipse。导入之后界面如下:

pic

  • balance-mng:余额管理系统,提供扣减余额服务
  • stock-mng:库存管理系统,提供扣减库存服务

2、引入依赖

将下面的依赖引入到 balance-mng 和 stock-mng 工程模块的 pom.xml 文件中。

<!--SOFARPC 依赖-->
<dependency>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>rpc-sofa-boot-starter</artifactId>
</dependency>
<!--SOFATracer 依赖-->
<dependency>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>tracer-sofa-boot-starter</artifactId>
</dependency>
<!--SOFARegistry 依赖-->
<dependency>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>registry-client-all</artifactId>
</dependency>
<!--runtime 依赖-->
<dependency>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>runtime-sofa-boot-starter</artifactId>
</dependency>
<!--SOFALookout 依赖-->
<dependency>
    <groupId>com.alipay.sofa.lookout</groupId>
    <artifactId>lookout-sofa-boot-starter</artifactId>
</dependency>

balance-mng 工程需要将依赖引入 balance-mng/balance-mng-impl/pom.xml 文件:

pic

stock-mng 工程直接将依赖引入 stock-mng/pom.xml 文件:

pic

3、添加配置

将如下配置复制到 balance-mng 和 stock-mng 工程模块的 application.properties 中。

# 1、添加服务注册中心地址
com.alipay.sofa.rpc.registry.address=sofa://localhost:9603
# 2、添加 tracer 数据上报的服务端 zipkin 地址
# 如果上面前置条件未搭建 tracer,可以不配置
com.alipay.sofa.tracer.zipkin.base-url=http://localhost:9411
# 3、添加 metrics 数据上报的服务端地址
# 如果上面前置条件未搭建 lookout-server,可以不配置
com.alipay.sofa.lookout.agent-host-address=localhost

balance-mng 工程需要将配置添加至 balance-mng/balance-mng-bootstrap/src/main/resources/application.properties 文件:

pic

另外数据库配置修改为自己的数据库信息:

# database config
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/stock_db
spring.datasource.username=root
spring.datasource.password=root

stock-mng 工程需要将配置添加至 stock-mng/src/main/resources/application.properties 文件:

pic

另外数据库配置修改为自己的数据库信息:

# database config
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/stock_db
spring.datasource.username=root
spring.datasource.password=root

4、修改 unique id

由于所有人共用一套服务发现,为区分不同用户发布的服务,需要为服务增加 unique id。

KubeCon workshop 会给每个用户准备一个 SOFAStack 账号,格式为 [email protected][email protected],去掉 @sofastack.io 部分,账户前半部分的 user0 至 user99 即可作为 unique id。

注意:balance-mng 和 stock-mng 里的 unique id 需要一致。

balance-mng 工程需要在 balance-mng/balance-mng-bootstrap/src/main/resources/application.properties 文件修改:

pic

stock-mng 工程需要在 stock-mng/src/main/resources/application.properties 文件修改:

pic

5、发布 SOFARPC 服务

在 BalanceMngImpl 类上加上 @SofaService 注解 和 @Service 注解,将其发布成一个 SOFARPC 服务:

import org.springframework.stereotype.Service;
import com.alipay.sofa.runtime.api.annotation.SofaService;
import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;

@Service
@SofaService(interfaceType = BalanceMngFacade.class, uniqueId = "${service.unique.id}", bindings = { @SofaServiceBinding(bindingType = "bolt") })

增加之后的 BalanceMngImpl 类如下图所示:

pic

在 StockMngImpl 类上加上 @SofaService 注解 和 @Service 注解,将其发布成一个 SOFARPC 服务:

import org.springframework.stereotype.Service;
import com.alipay.sofa.runtime.api.annotation.SofaService;
import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;

@Service
@SofaService(interfaceType = StockMngFacade.class, uniqueId = "${service.unique.id}", bindings = { @SofaServiceBinding(bindingType = "bolt") })

增加之后的 StockMngImpl 类如下图所示:

pic

6、引用 SOFARPC 服务

在 BookStoreControllerImpl 类中的 stockMngFacade 变量上方加 @SofaReference 注解,用于引用 SOFARPC 服务:

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;

@SofaReference(interfaceType = StockMngFacade.class, uniqueId = "${service.unique.id}", binding = @SofaReferenceBinding(bindingType = "bolt"))

在 BookStoreControllerImpl 类中的 balanceMngFacade 变量上方加 @SofaReference 注解,用于引用 SOFARPC 服务:

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;

@SofaReference(interfaceType = BalanceMngFacade.class, uniqueId = "${service.unique.id}", binding = @SofaReferenceBinding(bindingType = "bolt"))

增加之后的 BookStoreControllerImpl 类如下图所示:

pic

7、实验验证

运行 BalanceMngApplication 和 StockMngApplication 即可启动应用。应用启动之后,通过浏览器访问:http://localhost:8080 即可正常操作页面:

pic

浏览器访问 http://localhost:9411,查看链路数据上报以链路关系图:

pic

浏览器访问 http://localhost:9090 即可查看上报 metrics:

pic

  • jvm.threads.totalStarted{app="stock_mng"}:可以查看 JVM 启动线程数
  • rpc.consumer.service.stats.total_count.count{app="stock_mng"}:可以查看 stock_mng 应用的调用次数

关于 SOFALookout 的更多用法,请参考: https://www.sofastack.tech/sofa-lookout/docs/Home

更多

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].