一个Apache ServiceComb 是一个微服务的开源解决方案。其包含多个组件,通过组件之间的搭配,可以灵活的应对不同的场景。本指南可以帮助你快速的使用 Apache ServiceComb,针对初次使用的用户,这是开始尝试的最佳入口。
官网:http://servicecomb.apache.org/cn/
入门:http://servicecomb.apache.org/cn/docs/getting-started/
1:点击:

2:找到

开发环境准备
jdk maven idea
服务注册中心cse:Cloud Service Engine
好处: 可以轻松将微服务 发布到云平台 华为云pass平台
用ServiceComb 开发微服务 在借用华为云 pass平台 实现微服治理
1:服务提供着 -------提供微服务
2:服务注册中心 --------注册微服务 --管理微服务
3:服务消费者 --------调用微服务
================
1L服务提供着向 服务注册中心 注册一个 服务
2:服务消费者可以向cse注册中心服务发现
3:如果咋cse中心 找到了 要调用的微服务 就可以发送一个微服务的实例给服务消费者
4;心跳 :通过心跳的消息 实现微服务状态检查
========================
开始
1:下载到本地后,解压(不要解压到中文目录下),目录结构如下:

建议每次都启动这两个程序。前台访问地址:http://127.0.0.1:30103,后台地址是:http://127.0.0.1:30100
启动本地服务中心后 在服务提供者/消费者 的 microservice.yaml文件中配置servicecenter地址和 端口
例如:
servicecomb
service:
registry:
address:
http://127.0.0.1:30100
===========================搭建快速开发脚手架入门
1:地址:http://start.servicecomb.io/

2:下载完demo 解压 通过idea打开
===============入门程序分析
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.12.RELEASE</version>
<relativePath/>
</parent>


===============运行报错
解决:No goals have been specified for this build. You must specify a valid lifecycle phase or a goal
pom.xml 文件中 <build> 标签要加配置: <defaultGoal>compile</defaultGoal>
===============运行报错
先开启servicecomb 微服务注册中心
运行DemoApplication
打开浏览器 输入; http://127.0.0.1:9080/hello

打开浏览器: http://127.0.0.1:30103/#!/sc/services/
可以看到微服务 已经添加注册成功

=========================框架搭建
1:打开ideA创建一个mavn工程

2:在pop 中添加坐标信息
===============================================
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.12.RELEASE</version>
<relativePath/>
</parent>
-----------------------------------------------------------
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>java-chassis-dependencies </artifactId>
<version>1.2.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
---------------------------------------------
<build>
<defaultGoal>compile</defaultGoal>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
===============================================
2:添加一个module -------》service-interface

2.1 创建一个类 ---- 微服务接口定义类

==========================
3"添加一个module 服务提供者-------》service-provider

.3.1:在resuource 目录下: 添加 microservice.yaml 文件

3.2:添加坐标信息

=============================
4 添加服务消费者 service-customer

4.1: 重复3.1 和3.2 的步骤
===================
5:编写 服务提供者
5.1:将服务提供者 安装到 本地仓库
5.2:将 代码注释 (项目的 pop 文件中)

5.3: 运行 install 运行完 将5.1 的注释 放开

5.4: 在service-provider 添加对 service-interface的依赖

5.5:在service-provider 中创建一个接口 实现类
=================================================
@RestSchema(schemaId ="hello")// 注册微服务到注册中心
@RequestMapping("/hello")
public class RestServiceImplimplements RestService {
@Override
@GetMapping("hello")
public StringsayRest(String name) {
return "hello lww"+name;
}
}
5.6创建 服务提供者 启动类
@SpringBootApplication
@EnableServiceComb 向注册中心注册
public class ProvideApplication {
public static void main(String[] args) {
SpringApplication.run(ProvideApplication.class,args);
}
}
5.7:测试服务提供者
5.7.1:启动servicecomb的服务 frontend.exe 和 service-center.exe
运行 ProvideApplication
在浏览器 打开 http://localhost:9080/hello/hello

=================================================
6: 编写 消费者 service-custom
:6.1 pop文件 添加

6.2:创建一个实现类
/**
* 消费者的服务实现类
*/
@Service
public class RestCustomImplimplements RestService {
// restTemplate 模板
private final RestTemplaterestTemplate= RestTemplateBuilder.create();
@Override
public StringsayRest(String name) {
String providename="provider";
String forObject =restTemplate.getForObject("cse:" + providename +"/hello/hello", String.class);
return forObject;
}
}
---------------------------------------------
6.3 创建消费者的 controller
@RestSchema(schemaId ="test")
@RequestMapping("/test")
public class RestCustomController {
@Autowired
RestServicerestService;
@GetMapping("/test")
public StringsayRest(String name) {
return restService.sayRest(name);
}
}
6.4:配置yaml

6.5:创建消费者启动类
@SpringBootApplication
@EnableServiceComb
public class ConsumeApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumeApplication.class,args);
}
}
--------------------------------------------------
测试: 运行 服务提供者 和 消费者
在浏览器输入; http://localhost:9081/test/test

网友评论