Skip to content

Commit

Permalink
[#136] add governance demo for cloud document (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
chengyouling authored Aug 13, 2024
1 parent 8ab0f57 commit 7572daa
Show file tree
Hide file tree
Showing 27 changed files with 656 additions and 0 deletions.
106 changes: 106 additions & 0 deletions governance/loadbalance/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Description
This project providers sample to show working with load-balance Deploy.

* provider-one
A Microserivce using Spring Cloud with a REST interface.

* provider-two
Same as provider-one, but with version 0.0.2

* provider-three
Same as provider-one, but with version 0.0.3

* consumer
A Microserivce using Spring Cloud with a REST interface. Consumer calls load-balance-provider with RestTemplate.

# Build and Run

* Prerequisites
[Setup CSE environment](../CSE-ENV.md)

* Build

mvn clean package

* Run provider

In ${Project}/provider-one/target/

java -jar provider-one-1.0-SNAPSHOT.jar

* Run provider-two

In ${Project}/provider-two/target/

java -jar provider-two-1.0-SNAPSHOT.jar

* Run provider-three

In ${Project}/provider-three/target/

java -jar provider-three-1.0-SNAPSHOT.jar

* Run consumer

In ${Project}/consumer/target/

java -jar consumer-1.0-SNAPSHOT.jar

* Testing

Open in browser: http://localhost:8090/load-balance?id=111

All returned results are sorted by version number.

# 项目说明

这个项目提供了 Spring Cloud Huawei 灰度发布的例子。

* provider-one
使用 Spring Cloud 开发一个 REST 接口。

* provider-two
load-balance-provider的0.0.2版本。

* provider-three
load-balance-provider的0.0.3版本。

* consumer
使用 Spring Cloud 开发一个 REST 接口, 接口实现通过 RestTemplate 调用 load-balance-provider 的接口。

## 使用

* 前提条件
[准备CSE运行环境](../CSE-ENV_CN.md)

* 编译

mvn clean package

* 启动 provider-one

进入目录 ${Project}/provider-one/target/

java -jar provider-one-1.0-SNAPSHOT.jar

* 启动 provider-two

进入目录 ${Project}/provider-two/target/

java -jar provider-two-1.0-SNAPSHOT.jar

* 启动 provider-three

进入目录 ${Project}/provider-three/target/

java -jar provider-three-1.0-SNAPSHOT.jar

* 启动 consumer

进入目录 ${Project}/consumer/target/

java -jar consumer-1.0-SNAPSHOT.jar

* 测试

启动4个微服务后, 然后通过界面访问: http://localhost:8090/load-balance?id=111。 返回结果按版本号有规律顺序的返回。
7 changes: 7 additions & 0 deletions governance/loadbalance/consumer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM openjdk:8u181-jdk-alpine

WORKDIR /home/apps/
ADD target/consumer-1.0-SNAPSHOT.jar .
ADD start.sh .

ENTRYPOINT ["sh", "/home/apps/start.sh"]
33 changes: 33 additions & 0 deletions governance/loadbalance/consumer/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.huaweicloud.samples</groupId>
<artifactId>loadbalance-application</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>consumer</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.huaweicloud</groupId>
<artifactId>spring-cloud-starter-huawei-service-engine</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,23 @@
package com.huaweicloud.sample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}


@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.huaweicloud.sample;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.config.DynamicPropertyFactory;

@RestController
public class ConsumerController {

@Autowired
private RestTemplate restTemplate;

@RequestMapping("/load-balance")
public String getOrder(@RequestParam("id") String id) {
return restTemplate.getForObject("http://load-balance-provider/load-balance?id=" + id, String.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
servicecomb:
matchGroup:
AllOperation: |
matches:
- apiPath:
prefix: "/"
loadbalance:
AllOperation: |
rule: RoundRobin
21 changes: 21 additions & 0 deletions governance/loadbalance/consumer/src/main/resources/bootstrap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
server:
port: 8090

spring:
application:
# 微服务名称,本示例使用固定值(可替换成自己想要的名字),因为微服务名称会被客户端使用,不能轻易变化。
name: load-balance-consumer
cloud:
servicecomb:
discovery:
# 应用名称,本示例固定值(可替换成自己想要的名字),因为只有应用名称相同的微服务才能够相互发现,不能轻易变化。
appName: load-balance-application
serviceName: ${spring.application.name}
# 注册中心地址,本示例使用ServiceStage环境变量。建议保留这种配置方式,部署的时候,不用手工修改地址。
address: ${PAAS_CSE_SC_ENDPOINT:http://127.0.0.1:30100}
# 微服务版本号,本示例使用ServiceStage环境变量。建议保留这种配置方式,部署的时候,不用手工修改版本号,防止契约注册失败。
version: ${CAS_INSTANCE_VERSION:0.0.1}
config:
# 配置中心地址,本示例使用ServiceStage环境变量。建议保留这种配置方式,部署的时候,不用手工修改地址。
serverAddr: ${PAAS_CSE_CC_ENDPOINT:http://127.0.0.1:30110}
serverType: kie
8 changes: 8 additions & 0 deletions governance/loadbalance/consumer/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
JAR=consumer-1.0-SNAPSHOT.jar
if [ ! -e $JAR ]; then
JAR=target/$JAR
if [ -e application.yaml ]; then
cp application.yaml ./target/
fi
fi
java $CMDVAR -jar ./$JAR
84 changes: 84 additions & 0 deletions governance/loadbalance/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>com.huaweicloud.samples</groupId>
<artifactId>loadbalance-application</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modelVersion>4.0.0</modelVersion>

<properties>
<spring-boot.version>2.7.18</spring-boot.version>
<spring-cloud.version>2021.0.9</spring-cloud.version>
<spring-cloud-huawei.version>1.11.8-2021.0.x</spring-cloud-huawei.version>
</properties>

<modules>
<module>consumer</module>
<module>provider-one</module>
<module>provider-two</module>
<module>provider-three</module>
</modules>

<dependencyManagement>
<dependencies>
<!-- configure user spring cloud / spring boot versions -->
<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>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<!-- configure spring cloud huawei version -->
<dependency>
<groupId>com.huaweicloud</groupId>
<artifactId>spring-cloud-huawei-dependencies</artifactId>
<version>${spring-cloud-huawei.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-parameters</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.6.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>${main.class}</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
7 changes: 7 additions & 0 deletions governance/loadbalance/provider-one/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM openjdk:8u181-jdk-alpine

WORKDIR /home/apps/
ADD target/provider-one-1.0-SNAPSHOT.jar .
ADD start.sh .

ENTRYPOINT ["sh", "/home/apps/start.sh"]
35 changes: 35 additions & 0 deletions governance/loadbalance/provider-one/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.huaweicloud.samples</groupId>
<artifactId>loadbalance-application</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>provider-one</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.huaweicloud</groupId>
<artifactId>spring-cloud-starter-huawei-service-engine</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,17 @@
package com.huaweicloud.sample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
* @Author GuoYl123
* @Date 2019/10/22
**/
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderOneApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderOneApplication.class, args);
}
}
Loading

0 comments on commit 7572daa

Please sign in to comment.