Initial commit

This commit is contained in:
Florian 2025-10-28 09:24:41 +01:00
commit fdeab1dc7f
14 changed files with 550 additions and 0 deletions

123
pom.xml Normal file
View File

@ -0,0 +1,123 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.samples.petclinic.visits</groupId>
<artifactId>spring-petclinic-visits-service</artifactId>
<packaging>jar</packaging>
<description>Spring PetClinic Visits Service</description>
<parent>
<groupId>org.springframework.samples</groupId>
<artifactId>spring-petclinic-microservices</artifactId>
<version>3.4.1</version>
</parent>
<properties>
<docker.exposed.port>8081</docker.exposed.port>
<docker.image.dockerfile.dir>${basedir}/../docker</docker.image.dockerfile.dir>
</properties>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Cloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- Third parties -->
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>chaos-monkey-spring-boot</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-observation</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter-brave</artifactId>
</dependency>
<dependency>
<groupId>net.ttddyy.observation</groupId>
<artifactId>datasource-micrometer-spring-boot</artifactId>
<version>1.0.2</version>
</dependency>
<!-- Testing -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>buildDocker</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@ -0,0 +1,32 @@
/*
* Copyright 2002-2021 the original author or authors.
*
* 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 org.springframework.samples.petclinic.visits;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author Maciej Szarlinski
*/
@EnableDiscoveryClient
@SpringBootApplication
public class VisitsServiceApplication {
public static void main(String[] args) {
SpringApplication.run(VisitsServiceApplication.class, args);
}
}

View File

@ -0,0 +1,22 @@
package org.springframework.samples.petclinic.visits.config;
import io.micrometer.core.aop.TimedAspect;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MetricConfig {
@Bean
MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
return registry -> registry.config().commonTags("application", "petclinic");
}
@Bean
TimedAspect timedAspect(MeterRegistry registry) {
return new TimedAspect(registry);
}
}

View File

@ -0,0 +1,126 @@
/*
* Copyright 2002-2021 the original author or authors.
*
* 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 org.springframework.samples.petclinic.visits.model;
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.*;
import jakarta.validation.constraints.Size;
import java.util.Date;
/**
* Simple JavaBean domain object representing a visit.
*
* @author Ken Krebs
* @author Maciej Szarlinski
* @author Ramazan Sakin
*/
@Entity
@Table(name = "visits")
public class Visit {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "visit_date")
@Temporal(TemporalType.TIMESTAMP)
@JsonFormat(pattern = "yyyy-MM-dd")
private Date date = new Date();
@Size(max = 8192)
@Column(name = "description")
private String description;
@Column(name = "pet_id")
private int petId;
public Integer getId() {
return this.id;
}
public Date getDate() {
return this.date;
}
public String getDescription() {
return this.description;
}
public int getPetId() {
return this.petId;
}
public void setId(Integer id) {
this.id = id;
}
public void setDate(Date date) {
this.date = date;
}
public void setDescription(String description) {
this.description = description;
}
public void setPetId(int petId) {
this.petId = petId;
}
public static final class VisitBuilder {
private Integer id;
private Date date;
private @Size(max = 8192) String description;
private int petId;
private VisitBuilder() {
}
public static VisitBuilder aVisit() {
return new VisitBuilder();
}
public VisitBuilder id(Integer id) {
this.id = id;
return this;
}
public VisitBuilder date(Date date) {
this.date = date;
return this;
}
public VisitBuilder description(String description) {
this.description = description;
return this;
}
public VisitBuilder petId(int petId) {
this.petId = petId;
return this;
}
public Visit build() {
Visit visit = new Visit();
visit.setId(id);
visit.setDate(date);
visit.setDescription(description);
visit.setPetId(petId);
return visit;
}
}
}

View File

@ -0,0 +1,38 @@
/*
* Copyright 2002-2021 the original author or authors.
*
* 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 org.springframework.samples.petclinic.visits.model;
import java.util.Collection;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* Repository class for <code>Visit</code> domain objects All method names are compliant with Spring Data naming conventions so this interface can easily be extended for Spring
* Data See here: http://static.springsource.org/spring-data/jpa/docs/current/reference/html/jpa.repositories.html#jpa.query-methods.query-creation
*
* @author Ken Krebs
* @author Juergen Hoeller
* @author Sam Brannen
* @author Michael Isvy
* @author Maciej Szarlinski
*/
public interface VisitRepository extends JpaRepository<Visit, Integer> {
List<Visit> findByPetId(int petId);
List<Visit> findByPetIdIn(Collection<Integer> petIds);
}

View File

@ -0,0 +1,82 @@
/*
* Copyright 2002-2021 the original author or authors.
*
* 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 org.springframework.samples.petclinic.visits.web;
import java.util.List;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import io.micrometer.core.annotation.Timed;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.samples.petclinic.visits.model.Visit;
import org.springframework.samples.petclinic.visits.model.VisitRepository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Juergen Hoeller
* @author Ken Krebs
* @author Arjen Poutsma
* @author Michael Isvy
* @author Maciej Szarlinski
* @author Ramazan Sakin
*/
@RestController
@Timed("petclinic.visit")
class VisitResource {
private static final Logger log = LoggerFactory.getLogger(VisitResource.class);
private final VisitRepository visitRepository;
VisitResource(VisitRepository visitRepository) {
this.visitRepository = visitRepository;
}
@PostMapping("owners/*/pets/{petId}/visits")
@ResponseStatus(HttpStatus.CREATED)
public Visit create(
@Valid @RequestBody Visit visit,
@PathVariable("petId") @Min(1) int petId) {
visit.setPetId(petId);
log.info("Saving visit {}", visit);
return visitRepository.save(visit);
}
@GetMapping("owners/*/pets/{petId}/visits")
public List<Visit> read(@PathVariable("petId") @Min(1) int petId) {
return visitRepository.findByPetId(petId);
}
@GetMapping("pets/visits")
public Visits read(@RequestParam("petId") List<Integer> petIds) {
final List<Visit> byPetIdIn = visitRepository.findByPetIdIn(petIds);
return new Visits(byPetIdIn);
}
record Visits(
List<Visit> items
) {
}
}

View File

@ -0,0 +1,13 @@
spring:
application:
name: visits-service
config:
import: optional:configserver:${CONFIG_SERVER_URL:http://localhost:8888/}
---
spring:
config:
activate:
on-profile: docker
import: configserver:http://config-server:8888

View File

@ -0,0 +1,4 @@
INSERT INTO visits VALUES (1, 7, '2013-01-01', 'rabies shot');
INSERT INTO visits VALUES (2, 8, '2013-01-02', 'rabies shot');
INSERT INTO visits VALUES (3, 8, '2013-01-03', 'neutered');
INSERT INTO visits VALUES (4, 7, '2013-01-04', 'spayed');

View File

@ -0,0 +1,10 @@
DROP TABLE visits IF EXISTS;
CREATE TABLE visits (
id INTEGER IDENTITY PRIMARY KEY,
pet_id INTEGER NOT NULL,
visit_date DATE,
description VARCHAR(8192)
);
CREATE INDEX visits_pet_id ON visits (pet_id);

View File

@ -0,0 +1,4 @@
INSERT IGNORE INTO visits VALUES (1, 7, '2010-03-04', 'rabies shot');
INSERT IGNORE INTO visits VALUES (2, 8, '2011-03-04', 'rabies shot');
INSERT IGNORE INTO visits VALUES (3, 8, '2009-06-04', 'neutered');
INSERT IGNORE INTO visits VALUES (4, 7, '2008-09-04', 'spayed');

View File

@ -0,0 +1,11 @@
CREATE DATABASE IF NOT EXISTS petclinic;
USE petclinic;
CREATE TABLE IF NOT EXISTS visits (
id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
pet_id INT(4) UNSIGNED NOT NULL,
visit_date DATE,
description VARCHAR(8192),
FOREIGN KEY (pet_id) REFERENCES pets(id)
) engine=InnoDB;

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<!-- Required for Loglevel managment into the Spring Petclinic Admin Server-->
<jmxConfigurator/>
</configuration>

View File

@ -0,0 +1,61 @@
package org.springframework.samples.petclinic.visits.web;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.samples.petclinic.visits.model.Visit;
import org.springframework.samples.petclinic.visits.model.VisitRepository;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import static java.util.Arrays.asList;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ExtendWith(SpringExtension.class)
@WebMvcTest(VisitResource.class)
@ActiveProfiles("test")
class VisitResourceTest {
@Autowired
MockMvc mvc;
@MockBean
VisitRepository visitRepository;
@Test
void shouldFetchVisits() throws Exception {
given(visitRepository.findByPetIdIn(asList(111, 222)))
.willReturn(
asList(
Visit.VisitBuilder.aVisit()
.id(1)
.petId(111)
.build(),
Visit.VisitBuilder.aVisit()
.id(2)
.petId(222)
.build(),
Visit.VisitBuilder.aVisit()
.id(3)
.petId(222)
.build()
)
);
mvc.perform(get("/pets/visits?petId=111,222"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.items[0].id").value(1))
.andExpect(jsonPath("$.items[1].id").value(2))
.andExpect(jsonPath("$.items[2].id").value(3))
.andExpect(jsonPath("$.items[0].petId").value(111))
.andExpect(jsonPath("$.items[1].petId").value(222))
.andExpect(jsonPath("$.items[2].petId").value(222));
}
}

View File

@ -0,0 +1,18 @@
spring:
cloud:
config:
enabled: false
sql:
init:
schema-locations: classpath*:db/hsqldb/schema.sql
data-locations: classpath*:db/hsqldb/data.sql
jpa:
hibernate:
ddl-auto: none
eureka:
client:
enabled: false
logging.level.org.springframework: INFO