Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spring-cloud-bus example #16

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
.DS_Store
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ Not intended for production usage, but to reproduce issues or to try out things.

## Discovery

### Kubernetes
- [kubernetes](kubernetes)

### Nacos
- [nacos](nacos)

- [eureka](eureka)

## Proxy

- [nginx](nginx)

## Cloud

- [Spring Cloud Bus](spring-cloud-bus)
2 changes: 1 addition & 1 deletion eureka/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ Admin UI: http://localhost:8080/

## Stop Everything
```bash
docker-compose down -v
docker compose down -v
```
2 changes: 1 addition & 1 deletion eureka/buildAndRunAll.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ cd apps/hello-world
./build.sh
cd ../..

docker-compose up -d
docker compose up -d
2 changes: 1 addition & 1 deletion nacos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ http://localhost:8080/

## Stop Everything
```bash
docker-compose down -v
docker compose down -v
```

## Step-by-step Guide
Expand Down
2 changes: 1 addition & 1 deletion nacos/buildAndRunAll.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ cd apps/health-simulator
./build.sh
cd ../..

docker-compose up -d
docker compose up -d
2 changes: 1 addition & 1 deletion nginx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ chmod u+x buildAndRunAll.sh

## Stop Everything
```bash
docker-compose down -v
docker compose down -v
```
2 changes: 1 addition & 1 deletion nginx/buildAndRunAll.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ cd apps/hello-world
./build.sh
cd ../..

docker-compose up -d
docker compose up -d
30 changes: 30 additions & 0 deletions spring-cloud-bus/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Sample for [Spring Cloud Bus](https://docs.spring.io/spring-cloud-bus/docs/current/reference/html/)

Starts
- RabbitMQ (http://localhost:15672, credentials: guest/guest)
- a Spring Boot Admin Server (http://localhost:8080)
- a Hello World app (http://localhost:8081)

If a bus-refresh is triggered on one of the spring boot apps, spring cloud sends a message to all other apps to
refresh their configuration (visible in the logs, currently there is nothing to refresh).

Bus-refresh is now also available in the Spring Boot Admin UI on the environment page.
Bus-env and bus-shutdown are not yet supported.

## Prerequisites

- Java
- Maven
- Docker and Docker Compose

## Run Everything
You can run the whole build and start all apps in docker containers with the following script
```bash
chmod u+x buildAndRunAll.sh
./buildAndRunAll.sh
```

## Stop Everything
```bash
docker compose down -v
```
5 changes: 5 additions & 0 deletions spring-cloud-bus/apps/hello-world/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
target
.idea
*.iml
*.log
*.gz
8 changes: 8 additions & 0 deletions spring-cloud-bus/apps/hello-world/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# https://hub.docker.com/_/eclipse-temurin/
FROM eclipse-temurin:17

VOLUME /tmp

COPY target/app.jar /opt/app/app.jar

CMD ["bash", "-c", "java $JAVA_OPTS -jar /opt/app/app.jar"]
6 changes: 6 additions & 0 deletions spring-cloud-bus/apps/hello-world/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

# Build App
mvn package
# Build Docker Image
docker build --tag hello-world-cloudbus .
86 changes: 86 additions & 0 deletions spring-cloud-bus/apps/hello-world/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.1</version>
</parent>

<groupId>de.codecentric</groupId>
<artifactId>hello-world-cloud-bus</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>hello-world-cloud-bus</name>
<description>hello world with spring cloud-bus</description>

<properties>
<java.version>17</java.version>
<spring-boot-admin.version>3.4.2-SNAPSHOT</spring-boot-admin.version>
<spring-cloud.version>2024.0.0</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-dependencies</artifactId>
<version>${spring-boot-admin.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>
</dependencies>
</dependencyManagement>

<build>
<finalName>app</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
<!-- Build-Info for Info-Actuator -->
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package de.codecentric.helloworld.cloudbus;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorld {

@GetMapping("/")
public String hello() {
return "Hello World!";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.codecentric.helloworld.cloudbus;

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

@SpringBootApplication
public class HelloWorldCloudBusApplication {

public static void main(String[] args) {
SpringApplication.run(HelloWorldCloudBusApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
server:
port: 8081
spring:
application: # Application-Info for the Info-Actuator
name: "@pom.artifactId@"
boot:
admin:
client:
url: "http://localhost:8080"
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
management: # Actuator Configuration
endpoints:
web:
exposure:
include: "*"
endpoint: # Health-Actuator
health:
show-details: always
env:
post:
enabled: true
info: # Info-Actuator
java:
enabled: true
os:
enabled: true
build:
enabled: true
env:
enabled: true
info: # Application-Info for the Info-Actuator
group: "@pom.groupId@"
artifact: "@pom.artifactId@"
description: "@pom.description@"
version: "@pom.version@"
spring-boot: "@pom.parent.version@"
# Tags for the Spring Boot Admin UI
tags:
spring-boot: "@pom.parent.version@"
level:
de.codecentric.boot.admin: DEBUG
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.codecentric.helloworld.cloudbus;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class HelloWorldCloudBusApplicationTests {

@Test
void contextLoads() {
}

}
5 changes: 5 additions & 0 deletions spring-cloud-bus/apps/spring-boot-admin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
target
.idea
*.iml
*.log
*.gz
8 changes: 8 additions & 0 deletions spring-cloud-bus/apps/spring-boot-admin/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# https://hub.docker.com/_/eclipse-temurin/
FROM eclipse-temurin:17

VOLUME /tmp

COPY target/app.jar /opt/app/app.jar

CMD ["bash", "-c", "java $JAVA_OPTS -jar /opt/app/app.jar"]
6 changes: 6 additions & 0 deletions spring-cloud-bus/apps/spring-boot-admin/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

# Build App
mvn package
# Build Docker Image
docker build --tag spring-boot-admin-cloudbus .
Loading