From 01acc9cd10d4b01f85a20e05d22d0924603abf44 Mon Sep 17 00:00:00 2001 From: Eric Deandrea Date: Fri, 18 Jun 2021 08:05:46 -0400 Subject: [PATCH] Remove Spring Boot fault tolerance Part of openshift-labs/learn-katacoda#1060 & openshift-labs/learn-katacoda#1193 --- .../microservices-circuit-breaking/.gitignore | 4 - .../fruit-service/pom.xml | 126 ------------ .../fruit-service/src/main/fabric8/route.yml | 10 - .../openshift/booster/BoosterApplication.java | 35 ---- .../service/CircuitBreakerController.java | 39 ---- .../service/CircuitBreakerHandler.java | 60 ------ .../booster/service/CircuitBreakerState.java | 41 ---- .../booster/service/FruitController.java | 68 ------- .../booster/service/NameService.java | 49 ----- .../booster/service/WebSocketConfig.java | 42 ---- .../src/main/resources/application.yml | 8 - .../src/main/resources/static/index.html | 187 ------------------ .../name-service/pom.xml | 122 ------------ .../name-service/src/main/fabric8/route.yml | 10 - .../openshift/booster/BoosterApplication.java | 33 ---- .../booster/service/NameController.java | 173 ---------------- .../booster/service/WebSocketConfig.java | 42 ---- spring/microservices-circuit-breaking/pom.xml | 49 ----- 18 files changed, 1098 deletions(-) delete mode 100644 spring/microservices-circuit-breaking/.gitignore delete mode 100644 spring/microservices-circuit-breaking/fruit-service/pom.xml delete mode 100644 spring/microservices-circuit-breaking/fruit-service/src/main/fabric8/route.yml delete mode 100644 spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/BoosterApplication.java delete mode 100644 spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/service/CircuitBreakerController.java delete mode 100644 spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/service/CircuitBreakerHandler.java delete mode 100644 spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/service/CircuitBreakerState.java delete mode 100644 spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/service/FruitController.java delete mode 100644 spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/service/NameService.java delete mode 100644 spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/service/WebSocketConfig.java delete mode 100644 spring/microservices-circuit-breaking/fruit-service/src/main/resources/application.yml delete mode 100644 spring/microservices-circuit-breaking/fruit-service/src/main/resources/static/index.html delete mode 100644 spring/microservices-circuit-breaking/name-service/pom.xml delete mode 100644 spring/microservices-circuit-breaking/name-service/src/main/fabric8/route.yml delete mode 100644 spring/microservices-circuit-breaking/name-service/src/main/java/io/openshift/booster/BoosterApplication.java delete mode 100644 spring/microservices-circuit-breaking/name-service/src/main/java/io/openshift/booster/service/NameController.java delete mode 100644 spring/microservices-circuit-breaking/name-service/src/main/java/io/openshift/booster/service/WebSocketConfig.java delete mode 100644 spring/microservices-circuit-breaking/pom.xml diff --git a/spring/microservices-circuit-breaking/.gitignore b/spring/microservices-circuit-breaking/.gitignore deleted file mode 100644 index 7750e9a..0000000 --- a/spring/microservices-circuit-breaking/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -.idea/ -*.iml -*.ipr -target diff --git a/spring/microservices-circuit-breaking/fruit-service/pom.xml b/spring/microservices-circuit-breaking/fruit-service/pom.xml deleted file mode 100644 index 3265568..0000000 --- a/spring/microservices-circuit-breaking/fruit-service/pom.xml +++ /dev/null @@ -1,126 +0,0 @@ - - - 4.0.0 - - spring-boot-circuit-breaker-greeting - io.openshift.booster - 1.0.0-SNAPSHOT - - Spring Boot - Circuit Breaker Booster - Greeting Service - - 1.8 - 1.8 - UTF-8 - UTF-8 - 1.0.3 - - 3.3.9 - 9.4.1212 - 1.5 - - 2.1.6.RELEASE - 2.1.6.Final-redhat-00004 - 2.20 - 3.5.40 - true - true - - registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift:${openjdk18-openshift.version} - - 3.1.0 - 1.4.0.Final - 3.1.0 - - - - - me.snowdrop - spring-boot-bom - ${spring-boot-bom.version} - pom - import - - - - - - - org.springframework.cloud - spring-cloud-starter-netflix-hystrix - 2.1.1.RELEASE - - - - org.springframework.boot - spring-boot-starter-web - - - - org.springframework.boot - spring-boot-starter-websocket - - - - org.springframework.boot - spring-boot-starter-actuator - - - - - - org.springframework.boot - spring-boot-maven-plugin - ${spring-boot.version} - - - - repackage - - - - - - local - - exec - - - - - - - - openshift - - - - - - - io.fabric8 - fabric8-maven-plugin - ${fabric8-maven-plugin.version} - - - fmp - - resource - build - - - - - - - - - - diff --git a/spring/microservices-circuit-breaking/fruit-service/src/main/fabric8/route.yml b/spring/microservices-circuit-breaking/fruit-service/src/main/fabric8/route.yml deleted file mode 100644 index e63998c..0000000 --- a/spring/microservices-circuit-breaking/fruit-service/src/main/fabric8/route.yml +++ /dev/null @@ -1,10 +0,0 @@ -apiVersion: v1 -kind: Route -metadata: - name: ${project.artifactId} -spec: - port: - targetPort: 8080 - to: - kind: Service - name: ${project.artifactId} diff --git a/spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/BoosterApplication.java b/spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/BoosterApplication.java deleted file mode 100644 index f790567..0000000 --- a/spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/BoosterApplication.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2016-2017 Red Hat, Inc, and individual contributors. - * - * 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.openshift.booster; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; - -/** - * Entry point to the application. - */ -@SpringBootApplication -@EnableCircuitBreaker -public class BoosterApplication { - - public static void main(String[] args) { - SpringApplication.run(BoosterApplication.class, args); - } - -} - diff --git a/spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/service/CircuitBreakerController.java b/spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/service/CircuitBreakerController.java deleted file mode 100644 index 3727d23..0000000 --- a/spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/service/CircuitBreakerController.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2016-2017 Red Hat, Inc, and individual contributors. - * - * 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.openshift.booster.service; - -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -/** - * Circuit Breaker state controller. - */ -@RestController -public class CircuitBreakerController { - - private NameService nameService; - - public CircuitBreakerController(NameService nameService) { - this.nameService = nameService; - } - - @RequestMapping("/api/cb-state") - public CircuitBreakerState getState() throws Exception { - return nameService.getState(); - } - -} diff --git a/spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/service/CircuitBreakerHandler.java b/spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/service/CircuitBreakerHandler.java deleted file mode 100644 index f2471af..0000000 --- a/spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/service/CircuitBreakerHandler.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2016-2017 Red Hat, Inc, and individual contributors. - * - * 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.openshift.booster.service; - -import java.util.Queue; -import java.util.concurrent.ConcurrentLinkedQueue; - -import org.springframework.web.socket.CloseStatus; -import org.springframework.web.socket.TextMessage; -import org.springframework.web.socket.WebSocketHandler; -import org.springframework.web.socket.WebSocketMessage; -import org.springframework.web.socket.WebSocketSession; - -/** - * Circuit Breaker state handler. - */ -public class CircuitBreakerHandler implements WebSocketHandler { - - private Queue currentSessions = new ConcurrentLinkedQueue<>(); - - void sendMessage(CircuitBreakerState state) throws Exception { - TextMessage message = new TextMessage("isOpen:" + CircuitBreakerState.OPEN.equals(state)); - for (WebSocketSession session : currentSessions) { - session.sendMessage(message); - } - } - - public void afterConnectionEstablished(WebSocketSession session) throws Exception { - currentSessions.add(session); - } - - public void handleMessage(WebSocketSession session, WebSocketMessage webSocketMessage) throws Exception { - } - - public void handleTransportError(WebSocketSession session, Throwable throwable) throws Exception { - - } - - public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { - currentSessions.remove(session); - } - - public boolean supportsPartialMessages() { - return false; - } -} diff --git a/spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/service/CircuitBreakerState.java b/spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/service/CircuitBreakerState.java deleted file mode 100644 index 990e3ca..0000000 --- a/spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/service/CircuitBreakerState.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2016-2017 Red Hat, Inc, and individual contributors. - * - * 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.openshift.booster.service; - -/** - * Circuit Breaker state. - */ -public class CircuitBreakerState { - - static final CircuitBreakerState OPEN = new CircuitBreakerState("open"); - static final CircuitBreakerState CLOSED = new CircuitBreakerState("closed"); - - private String state; - - public CircuitBreakerState() { - } - - public CircuitBreakerState(String state) { - this.state = state; - } - - public String getState() { - return state; - } - -} - diff --git a/spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/service/FruitController.java b/spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/service/FruitController.java deleted file mode 100644 index 38c53a1..0000000 --- a/spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/service/FruitController.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2016-2017 Red Hat, Inc, and individual contributors. - * - * 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.openshift.booster.service; - -import org.springframework.context.annotation.Bean; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.socket.WebSocketHandler; - -/** - * Fruit service controller. - */ -@RestController -public class FruitController { - - private final NameService nameService; - private final CircuitBreakerHandler handler = new CircuitBreakerHandler(); - - public FruitController(NameService nameService) { - this.nameService = nameService; - } - - @RequestMapping("/api/ping") - public Fruit getPing() throws Exception { - return new Fruit("OK"); - } - - /** - * Endpoint to get a greeting. This endpoint uses a name server to get a name for the greeting. - *

- * Request to the name service is guarded with a circuit breaker. Therefore if a name service is not available or is too - * slow to response fallback name is used. - * - * @return Fruit string. - */ - // TODO Call name service here - - @Bean - public WebSocketHandler getHandler() { - return handler; - } - - static class Fruit { - private final String content; - - public Fruit(String content) { - this.content = content; - } - - public String getContent() { - return content; - } - } -} diff --git a/spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/service/NameService.java b/spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/service/NameService.java deleted file mode 100644 index b1ed78a..0000000 --- a/spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/service/NameService.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2016-2017 Red Hat, Inc, and individual contributors. - * - * 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.openshift.booster.service; - -import com.netflix.hystrix.HystrixCircuitBreaker; -import com.netflix.hystrix.HystrixCommandKey; -import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; -import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; -import org.springframework.stereotype.Service; -import org.springframework.web.client.RestTemplate; - -/** - * Service invoking name-service via REST and guarded by Hystrix. - */ -@Service -public class NameService { - - private static final HystrixCommandKey KEY = HystrixCommandKey.Factory.asKey("NameService"); - - private final String nameHost = System.getProperty("name.host", "http://spring-boot-circuit-breaker-name:8080"); - private final RestTemplate restTemplate = new RestTemplate(); - - // TODO Add Hystrix command here - - public String getName() { - return restTemplate.getForObject(nameHost + "/api/name", String.class); - } - - // TODO Add fallback method here - - CircuitBreakerState getState() throws Exception { - HystrixCircuitBreaker circuitBreaker = HystrixCircuitBreaker.Factory.getInstance(KEY); - return circuitBreaker != null && circuitBreaker.isOpen() ? CircuitBreakerState.OPEN : CircuitBreakerState.CLOSED; - } -} diff --git a/spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/service/WebSocketConfig.java b/spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/service/WebSocketConfig.java deleted file mode 100644 index 672606a..0000000 --- a/spring/microservices-circuit-breaking/fruit-service/src/main/java/io/openshift/booster/service/WebSocketConfig.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2016-2017 Red Hat, Inc, and individual contributors. - * - * 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.openshift.booster.service; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.socket.WebSocketHandler; -import org.springframework.web.socket.config.annotation.EnableWebSocket; -import org.springframework.web.socket.config.annotation.WebSocketConfigurer; -import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; - -/** - */ -@Configuration -@EnableWebSocket -public class WebSocketConfig implements WebSocketConfigurer { - - private WebSocketHandler handler; - - @Autowired - public WebSocketConfig(WebSocketHandler handler) { - this.handler = handler; - } - - public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { - registry.addHandler(handler, "/cb-ws"); - } -} \ No newline at end of file diff --git a/spring/microservices-circuit-breaking/fruit-service/src/main/resources/application.yml b/spring/microservices-circuit-breaking/fruit-service/src/main/resources/application.yml deleted file mode 100644 index 1dd9fd1..0000000 --- a/spring/microservices-circuit-breaking/fruit-service/src/main/resources/application.yml +++ /dev/null @@ -1,8 +0,0 @@ -hystrix: - command: - default: - circuitBreaker: - requestVolumeThreshold: 3 - metrics: - healthSnapshot: - intervalInMilliseconds: 100 \ No newline at end of file diff --git a/spring/microservices-circuit-breaking/fruit-service/src/main/resources/static/index.html b/spring/microservices-circuit-breaking/fruit-service/src/main/resources/static/index.html deleted file mode 100644 index 8eb63f0..0000000 --- a/spring/microservices-circuit-breaking/fruit-service/src/main/resources/static/index.html +++ /dev/null @@ -1,187 +0,0 @@ - - - - - - Circuit Breaker Mission - Spring Boot - - - - - -

- -
-

Circuit Breaker Mission - Spring Boot

-

- Cloud applications are often comprised of many services. And between those services there often can be many interactions. - But even if some of those services fail or are unavailable, we may want to offer an alternative response to an error condition, - that it does not increase load on service in, which is unavailable because of high load. And to achieve this, we need a generic - mechanism over the failed or unavailable service to service communication, which can quickly and easily determine that the services - are not functioning properly, hence can either return a default result or fail quickly and gracefully with enough information about - the current failure. This mechanism is called circuit breaker. -

-

- On this page you can invoke the greeting service. This service uses a name - service. The invocations are protected by a circuit breaker. You can configure the state of the - name service using the toggle button. -

- -
- -
-

Greeting service

-
- -

Results:

-
- -
-
-
- -
-

Name service

-
-
- -
-

Requests:

-
- -
-
-
- -
- -
- - - - - - - - \ No newline at end of file diff --git a/spring/microservices-circuit-breaking/name-service/pom.xml b/spring/microservices-circuit-breaking/name-service/pom.xml deleted file mode 100644 index e392565..0000000 --- a/spring/microservices-circuit-breaking/name-service/pom.xml +++ /dev/null @@ -1,122 +0,0 @@ - - - 4.0.0 - - spring-boot-circuit-breaker-name - io.openshift.booster - 1.0.0-SNAPSHOT - - Spring Boot - Circuit Breaker Booster - Name Service - - 1.8 - 1.8 - UTF-8 - UTF-8 - - 3.3.9 - 9.4.1212 - 1.3 - - 1.0.3 - 2.1.6.RELEASE - 2.1.6.Final-redhat-00004 - 2.20 - 3.5.40 - true - true - - registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift:${openjdk18-openshift.version} - - 3.1.0 - 1.4.0.Final - 3.1.0 - - - - - - me.snowdrop - spring-boot-bom - ${spring-boot-bom.version} - pom - import - - - - - - - org.springframework.boot - spring-boot-starter-web - - - - org.springframework.boot - spring-boot-starter-websocket - - - - org.springframework.boot - spring-boot-starter-actuator - - - - - - - org.springframework.boot - spring-boot-maven-plugin - ${spring-boot.version} - - - local - - exec - - - - - repackage - - - - - - - - - - openshift - - - - - - - io.fabric8 - fabric8-maven-plugin - ${fabric8-maven-plugin.version} - - - fmp - - resource - build - - - - - - - - - - diff --git a/spring/microservices-circuit-breaking/name-service/src/main/fabric8/route.yml b/spring/microservices-circuit-breaking/name-service/src/main/fabric8/route.yml deleted file mode 100644 index e63998c..0000000 --- a/spring/microservices-circuit-breaking/name-service/src/main/fabric8/route.yml +++ /dev/null @@ -1,10 +0,0 @@ -apiVersion: v1 -kind: Route -metadata: - name: ${project.artifactId} -spec: - port: - targetPort: 8080 - to: - kind: Service - name: ${project.artifactId} diff --git a/spring/microservices-circuit-breaking/name-service/src/main/java/io/openshift/booster/BoosterApplication.java b/spring/microservices-circuit-breaking/name-service/src/main/java/io/openshift/booster/BoosterApplication.java deleted file mode 100644 index bf652f9..0000000 --- a/spring/microservices-circuit-breaking/name-service/src/main/java/io/openshift/booster/BoosterApplication.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2016-2017 Red Hat, Inc, and individual contributors. - * - * 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.openshift.booster; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -/** - * Entry point to the application. - */ -@SpringBootApplication -public class BoosterApplication { - - public static void main(String[] args) { - SpringApplication.run(BoosterApplication.class, args); - } - -} - diff --git a/spring/microservices-circuit-breaking/name-service/src/main/java/io/openshift/booster/service/NameController.java b/spring/microservices-circuit-breaking/name-service/src/main/java/io/openshift/booster/service/NameController.java deleted file mode 100644 index 3a7ee87..0000000 --- a/spring/microservices-circuit-breaking/name-service/src/main/java/io/openshift/booster/service/NameController.java +++ /dev/null @@ -1,173 +0,0 @@ -/* - * Copyright 2016-2017 Red Hat, Inc, and individual contributors. - * - * 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.openshift.booster.service; - -import java.io.IOException; -import java.time.LocalTime; -import java.util.Queue; -import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.concurrent.atomic.AtomicBoolean; - -import javax.servlet.Filter; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.context.annotation.Bean; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -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.cors.CorsConfiguration; -import org.springframework.web.filter.CorsFilter; -import org.springframework.web.socket.CloseStatus; -import org.springframework.web.socket.TextMessage; -import org.springframework.web.socket.WebSocketHandler; -import org.springframework.web.socket.WebSocketMessage; -import org.springframework.web.socket.WebSocketSession; - -/** - * Name service controller. - */ -@RestController -public class NameController { - - private static final Logger LOG = LoggerFactory.getLogger(NameController.class); - - private static final String theName = "apple"; - - private final AtomicBoolean doFail = new AtomicBoolean(); - private final NameServiceWebSockerHandler handler = new NameServiceWebSockerHandler(); - - @RequestMapping("/api/ping") - public StateInfo getPing() throws Exception { - return StateInfo.OK; - } - - /** - * Endpoint to get a name. - * - * @return Host name. - */ - @RequestMapping("/api/name") - public ResponseEntity getName() throws IOException { - handler.sendMessage("GET /api/name at " + LocalTime.now()); - - if (doFail.get()) { - return new ResponseEntity<>("Name service down", HttpStatus.INTERNAL_SERVER_ERROR); - } else { - LOG.info(String.format("Returning a name '%s'", theName)); - return new ResponseEntity<>(theName, HttpStatus.OK); - } - } - - /** - * Set name service state - via GET. - */ - @GetMapping("/api/state") - public StateInfo applyState(@RequestParam(name = "state") String state) throws Exception { - doFail.set("fail".equalsIgnoreCase(state)); - LOG.info("Name service state set to " + state); - handler.sendMessage("state:" + !doFail.get()); - return getState(); - } - - /** - * Set name service state - via PUT. - */ - @PutMapping("/api/state") - public StateInfo applyState(@RequestBody StateInfo info) throws Exception { - return applyState(info.getState()); - } - - /** - * Set name service state. - */ - @RequestMapping("/api/info") - public StateInfo getState() throws Exception { - return doFail.get() ? StateInfo.FAIL : StateInfo.OK; - } - - static class StateInfo { - private String state; - - static final StateInfo OK = new StateInfo("ok"); - static final StateInfo FAIL = new StateInfo("fail"); - - public StateInfo() { - } - - public StateInfo(String state) { - this.state = state; - } - - public String getState() { - return state; - } - - public void setState(String state) { - this.state = state; - } - } - - @Bean - public WebSocketHandler getHandler() { - return handler; - } - - @Bean - public Filter getCorsFilter() { - final CorsConfiguration configuration = new CorsConfiguration(); - configuration.addAllowedOrigin(CorsConfiguration.ALL); - configuration.addAllowedMethod(CorsConfiguration.ALL); - configuration.addAllowedHeader(CorsConfiguration.ALL); - return new CorsFilter(request -> configuration); - } - - private class NameServiceWebSockerHandler implements WebSocketHandler { - private Queue currentSessions = new ConcurrentLinkedQueue<>(); - - void sendMessage(String message) throws IOException { - TextMessage textMessage = new TextMessage(message); - for (WebSocketSession session : currentSessions) { - session.sendMessage(textMessage); - } - } - - public void afterConnectionEstablished(WebSocketSession session) throws Exception { - currentSessions.add(session); - } - - public void handleMessage(WebSocketSession session, WebSocketMessage webSocketMessage) throws Exception { - } - - public void handleTransportError(WebSocketSession session, Throwable throwable) throws Exception { - - } - - public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { - currentSessions.remove(session); - } - - public boolean supportsPartialMessages() { - return false; - } - } -} diff --git a/spring/microservices-circuit-breaking/name-service/src/main/java/io/openshift/booster/service/WebSocketConfig.java b/spring/microservices-circuit-breaking/name-service/src/main/java/io/openshift/booster/service/WebSocketConfig.java deleted file mode 100644 index fb8dbcd..0000000 --- a/spring/microservices-circuit-breaking/name-service/src/main/java/io/openshift/booster/service/WebSocketConfig.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2016-2017 Red Hat, Inc, and individual contributors. - * - * 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.openshift.booster.service; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.socket.WebSocketHandler; -import org.springframework.web.socket.config.annotation.EnableWebSocket; -import org.springframework.web.socket.config.annotation.WebSocketConfigurer; -import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; - -/** - */ -@Configuration -@EnableWebSocket -public class WebSocketConfig implements WebSocketConfigurer { - - private WebSocketHandler handler; - - @Autowired - public WebSocketConfig(WebSocketHandler handler) { - this.handler = handler; - } - - public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { - registry.addHandler(handler, "/name-ws").setAllowedOrigins("*"); - } -} \ No newline at end of file diff --git a/spring/microservices-circuit-breaking/pom.xml b/spring/microservices-circuit-breaking/pom.xml deleted file mode 100644 index eda6659..0000000 --- a/spring/microservices-circuit-breaking/pom.xml +++ /dev/null @@ -1,49 +0,0 @@ - - - 4.0.0 - - booster-circuit-breaker-spring-boot - - io.openshift.booster - 1.0.0-SNAPSHOT - pom - - Spring Boot - Circuit Breaker Booster - Spring Boot - Circuit Breaker Booster - - - - 1.0.3 - - - - fruit-service - name-service - - - - - - org.glassfish - javax.json - ${org.glassfish.javax.json} - - - - -