Skip to content
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
32 changes: 32 additions & 0 deletions samples/bmi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Body Mass Index(BMI) Calculator Microservice Demo
## Architecture of BMI Calculator
There are two microservices in this demo.
* Webapp (API Gateway)
* BMI Calculator (computing service)

## Prerequisite
1. [Oracle JDK 1.8+](https://docs.oracle.com/javase/8/docs/technotes/guides/install/install_overview.html)
2. [Maven 3.x](https://maven.apache.org/install.html)

## Quick Start(Linux)
1. Run the service center
```bash
wget https://github.com/ServiceComb/service-center/releases/download/0.1.1/service-center-0.1.1-linux-amd64.tar.gz
tar xvf service-center-0.1.1-linux-amd64.tar.gz
(cd service-center-0.1.1-linux-amd64; bash start.sh)
```
2. Get the bmi demo's code
```bash
git clone https://github.com/ServiceComb/ServiceComb-Java-Chassis
cd ServiceComb-Java-Chassis/samples
```
3. Run microservices
* Run the **BMI calculator service**
```bash
(cd bmi/calculator; mvn spring-boot:run)
```
* Run the **webapp service**
```bash
(cd bmi/webapp; mvn spring-boot:run)
```
4. Visit the services via **<a>http://127.0.0.1:8888</a>**.
67 changes: 67 additions & 0 deletions samples/bmi/calculator/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2017 Huawei Technologies Co., Ltd
~
~ Licensed 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="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>
<artifactId>bmi</artifactId>
<groupId>io.servicecomb.samples</groupId>
<version>0.3.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>bmi-calculator</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
</dependency>
<dependency>
<groupId>io.servicecomb</groupId>
<artifactId>spring-boot-starter-provider</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</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,31 @@
/*
* Copyright 2017 Huawei Technologies Co., Ltd
*
* Licensed 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 io.servicecomb.samples.bmi.calculator;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import io.servicecomb.springboot.starter.provider.EnableServiceComb;

@SpringBootApplication
@EnableServiceComb
public class CalculatorApplication {

public static void main(String[] args) {
SpringApplication.run(CalculatorApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.servicecomb.samples.bmi.calculator;

/**
* {@link CalculatorEndpoint} provides the common interface for different endpoint implementations.
* It needs to be declared as public.
*/
public interface CalculatorEndpoint {
/**
* Calculate the BMI(Body Mass Index).
*/
double calculate(double height, double weight);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2017 Huawei Technologies Co., Ltd
*
* Licensed 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 io.servicecomb.samples.bmi.calculator;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import io.servicecomb.provider.rest.common.RestSchema;

/**
* {@link CalculatorRestEndpoint} provides the rest implementation of {@link CalculatorEndpoint}.
* The rest endpoint is accessed by /bmi?height={value}&width={value} with HTTP GET.
*/
@RestSchema(schemaId = "calculatorRestEndpoint")
@RequestMapping("/")
@Controller
public class CalculatorRestEndpoint implements CalculatorEndpoint {

private final CalculatorService calculatorService;

@Autowired
public CalculatorRestEndpoint(CalculatorService calculatorService) {
this.calculatorService = calculatorService;
}

@Override
@RequestMapping(value = "/bmi", method = RequestMethod.GET)
@ResponseBody
public double calculate(double height, double weight) {
return calculatorService.calculate(height, weight);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2017 Huawei Technologies Co., Ltd
*
* Licensed 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 io.servicecomb.samples.bmi.calculator;

/**
* {@link CalculatorService} provides interface of actual BMI calculation.
*/
public interface CalculatorService {

/**
* @see CalculatorEndpoint#calculate(double, double)
*/
double calculate(double height, double weight);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2017 Huawei Technologies Co., Ltd
*
* Licensed 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 io.servicecomb.samples.bmi.calculator;

import java.math.BigDecimal;
import java.math.RoundingMode;

import org.springframework.stereotype.Service;

@Service
public class CalculatorServiceImpl implements CalculatorService {

/**
* {@inheritDoc}
*/
@Override
public double calculate(double height, double weight) {
double heightInMeter = height / 100;
double bmi = weight / (heightInMeter * heightInMeter);
return roundToOnePrecision(bmi);
}

private double roundToOnePrecision(double value) {
if (Double.isInfinite(value) || Double.isNaN(value)) {
return value;
}
return new BigDecimal(value).setScale(1, RoundingMode.HALF_UP).doubleValue();
}
}
18 changes: 18 additions & 0 deletions samples/bmi/calculator/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Copyright 2017 Huawei Technologies Co., Ltd
#
# Licensed 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.
#

server:
port: 0
29 changes: 29 additions & 0 deletions samples/bmi/calculator/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
~ Copyright 2017 Huawei Technologies Co., Ltd
~
~ Licensed 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.
-->

<Configuration status="INFO">
<Appenders>
<Console name="stdout" target="SYSTEM_OUT">
<PatternLayout pattern="%d [%p] %m %l%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="stdout"/>
</Root>
</Loggers>
</Configuration>
28 changes: 28 additions & 0 deletions samples/bmi/calculator/src/main/resources/microservice.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#
# Copyright 2017 Huawei Technologies Co., Ltd
#
# Licensed 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.
#

# all interconnected microservices must belong to an application wth the same ID
APPLICATION_ID: bmi
service_description:
# name of the declaring microservice
name: calculator
version: 0.0.1
cse:
service:
registry:
address: http://127.0.0.1:30100
rest:
address: 0.0.0.0:7777
Loading