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

Commit

Permalink
[#60]spring boot场景下不触发ContextStartedEvent,导致没有注册 (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
liubao68 authored Nov 1, 2020
1 parent 44b8e08 commit ee77eb9
Show file tree
Hide file tree
Showing 33 changed files with 1,531 additions and 16 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ jobs:
${{ runner.os }}-maven
- name: Compilation and Installation
run: bash scripts/build.sh install
- name: publish coverage report
run: bash <(curl -s https://codecov.io/bash)

63 changes: 49 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,62 @@ dubbo.servicecomb.governance: {"providerInfos":[{"serviceName":"price-provider",

* POM 中引入依赖

```yaml
<dependency>
<groupId>com.huaweicloud.dubbo-servicecomb</groupId>
<artifactId>dubbo-servicecomb-service-center</artifactId>
</dependency>
<dependency>
<groupId>com.huaweicloud.dubbo-servicecomb</groupId>
<artifactId>dubbo-servicecomb-config-center</artifactId>
</dependency>
```
<dependency>
<groupId>com.huaweicloud.dubbo-servicecomb</groupId>
<artifactId>dubbo-servicecomb-service-center</artifactId>
</dependency>
<dependency>
<groupId>com.huaweicloud.dubbo-servicecomb</groupId>
<artifactId>dubbo-servicecomb-config-center</artifactId>
</dependency>
```

上面两个部件,实现 dubbo 应用的注册和动态配置、服务治理配置项检测等功能。

* 采用 Spring 启动(目前只支持 Spring 启动方式)。
* 采用 Spring 启动
Spring 扫描路径中,需要增加 `classpath*:spring/dubbo-servicecomb.xml`, 举例如下:

```java
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"classpath*:spring/dubbo-provider.xml", "classpath*:spring/dubbo-servicecomb.xml");
```
public class PriceApplication {
public static void main(String[] args) throws Exception {
try {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"classpath*:spring/dubbo-provider.xml", "classpath*:spring/dubbo-servicecomb.xml");
context.start();
System.in.read();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
```

* 采用 Spring Boot 启动
Spring 扫描路径中,需要增加 `classpath*:spring/dubbo-servicecomb.xml`, 举例如下:

```
@SpringBootApplication
@ImportResource({"classpath*:spring/dubbo-provider.xml", "classpath*:spring/dubbo-servicecomb.xml"})
public class PriceApplication {
public static void main(String[] args) throws Exception {
try {
SpringApplication.run(PriceApplication.class);
} catch (Throwable e) {
e.printStackTrace();
}
}
}
```

如果使用 Spring Boot, 也可以采用 @ImportResource 等标签引入 dubbo-servicecomb 相关 bean。
Spring Boot 启动,需要额外依赖如下 jar 包:

```
<dependency>
<groupId>com.huaweicloud.dubbo-servicecomb</groupId>
<artifactId>dubbo-servicecomb-spring-boot</artifactId>
</dependency>
```

* 使用 service center 作为注册发现。
在 Spring 配置文件中增加如下配置。 如果配置文件已经配置了 zookeeper, 那么需要使用下面的配置项进行替换。
Expand Down
34 changes: 34 additions & 0 deletions dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,40 @@

<dependencyManagement>
<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>4.0.3</version>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.5.Final</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.24</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
<version>${jackson.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-bom</artifactId>
Expand All @@ -55,6 +82,13 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
13 changes: 13 additions & 0 deletions integration-tests/discovery-tests-spring-boot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
运行测试的步骤:

1. 安装本地微服务引擎并启动
2. 运行 PriceApplication
3. 运行 OrderApplication
4. 配置中心下发配置项:

[全局配置] dubbo.servicecomb.test.configuration: peizhi
[服务配置:price-provider] dubbo.servicecomb.test.configurationService: peizhi_service
[全局配置] dubbo.servicecomb.governance: {"providerInfos":[{"serviceName":"price-provider","schemaInfos":[{"schemaId":"com.huaweicloud.it.price.PriceService","parameters":{"timeout":5000}}]}]}

5. 运行 PortalApplication 查看测试结果, 如果成功,输出 `running all test cases successfully`

35 changes: 35 additions & 0 deletions integration-tests/discovery-tests-spring-boot/common-api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.huaweicloud.dubbo-servicecomb</groupId>
<artifactId>spring-boot-discovery-tests</artifactId>
<version>1.1.3-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>spring-boot-discovery-common-api</artifactId>

<dependencies>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.huaweicloud.it.price;

public interface PingService {
boolean ping();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.huaweicloud.it.price;

import java.util.concurrent.CompletableFuture;

public interface PriceService {
String sayHello(String name);

CompletableFuture<String> sayHelloAsync(String name);

String testConfiguration(String value);

String testConfigurationService(String value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.huaweicloud.it.price;

import java.util.concurrent.CompletableFuture;

public interface PriceServiceRest {
String sayHello(String name);

CompletableFuture<String> sayHelloAsync(String name);

String testConfiguration(String value);

String testConfigurationService(String value);
}
Loading

0 comments on commit ee77eb9

Please sign in to comment.