├── docker ├── .env ├── apache │ ├── 000-default.conf │ ├── 000-default.ctmpl │ ├── Dockerfile │ └── index.html └── docker-compose.yml ├── microservice-consuldns-demo ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.properties │ │ └── maven-wrapper.jar ├── microservice-consuldns-demo-catalog │ ├── src │ │ ├── test │ │ │ ├── resources │ │ │ │ └── application-test.properties │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── ewolff │ │ │ │ └── microservice │ │ │ │ └── catalog │ │ │ │ ├── CatalogTestApp.java │ │ │ │ ├── RepositoryTest.java │ │ │ │ ├── cdc │ │ │ │ ├── CatalogConsumerDrivenContractTest.java │ │ │ │ ├── Item.java │ │ │ │ └── CatalogClient.java │ │ │ │ └── CatalogWebIntegrationTest.java │ │ └── main │ │ │ ├── resources │ │ │ ├── application.properties │ │ │ ├── templates │ │ │ │ ├── success.html │ │ │ │ ├── searchForm.html │ │ │ │ ├── item.html │ │ │ │ ├── layout.html │ │ │ │ └── itemlist.html │ │ │ └── logback-spring.xml │ │ │ └── java │ │ │ └── com │ │ │ └── ewolff │ │ │ └── microservice │ │ │ └── catalog │ │ │ ├── ItemRepository.java │ │ │ ├── SpringRestDataConfig.java │ │ │ ├── CatalogApp.java │ │ │ ├── Item.java │ │ │ └── web │ │ │ └── CatalogController.java │ ├── Dockerfile │ └── pom.xml ├── microservice-consuldns-demo-order │ ├── src │ │ ├── test │ │ │ ├── resources │ │ │ │ └── application-test.properties │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── ewolff │ │ │ │ └── microservice │ │ │ │ └── order │ │ │ │ ├── OrderTestApp.java │ │ │ │ ├── catalogstub │ │ │ │ └── CatalogStub.java │ │ │ │ ├── logic │ │ │ │ ├── CatalogConsumerDrivenContractTest.java │ │ │ │ ├── CustomerConsumerDrivenContractTest.java │ │ │ │ └── OrderWebIntegrationTest.java │ │ │ │ └── customerstub │ │ │ │ └── CustomerStub.java │ │ └── main │ │ │ ├── resources │ │ │ ├── application.properties │ │ │ ├── templates │ │ │ │ ├── success.html │ │ │ │ ├── layout.html │ │ │ │ ├── order.html │ │ │ │ ├── orderlist.html │ │ │ │ └── orderForm.html │ │ │ ├── logback-spring.xml │ │ │ └── static │ │ │ │ └── monitor.html │ │ │ └── java │ │ │ └── com │ │ │ └── ewolff │ │ │ └── microservice │ │ │ └── order │ │ │ ├── logic │ │ │ ├── OrderRepository.java │ │ │ ├── SpringRestDataConfig.java │ │ │ ├── OrderService.java │ │ │ ├── OrderLine.java │ │ │ ├── Order.java │ │ │ └── OrderController.java │ │ │ ├── OrderApp.java │ │ │ └── clients │ │ │ ├── Item.java │ │ │ ├── Customer.java │ │ │ ├── CatalogClient.java │ │ │ └── CustomerClient.java │ ├── Dockerfile │ └── pom.xml ├── microservice-consuldns-demo-customer │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ ├── application.properties │ │ │ │ ├── templates │ │ │ │ │ ├── success.html │ │ │ │ │ ├── layout.html │ │ │ │ │ ├── customerlist.html │ │ │ │ │ └── customer.html │ │ │ │ └── logback-spring.xml │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── ewolff │ │ │ │ └── microservice │ │ │ │ └── customer │ │ │ │ ├── CustomerRepository.java │ │ │ │ ├── SpringRestDataConfig.java │ │ │ │ ├── CustomerApp.java │ │ │ │ ├── web │ │ │ │ └── CustomerController.java │ │ │ │ └── Customer.java │ │ └── test │ │ │ └── java │ │ │ └── com │ │ │ └── ewolff │ │ │ └── microservice │ │ │ └── customer │ │ │ ├── CustomerTestApp.java │ │ │ ├── cdc │ │ │ ├── CustomerConsumerDrivenContractTest.java │ │ │ ├── Customer.java │ │ │ └── CustomerClient.java │ │ │ └── CustomerWebIntegrationTest.java │ ├── Dockerfile │ └── pom.xml ├── pom.xml ├── mvnw.cmd └── mvnw ├── .gitignore ├── README.md ├── HOW-TO-RUN.md ├── WIE-LAUFEN.md └── LICENSE /docker/.env: -------------------------------------------------------------------------------- 1 | COMPOSE_PROJECT_NAME=msconsuldns -------------------------------------------------------------------------------- /microservice-consuldns-demo/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.2/apache-maven-3.5.2-bin.zip -------------------------------------------------------------------------------- /microservice-consuldns-demo/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewolff/microservice-consul-dns/HEAD/microservice-consuldns-demo/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-catalog/src/test/resources/application-test.properties: -------------------------------------------------------------------------------- 1 | catalog.service.host=localhost 2 | catalog.service.port=${server.port} 3 | spring.thymeleaf.cache=false 4 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-order/src/test/resources/application-test.properties: -------------------------------------------------------------------------------- 1 | customer.service.host=localhost 2 | customer.service.port=${server.port} 3 | catalog.service.host=localhost 4 | catalog.service.port=${server.port} 5 | spring.thymeleaf.cache=false 6 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-catalog/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | logging.level.org.springframework.boot.actuate.trace.WebRequestTraceFilter: TRACE 2 | spring.application.name=catalog 3 | server.port=8080 4 | management.endpoints.web.exposure.include=* 5 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-customer/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | logging.level.org.springframework.boot.actuate.trace.WebRequestTraceFilter: TRACE 2 | spring.application.name=customer 3 | server.port=8080 4 | management.endpoints.web.exposure.include=* 5 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-order/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM openjdk:10.0.2-jre-slim 2 | COPY target/microservice-consuldns-demo-order-0.0.1-SNAPSHOT.jar . 3 | CMD /usr/bin/java -Dlogging.path=/log/ -Xmx400m -Xms400m -jar microservice-consuldns-demo-order-0.0.1-SNAPSHOT.jar 4 | EXPOSE 8080 5 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-catalog/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM openjdk:10.0.2-jre-slim 2 | COPY target/microservice-consuldns-demo-catalog-0.0.1-SNAPSHOT.jar . 3 | CMD /usr/bin/java -Dlogging.path=/log/ -Xmx400m -Xms400m -jar microservice-consuldns-demo-catalog-0.0.1-SNAPSHOT.jar 4 | EXPOSE 8080 5 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-customer/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM openjdk:10.0.2-jre-slim 2 | COPY target/microservice-consuldns-demo-customer-0.0.1-SNAPSHOT.jar . 3 | CMD /usr/bin/java -Dlogging.path=/log/ -Xmx400m -Xms400m -jar microservice-consuldns-demo-customer-0.0.1-SNAPSHOT.jar 4 | EXPOSE 8080 5 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-order/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | logging.level.org.springframework.boot.actuate.trace.WebRequestTraceFilter: TRACE 2 | spring.application.name=order 3 | server.port=8080 4 | management.endpoints.web.exposure.include=* 5 | logging.level.com.ewolff.microservice.order.clients: TRACE -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | LOG_PATH_IS_UNDEFINED 3 | .vscode 4 | .vagrant 5 | .springBeans 6 | .classpath 7 | .project 8 | .settings 9 | .idea 10 | *.iml 11 | target/ 12 | pom.xml.tag 13 | pom.xml.releaseBackup 14 | pom.xml.versionsBackup 15 | pom.xml.next 16 | release.properties 17 | dependency-reduced-pom.xml 18 | buildNumber.properties 19 | .mvn/timing.properties -------------------------------------------------------------------------------- /docker/apache/000-default.conf: -------------------------------------------------------------------------------- 1 | 2 | DocumentRoot /var/www/html 3 | 4 | # This should be secured! 5 | 6 | SetHandler balancer-manager 7 | 8 | 9 | ErrorLog ${APACHE_LOG_DIR}/error.log 10 | CustomLog ${APACHE_LOG_DIR}/access.log combined 11 | 12 | ProxyPreserveHost On 13 | 14 | 15 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-catalog/src/main/resources/templates/success.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | Success 7 | 8 | 9 |

Success

10 |
11 | Action was successful! 12 |
13 | 14 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-customer/src/main/resources/templates/success.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | Success 7 | 8 | 9 |

Success

10 |
11 | Action was successful! 12 |
13 | 14 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-order/src/main/resources/templates/success.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | Success 7 | 8 | 9 |

Success

10 |
11 | Action was successful! 12 |
13 | 14 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-order/src/main/java/com/ewolff/microservice/order/logic/OrderRepository.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.order.logic; 2 | 3 | import org.springframework.data.repository.PagingAndSortingRepository; 4 | import org.springframework.data.rest.core.annotation.RepositoryRestResource; 5 | 6 | @RepositoryRestResource(collectionResourceRel = "order", path = "order") 7 | interface OrderRepository extends PagingAndSortingRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-order/src/test/java/com/ewolff/microservice/order/OrderTestApp.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.order; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class OrderTestApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication app = new SpringApplication(OrderTestApp.class); 11 | app.setAdditionalProfiles("test"); 12 | app.run(args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-catalog/src/test/java/com/ewolff/microservice/catalog/CatalogTestApp.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.catalog; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class CatalogTestApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication app = new SpringApplication(CatalogTestApp.class); 11 | app.setAdditionalProfiles("test"); 12 | app.run(args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-customer/src/test/java/com/ewolff/microservice/customer/CustomerTestApp.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.customer; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 | import org.springframework.context.annotation.ComponentScan; 6 | 7 | @ComponentScan 8 | @EnableAutoConfiguration 9 | public class CustomerTestApp { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication app = new SpringApplication(CustomerTestApp.class); 13 | app.setAdditionalProfiles("test"); 14 | app.run(args); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /docker/apache/000-default.ctmpl: -------------------------------------------------------------------------------- 1 | 2 | DocumentRoot /var/www/html 3 | 4 | # This should be secured! 5 | 6 | SetHandler balancer-manager 7 | 8 | 9 | ErrorLog ${APACHE_LOG_DIR}/error.log 10 | CustomLog ${APACHE_LOG_DIR}/access.log combined 11 | 12 | ProxyPreserveHost On 13 | 14 | {{range services}} 15 | 16 | 17 | {{range service .Name}} BalancerMember http://{{.Address}}:{{.Port}} 18 | {{end}} 19 | 20 | ProxyPass /{{.Name}} balancer://{{.Name}} 21 | ProxyPassReverse /{{.Name}} balancer://{{.Name}} 22 | 23 | {{end}} 24 | 25 | 26 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-order/src/main/java/com/ewolff/microservice/order/OrderApp.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.order; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.web.client.RestTemplate; 7 | 8 | @SpringBootApplication 9 | public class OrderApp { 10 | 11 | @Bean 12 | public RestTemplate restTemplate() { 13 | return new RestTemplate(); 14 | } 15 | 16 | public static void main(String[] args) { 17 | SpringApplication.run(OrderApp.class, args); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-customer/src/main/java/com/ewolff/microservice/customer/CustomerRepository.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.customer; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.data.repository.PagingAndSortingRepository; 6 | import org.springframework.data.repository.query.Param; 7 | import org.springframework.data.rest.core.annotation.RepositoryRestResource; 8 | 9 | @RepositoryRestResource(collectionResourceRel = "customer", path = "customer") 10 | public interface CustomerRepository extends 11 | PagingAndSortingRepository { 12 | 13 | List findByName(@Param("name") String name); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-catalog/src/main/java/com/ewolff/microservice/catalog/ItemRepository.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.catalog; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.data.repository.PagingAndSortingRepository; 6 | import org.springframework.data.repository.query.Param; 7 | import org.springframework.data.rest.core.annotation.RepositoryRestResource; 8 | 9 | @RepositoryRestResource(collectionResourceRel = "catalog", path = "catalog") 10 | public interface ItemRepository extends PagingAndSortingRepository { 11 | 12 | List findByName(@Param("name") String name); 13 | 14 | List findByNameContaining(@Param("name") String name); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-catalog/src/main/resources/templates/searchForm.html: -------------------------------------------------------------------------------- 1 | 4 | 5 | Item : Search 6 | 7 | 8 |

Item : Search

9 |
10 |
11 |
12 |
13 | 14 |
15 |
16 | 17 |
18 |
19 |
20 | 21 |
22 |
23 |
24 | 25 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-catalog/src/main/java/com/ewolff/microservice/catalog/SpringRestDataConfig.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.catalog; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.data.rest.core.config.RepositoryRestConfiguration; 6 | import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer; 7 | import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter; 8 | 9 | @Configuration 10 | class SpringRestDataConfig extends RepositoryRestConfigurerAdapter { 11 | 12 | @Bean 13 | public RepositoryRestConfigurer repositoryRestConfigurer() { 14 | 15 | return new RepositoryRestConfigurerAdapter() { 16 | @Override 17 | public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { 18 | config.exposeIdsFor(Item.class); 19 | } 20 | }; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-customer/src/main/java/com/ewolff/microservice/customer/SpringRestDataConfig.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.customer; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.data.rest.core.config.RepositoryRestConfiguration; 6 | import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer; 7 | import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter; 8 | 9 | @Configuration 10 | class SpringRestDataConfig extends RepositoryRestConfigurerAdapter { 11 | 12 | @Bean 13 | public RepositoryRestConfigurer repositoryRestConfigurer() { 14 | 15 | return new RepositoryRestConfigurerAdapter() { 16 | @Override 17 | public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { 18 | config.exposeIdsFor(Customer.class); 19 | } 20 | }; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-catalog/src/main/resources/templates/item.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | Item : Edit 7 | 8 | 9 |

Item : Edit

10 |
11 |
13 |
14 | 16 |
17 | 18 |
19 | 22 |
23 | 24 |
25 |
26 | 27 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-order/src/main/java/com/ewolff/microservice/order/logic/SpringRestDataConfig.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.order.logic; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.data.rest.core.config.RepositoryRestConfiguration; 6 | import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer; 7 | import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter; 8 | 9 | import com.ewolff.microservice.order.clients.Customer; 10 | import com.ewolff.microservice.order.clients.Item; 11 | 12 | @Configuration 13 | class SpringRestDataConfig extends RepositoryRestConfigurerAdapter { 14 | 15 | @Bean 16 | public RepositoryRestConfigurer repositoryRestConfigurer() { 17 | 18 | return new RepositoryRestConfigurerAdapter() { 19 | @Override 20 | public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { 21 | config.exposeIdsFor(Order.class, Item.class, Customer.class); 22 | } 23 | }; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /docker/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.7' 2 | services: 3 | consul: 4 | image: consul:1.3.0 5 | command: consul agent -dev -recursor=8.8.8.8 -client 0.0.0.0 6 | environment: 7 | - CONSUL_ALLOW_PRIVILEGED_PORTS 8 | ports: 9 | - "8500:8500" 10 | - "53:8600/udp" 11 | registrator: 12 | command: -internal consul://consul:8500 13 | image: gliderlabs/registrator:v7 14 | volumes: 15 | - "/var/run/docker.sock:/tmp/docker.sock" 16 | links: 17 | - consul 18 | customer: 19 | build: ../microservice-consuldns-demo/microservice-consuldns-demo-customer 20 | environment: 21 | - SERVICE_NAME=msconsuldns-customer 22 | catalog: 23 | build: ../microservice-consuldns-demo/microservice-consuldns-demo-catalog 24 | environment: 25 | - SERVICE_NAME=msconsuldns-catalog 26 | order: 27 | build: ../microservice-consuldns-demo/microservice-consuldns-demo-order 28 | dns: ${CONSUL_HOST} 29 | environment: 30 | - SERVICE_NAME=msconsuldns-order 31 | apache: 32 | build: apache 33 | links: 34 | - consul 35 | depends_on: 36 | - consul 37 | ports: 38 | - "8080:80" -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-order/src/main/resources/templates/layout.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Layout 6 | 8 | 10 | 14 | 15 | 16 | 17 |
18 | 24 |

Layout

25 |
Fake content
26 |
27 | 14 | 15 | 16 | 17 |

Order Processing

18 |
19 |
20 |
21 | Customer 22 |
23 |
List / add / remove customers
24 |
25 |
26 |
27 | Catalog 28 |
29 |
List / add / remove items
30 |
31 |
32 |
33 | Catalog 34 |
35 |
Search Items
36 |
37 |
38 |
39 | Order 40 |
41 |
Create an order
42 |
43 |
44 |
45 |
46 |
47 | Consul 48 |
49 |
Service discovery
50 |
51 |
52 | 53 | 54 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-catalog/src/test/java/com/ewolff/microservice/catalog/cdc/CatalogConsumerDrivenContractTest.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.catalog.cdc; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.util.Collection; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.boot.test.context.SpringBootTest; 11 | import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; 12 | import org.springframework.test.context.ActiveProfiles; 13 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 14 | 15 | import com.ewolff.microservice.catalog.CatalogApp; 16 | 17 | @RunWith(SpringJUnit4ClassRunner.class) 18 | @SpringBootTest(classes = CatalogApp.class, webEnvironment = WebEnvironment.DEFINED_PORT) 19 | @ActiveProfiles("test") 20 | public class CatalogConsumerDrivenContractTest { 21 | 22 | @Autowired 23 | private CatalogClient catalogClient; 24 | 25 | @Test 26 | public void testFindAll() { 27 | Collection result = catalogClient.findAll(); 28 | assertEquals( 29 | 1, 30 | result.stream() 31 | .filter(i -> (i.getName().equals("iPod") 32 | && i.getPrice() == 42.0 && i.getItemId() == 1)) 33 | .count()); 34 | } 35 | 36 | @Test 37 | public void testGetOne() { 38 | Collection allItems = catalogClient.findAll(); 39 | Long id = allItems.iterator().next().getItemId(); 40 | Item result = catalogClient.getOne(id); 41 | assertEquals(id.longValue(), result.getItemId()); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-customer/src/main/resources/templates/customer.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | Customer : Edit 7 | 8 | 9 |

Customer : Edit

10 |
11 |
13 |
14 | 16 |
17 | 18 |
19 | 22 |
23 | 24 |
25 | 28 |
29 | 30 |
31 | 34 |
35 | 36 |
37 | 40 |
41 | 42 | 43 |
44 |
45 | 46 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-catalog/src/test/java/com/ewolff/microservice/catalog/cdc/Item.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.catalog.cdc; 2 | 3 | import org.apache.commons.lang.builder.EqualsBuilder; 4 | import org.apache.commons.lang.builder.HashCodeBuilder; 5 | import org.apache.commons.lang.builder.ToStringBuilder; 6 | import org.springframework.hateoas.ResourceSupport; 7 | 8 | import com.fasterxml.jackson.annotation.JsonProperty; 9 | 10 | public class Item extends ResourceSupport { 11 | 12 | private String name; 13 | 14 | private double price; 15 | 16 | @JsonProperty("id") 17 | private long itemId; 18 | 19 | public Item() { 20 | super(); 21 | } 22 | 23 | public Item(long id, String name, double price) { 24 | super(); 25 | this.itemId = id; 26 | this.name = name; 27 | this.price = price; 28 | } 29 | 30 | public String getName() { 31 | return name; 32 | } 33 | 34 | public void setName(String name) { 35 | this.name = name; 36 | } 37 | 38 | public double getPrice() { 39 | return price; 40 | } 41 | 42 | public void setPrice(double price) { 43 | this.price = price; 44 | } 45 | 46 | public long getItemId() { 47 | return itemId; 48 | } 49 | 50 | public void setItemId(long id) { 51 | this.itemId = id; 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | return ToStringBuilder.reflectionToString(this); 57 | } 58 | 59 | @Override 60 | public int hashCode() { 61 | return HashCodeBuilder.reflectionHashCode(this); 62 | 63 | } 64 | 65 | @Override 66 | public boolean equals(Object obj) { 67 | return EqualsBuilder.reflectionEquals(this, obj); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-order/src/main/java/com/ewolff/microservice/order/clients/Item.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.order.clients; 2 | 3 | import org.apache.commons.lang.builder.EqualsBuilder; 4 | import org.apache.commons.lang.builder.HashCodeBuilder; 5 | import org.apache.commons.lang.builder.ToStringBuilder; 6 | import org.springframework.hateoas.ResourceSupport; 7 | 8 | import com.fasterxml.jackson.annotation.JsonProperty; 9 | 10 | public class Item extends ResourceSupport { 11 | 12 | private String name; 13 | 14 | private double price; 15 | 16 | @JsonProperty("id") 17 | private long itemId; 18 | 19 | public Item() { 20 | super(); 21 | } 22 | 23 | public Item(long id, String name, double price) { 24 | super(); 25 | this.itemId = id; 26 | this.name = name; 27 | this.price = price; 28 | } 29 | 30 | public String getName() { 31 | return name; 32 | } 33 | 34 | public void setName(String name) { 35 | this.name = name; 36 | } 37 | 38 | public double getPrice() { 39 | return price; 40 | } 41 | 42 | public void setPrice(double price) { 43 | this.price = price; 44 | } 45 | 46 | public long getItemId() { 47 | return itemId; 48 | } 49 | 50 | public void setItemId(long id) { 51 | this.itemId = id; 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | return ToStringBuilder.reflectionToString(this); 57 | } 58 | 59 | @Override 60 | public int hashCode() { 61 | return HashCodeBuilder.reflectionHashCode(this); 62 | 63 | } 64 | 65 | @Override 66 | public boolean equals(Object obj) { 67 | return EqualsBuilder.reflectionEquals(this, obj); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-order/src/test/java/com/ewolff/microservice/order/logic/CatalogConsumerDrivenContractTest.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.order.logic; 2 | 3 | import com.ewolff.microservice.order.OrderApp; 4 | import com.ewolff.microservice.order.clients.CatalogClient; 5 | import com.ewolff.microservice.order.clients.Item; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.boot.test.context.SpringBootTest; 10 | import org.springframework.test.context.ActiveProfiles; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | 13 | import java.util.Collection; 14 | 15 | import static org.junit.Assert.assertEquals; 16 | 17 | @RunWith(SpringJUnit4ClassRunner.class) 18 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, classes = OrderApp.class) 19 | @ActiveProfiles("test") 20 | public class CatalogConsumerDrivenContractTest { 21 | 22 | @Autowired 23 | CatalogClient catalogClient; 24 | 25 | @Test 26 | public void testFindAll() { 27 | Collection result = catalogClient.findAll(); 28 | assertEquals( 29 | 1, 30 | result.stream() 31 | .filter(i -> (i.getName().equals("iPod") 32 | && i.getPrice() == 42.0 && i.getItemId() == 1)) 33 | .count()); 34 | } 35 | 36 | @Test 37 | public void testGetOne() { 38 | Collection allItems = catalogClient.findAll(); 39 | Long id = allItems.iterator().next().getItemId(); 40 | Item result = catalogClient.getOne(id); 41 | assertEquals(id.longValue(), result.getItemId()); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-order/src/test/java/com/ewolff/microservice/order/customerstub/CustomerStub.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.order.customerstub; 2 | 3 | import java.util.Arrays; 4 | 5 | import org.springframework.context.annotation.Profile; 6 | import org.springframework.hateoas.PagedResources; 7 | import org.springframework.hateoas.PagedResources.PageMetadata; 8 | import org.springframework.http.HttpStatus; 9 | import org.springframework.http.ResponseEntity; 10 | import org.springframework.web.bind.annotation.PathVariable; 11 | import org.springframework.web.bind.annotation.RequestMapping; 12 | import org.springframework.web.bind.annotation.RequestMethod; 13 | import org.springframework.web.bind.annotation.RestController; 14 | 15 | import com.ewolff.microservice.order.clients.Customer; 16 | 17 | @RestController 18 | @RequestMapping("/customer") 19 | @Profile("test") 20 | public class CustomerStub { 21 | 22 | @RequestMapping(value = "/{id}", method = RequestMethod.GET) 23 | public ResponseEntity getById(@PathVariable("id") long id) { 24 | 25 | if (id != 42) { 26 | return new ResponseEntity(HttpStatus.NOT_FOUND); 27 | } 28 | return new ResponseEntity(new Customer(42, "Eberhard", 29 | "Wolff", "eberhard.wolff@gmail.com", "Unter den Linden", 30 | "Berlin"), HttpStatus.OK); 31 | } 32 | 33 | @RequestMapping(method = RequestMethod.GET) 34 | public PagedResources getAll() { 35 | return new PagedResources(Arrays.asList(new Customer(42, 36 | "Eberhard", "Wolff", "eberhard.wolff@gmail.com", 37 | "Unter den Linden", "Berlin")), new PageMetadata(1, 0, 1)); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-catalog/src/main/java/com/ewolff/microservice/catalog/Item.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.catalog; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.Id; 7 | 8 | import org.apache.commons.lang.builder.EqualsBuilder; 9 | import org.apache.commons.lang.builder.HashCodeBuilder; 10 | import org.apache.commons.lang.builder.ToStringBuilder; 11 | 12 | @Entity 13 | public class Item { 14 | 15 | @Id 16 | @GeneratedValue 17 | private Long id; 18 | 19 | @Column(nullable = false) 20 | private String name; 21 | 22 | @Column(nullable = false) 23 | private double price; 24 | 25 | public Item() { 26 | super(); 27 | id = 0l; 28 | } 29 | 30 | public Item(String name, double price) { 31 | super(); 32 | this.name = name; 33 | this.price = price; 34 | } 35 | 36 | public String getName() { 37 | return name; 38 | } 39 | 40 | public void setName(String name) { 41 | this.name = name; 42 | } 43 | 44 | public double getPrice() { 45 | return price; 46 | } 47 | 48 | public void setPrice(double price) { 49 | this.price = price; 50 | } 51 | 52 | public Long getId() { 53 | return id; 54 | } 55 | 56 | public void setId(Long id) { 57 | this.id = id; 58 | } 59 | 60 | @Override 61 | public String toString() { 62 | return ToStringBuilder.reflectionToString(this); 63 | } 64 | 65 | @Override 66 | public int hashCode() { 67 | return HashCodeBuilder.reflectionHashCode(this); 68 | 69 | } 70 | 71 | @Override 72 | public boolean equals(Object obj) { 73 | return EqualsBuilder.reflectionEquals(this, obj); 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-order/src/main/resources/templates/orderForm.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | Order : Add 7 | 8 | 9 |

Order : Add

10 |
11 |
12 |
13 | 19 |
20 |
21 |
22 | 23 |
24 |
25 | 26 |
27 |
28 | 29 |
30 |
31 |
33 |
1
34 |
35 | 37 |
38 |
39 | 44 |
45 |
46 |
47 | 49 | 50 |
51 |
52 |
53 | 54 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-customer/src/test/java/com/ewolff/microservice/customer/cdc/CustomerConsumerDrivenContractTest.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.customer.cdc; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.util.Collection; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.boot.test.context.SpringBootTest; 11 | import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; 12 | import org.springframework.test.context.ActiveProfiles; 13 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 14 | 15 | import com.ewolff.microservice.customer.CustomerApp; 16 | 17 | @RunWith(SpringJUnit4ClassRunner.class) 18 | @SpringBootTest(classes = CustomerApp.class, webEnvironment = WebEnvironment.DEFINED_PORT) 19 | @ActiveProfiles("test") 20 | public class CustomerConsumerDrivenContractTest { 21 | 22 | @Autowired 23 | CustomerClient customerClient; 24 | 25 | @Test 26 | public void testFindAll() { 27 | Collection result = customerClient.findAll(); 28 | assertEquals( 29 | 1, 30 | result.stream() 31 | .filter(c -> (c.getName().equals("Wolff") 32 | && c.getFirstname().equals("Eberhard") 33 | && c.getEmail().equals( 34 | "eberhard.wolff@gmail.com") 35 | && c.getStreet().equals("Unter den Linden") && c 36 | .getCity().equals("Berlin"))) 37 | .count()); 38 | } 39 | 40 | @Test 41 | public void testGetOne() { 42 | Collection allCustomer = customerClient.findAll(); 43 | Long id = allCustomer.iterator().next().getCustomerId(); 44 | Customer result = customerClient.getOne(id); 45 | assertEquals(id.longValue(), result.getCustomerId()); 46 | } 47 | 48 | @Test 49 | public void testValidCustomerId() { 50 | Collection allCustomer = customerClient.findAll(); 51 | Long id = allCustomer.iterator().next().getCustomerId(); 52 | assertTrue(customerClient.isValidCustomerId(id)); 53 | assertFalse(customerClient.isValidCustomerId(-1)); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-order/src/test/java/com/ewolff/microservice/order/logic/CustomerConsumerDrivenContractTest.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.order.logic; 2 | 3 | import com.ewolff.microservice.order.OrderApp; 4 | import com.ewolff.microservice.order.clients.Customer; 5 | import com.ewolff.microservice.order.clients.CustomerClient; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.boot.test.context.SpringBootTest; 10 | import org.springframework.test.context.ActiveProfiles; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | 13 | import java.util.Collection; 14 | 15 | import static org.junit.Assert.*; 16 | 17 | @RunWith(SpringJUnit4ClassRunner.class) 18 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, classes = OrderApp.class) 19 | @ActiveProfiles("test") 20 | public class CustomerConsumerDrivenContractTest { 21 | 22 | @Autowired 23 | CustomerClient customerClient; 24 | 25 | @Test 26 | public void testFindAll() { 27 | Collection result = customerClient.findAll(); 28 | assertEquals( 29 | 1, 30 | result.stream() 31 | .filter(c -> (c.getName().equals("Wolff") 32 | && c.getFirstname().equals("Eberhard") 33 | && c.getEmail().equals( 34 | "eberhard.wolff@gmail.com") 35 | && c.getStreet().equals("Unter den Linden") && c 36 | .getCity().equals("Berlin"))).count()); 37 | } 38 | 39 | @Test 40 | public void testGetOne() { 41 | Collection allCustomer = customerClient.findAll(); 42 | Long id = allCustomer.iterator().next().getCustomerId(); 43 | Customer result = customerClient.getOne(id); 44 | assertEquals(id.longValue(), result.getCustomerId()); 45 | } 46 | 47 | @Test 48 | public void testValidCustomerId() { 49 | Collection allCustomer = customerClient.findAll(); 50 | Long id = allCustomer.iterator().next().getCustomerId(); 51 | assertTrue(customerClient.isValidCustomerId(id)); 52 | assertFalse(customerClient.isValidCustomerId(-1)); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-order/src/main/resources/static/monitor.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Monitoring 5 | 6 | 7 | 8 |

Monitoring Links

9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 21 | 22 | 23 | 24 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 60 | 61 | 62 | 63 | 65 | 66 | 67 |
IDDescription
autoconfigDisplays an auto-configuration report showing all 19 | auto-configuration candidates and the reason why they were or were 20 | not applied.
beansDisplays a complete list of all the Spring Beans in your 25 | application.
configpropsDisplays a collated list of all @ConfigurationProperties.
dumpPerforms a thread dump.
envExposes properties from Spring's ConfigurableEnvironment.
healthShows application health information (defaulting to a 42 | simple OK message).
infoDisplays arbitrary application info.
metricsShows metrics information for the current application.
mappingsDisplays a collated list of all @RequestMapping paths.
shutdownAllows the application to be gracefully shutdown (not 59 | enabled by default).
traceDisplays trace information (by default the last few HTTP 64 | requests).
68 |
69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-order/src/main/java/com/ewolff/microservice/order/clients/Customer.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.order.clients; 2 | 3 | import org.apache.commons.lang.builder.EqualsBuilder; 4 | import org.apache.commons.lang.builder.HashCodeBuilder; 5 | import org.apache.commons.lang.builder.ToStringBuilder; 6 | import org.springframework.hateoas.ResourceSupport; 7 | 8 | import com.fasterxml.jackson.annotation.JsonProperty; 9 | 10 | public class Customer extends ResourceSupport { 11 | 12 | private String name; 13 | 14 | private String firstname; 15 | 16 | private String email; 17 | 18 | private String street; 19 | 20 | private String city; 21 | 22 | @JsonProperty("id") 23 | private long customerId; 24 | 25 | public Customer() { 26 | } 27 | 28 | public Customer(long id, String firstname, String name, String email, 29 | String street, String city) { 30 | super(); 31 | this.customerId = id; 32 | this.firstname = firstname; 33 | this.name = name; 34 | this.email = email; 35 | this.street = street; 36 | this.city = city; 37 | } 38 | 39 | public long getCustomerId() { 40 | return customerId; 41 | } 42 | 43 | public void setCustomerId(long id) { 44 | this.customerId = id; 45 | } 46 | 47 | public String getName() { 48 | return name; 49 | } 50 | 51 | public void setName(String name) { 52 | this.name = name; 53 | } 54 | 55 | public String getFirstname() { 56 | return firstname; 57 | } 58 | 59 | public void setFirstname(String firstname) { 60 | this.firstname = firstname; 61 | } 62 | 63 | public String getEmail() { 64 | return email; 65 | } 66 | 67 | public void setEmail(String email) { 68 | this.email = email; 69 | } 70 | 71 | public String getStreet() { 72 | return street; 73 | } 74 | 75 | public void setStreet(String street) { 76 | this.street = street; 77 | } 78 | 79 | public String getCity() { 80 | return city; 81 | } 82 | 83 | public void setCity(String city) { 84 | this.city = city; 85 | } 86 | 87 | @Override 88 | public String toString() { 89 | return ToStringBuilder.reflectionToString(this); 90 | } 91 | 92 | @Override 93 | public int hashCode() { 94 | return HashCodeBuilder.reflectionHashCode(this); 95 | 96 | } 97 | 98 | @Override 99 | public boolean equals(Object obj) { 100 | return EqualsBuilder.reflectionEquals(this, obj); 101 | } 102 | 103 | } 104 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-customer/src/main/java/com/ewolff/microservice/customer/web/CustomerController.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.customer.web; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.MediaType; 7 | import org.springframework.stereotype.Controller; 8 | import org.springframework.web.bind.annotation.PathVariable; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.bind.annotation.RequestMethod; 11 | import org.springframework.web.servlet.ModelAndView; 12 | 13 | import com.ewolff.microservice.customer.Customer; 14 | import com.ewolff.microservice.customer.CustomerRepository; 15 | 16 | @Controller 17 | public class CustomerController { 18 | 19 | private CustomerRepository customerRepository; 20 | 21 | @Autowired 22 | public CustomerController(CustomerRepository customerRepository) { 23 | this.customerRepository = customerRepository; 24 | } 25 | 26 | @RequestMapping(value = "/{id}.html", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE) 27 | public ModelAndView customer(@PathVariable("id") long id) { 28 | return new ModelAndView("customer", "customer", 29 | customerRepository.findById(id).get()); 30 | } 31 | 32 | @RequestMapping("/list.html") 33 | public ModelAndView customerList() { 34 | return new ModelAndView("customerlist", "customers", 35 | customerRepository.findAll()); 36 | } 37 | 38 | @RequestMapping(value = "/form.html", method = RequestMethod.GET) 39 | public ModelAndView add() { 40 | return new ModelAndView("customer", "customer", new Customer()); 41 | } 42 | 43 | @RequestMapping(value = "/form.html", method = RequestMethod.POST) 44 | public ModelAndView post(Customer customer, HttpServletRequest httpRequest) { 45 | customer = customerRepository.save(customer); 46 | return new ModelAndView("success"); 47 | } 48 | 49 | @RequestMapping(value = "/{id}.html", method = RequestMethod.PUT) 50 | public ModelAndView put(@PathVariable("id") long id, Customer customer, 51 | HttpServletRequest httpRequest) { 52 | customer.setId(id); 53 | customerRepository.save(customer); 54 | return new ModelAndView("success"); 55 | } 56 | 57 | @RequestMapping(value = "/{id}.html", method = RequestMethod.DELETE) 58 | public ModelAndView delete(@PathVariable("id") long id) { 59 | customerRepository.deleteById(id); 60 | return new ModelAndView("success"); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-customer/src/test/java/com/ewolff/microservice/customer/cdc/Customer.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.customer.cdc; 2 | 3 | import javax.xml.bind.annotation.XmlRootElement; 4 | 5 | import org.apache.commons.lang.builder.EqualsBuilder; 6 | import org.apache.commons.lang.builder.HashCodeBuilder; 7 | import org.apache.commons.lang.builder.ToStringBuilder; 8 | import org.springframework.hateoas.ResourceSupport; 9 | 10 | import com.fasterxml.jackson.annotation.JsonProperty; 11 | 12 | @XmlRootElement 13 | public class Customer extends ResourceSupport { 14 | 15 | private String name; 16 | 17 | private String firstname; 18 | 19 | private String email; 20 | 21 | private String street; 22 | 23 | private String city; 24 | 25 | @JsonProperty("id") 26 | private long customerId; 27 | 28 | public Customer() { 29 | } 30 | 31 | public Customer(long id, String firstname, String name, String email, 32 | String street, String city) { 33 | super(); 34 | this.customerId = id; 35 | this.firstname = firstname; 36 | this.name = name; 37 | this.email = email; 38 | this.street = street; 39 | this.city = city; 40 | } 41 | 42 | public long getCustomerId() { 43 | return customerId; 44 | } 45 | 46 | public void setCustomerId(long id) { 47 | this.customerId = id; 48 | } 49 | 50 | public String getName() { 51 | return name; 52 | } 53 | 54 | public void setName(String name) { 55 | this.name = name; 56 | } 57 | 58 | public String getFirstname() { 59 | return firstname; 60 | } 61 | 62 | public void setFirstname(String firstname) { 63 | this.firstname = firstname; 64 | } 65 | 66 | public String getEmail() { 67 | return email; 68 | } 69 | 70 | public void setEmail(String email) { 71 | this.email = email; 72 | } 73 | 74 | public String getStreet() { 75 | return street; 76 | } 77 | 78 | public void setStreet(String street) { 79 | this.street = street; 80 | } 81 | 82 | public String getCity() { 83 | return city; 84 | } 85 | 86 | public void setCity(String city) { 87 | this.city = city; 88 | } 89 | 90 | @Override 91 | public String toString() { 92 | return ToStringBuilder.reflectionToString(this); 93 | } 94 | 95 | @Override 96 | public int hashCode() { 97 | return HashCodeBuilder.reflectionHashCode(this); 98 | 99 | } 100 | 101 | @Override 102 | public boolean equals(Object obj) { 103 | return EqualsBuilder.reflectionEquals(this, obj); 104 | } 105 | 106 | } 107 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-order/src/main/java/com/ewolff/microservice/order/logic/Order.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.order.logic; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import javax.persistence.CascadeType; 7 | import javax.persistence.Entity; 8 | import javax.persistence.GeneratedValue; 9 | import javax.persistence.Id; 10 | import javax.persistence.OneToMany; 11 | import javax.persistence.Table; 12 | 13 | import org.apache.commons.lang.builder.EqualsBuilder; 14 | import org.apache.commons.lang.builder.HashCodeBuilder; 15 | import org.apache.commons.lang.builder.ToStringBuilder; 16 | 17 | import com.ewolff.microservice.order.clients.CatalogClient; 18 | 19 | @Entity 20 | @Table(name = "ORDERTABLE") 21 | class Order { 22 | 23 | @Id 24 | @GeneratedValue 25 | private long id; 26 | 27 | private long customerId; 28 | 29 | @OneToMany(cascade = CascadeType.ALL) 30 | private List orderLine; 31 | 32 | public Order() { 33 | super(); 34 | orderLine = new ArrayList(); 35 | } 36 | 37 | public void setId(long id) { 38 | this.id = id; 39 | } 40 | 41 | public long getId() { 42 | return id; 43 | } 44 | 45 | public long getCustomerId() { 46 | return customerId; 47 | } 48 | 49 | public void setCustomerId(long customerId) { 50 | this.customerId = customerId; 51 | } 52 | 53 | public List getOrderLine() { 54 | return orderLine; 55 | } 56 | 57 | public Order(long customerId) { 58 | super(); 59 | this.customerId = customerId; 60 | this.orderLine = new ArrayList(); 61 | } 62 | 63 | public void setOrderLine(List orderLine) { 64 | this.orderLine = orderLine; 65 | } 66 | 67 | public void addLine(int count, long itemId) { 68 | this.orderLine.add(new OrderLine(count, itemId)); 69 | } 70 | 71 | public int getNumberOfLines() { 72 | return orderLine.size(); 73 | } 74 | 75 | public double totalPrice(CatalogClient itemClient) { 76 | return orderLine.stream() 77 | .map((ol) -> ol.getCount() * itemClient.price(ol.getItemId())) 78 | .reduce(0.0, (d1, d2) -> d1 + d2); 79 | } 80 | 81 | public void setCustomer(long customerId) { 82 | this.customerId = customerId; 83 | } 84 | 85 | @Override 86 | public String toString() { 87 | return ToStringBuilder.reflectionToString(this); 88 | } 89 | 90 | @Override 91 | public int hashCode() { 92 | return HashCodeBuilder.reflectionHashCode(this); 93 | 94 | } 95 | 96 | @Override 97 | public boolean equals(Object obj) { 98 | return EqualsBuilder.reflectionEquals(this, obj); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-customer/src/main/java/com/ewolff/microservice/customer/Customer.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.customer; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.Id; 7 | import javax.validation.constraints.Email; 8 | 9 | import org.apache.commons.lang.builder.EqualsBuilder; 10 | import org.apache.commons.lang.builder.HashCodeBuilder; 11 | import org.apache.commons.lang.builder.ToStringBuilder; 12 | 13 | @Entity 14 | public class Customer { 15 | 16 | @Id 17 | @GeneratedValue 18 | private Long id; 19 | 20 | @Column(nullable = false) 21 | private String name; 22 | 23 | @Column(nullable = false) 24 | private String firstname; 25 | 26 | @Column(nullable = false) 27 | @Email 28 | private String email; 29 | 30 | @Column(nullable = false) 31 | private String street; 32 | 33 | @Column(nullable = false) 34 | private String city; 35 | 36 | public Customer() { 37 | super(); 38 | id = 0l; 39 | } 40 | 41 | public Customer(String firstname, String name, String email, String street, 42 | String city) { 43 | super(); 44 | this.name = name; 45 | this.firstname = firstname; 46 | this.email = email; 47 | this.street = street; 48 | this.city = city; 49 | } 50 | 51 | public String getEmail() { 52 | return email; 53 | } 54 | 55 | public void setEmail(String email) { 56 | this.email = email; 57 | } 58 | 59 | public String getName() { 60 | return name; 61 | } 62 | 63 | public void setName(String name) { 64 | this.name = name; 65 | } 66 | 67 | public String getFirstname() { 68 | return firstname; 69 | } 70 | 71 | public void setFirstname(String firstname) { 72 | this.firstname = firstname; 73 | } 74 | 75 | public Long getId() { 76 | return id; 77 | } 78 | 79 | public void setId(Long id) { 80 | this.id = id; 81 | } 82 | 83 | public String getStreet() { 84 | return street; 85 | } 86 | 87 | public void setStreet(String street) { 88 | this.street = street; 89 | } 90 | 91 | public String getCity() { 92 | return city; 93 | } 94 | 95 | public void setCity(String city) { 96 | this.city = city; 97 | } 98 | 99 | @Override 100 | public String toString() { 101 | return ToStringBuilder.reflectionToString(this); 102 | } 103 | 104 | @Override 105 | public int hashCode() { 106 | return HashCodeBuilder.reflectionHashCode(this); 107 | 108 | } 109 | 110 | @Override 111 | public boolean equals(Object obj) { 112 | return EqualsBuilder.reflectionEquals(this, obj); 113 | } 114 | 115 | } 116 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-catalog/src/test/java/com/ewolff/microservice/catalog/cdc/CatalogClient.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.catalog.cdc; 2 | 3 | import java.util.Arrays; 4 | import java.util.Collection; 5 | import java.util.Collections; 6 | 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.beans.factory.annotation.Value; 9 | import org.springframework.hateoas.MediaTypes; 10 | import org.springframework.hateoas.PagedResources; 11 | import org.springframework.hateoas.hal.Jackson2HalModule; 12 | import org.springframework.http.converter.HttpMessageConverter; 13 | import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; 14 | import org.springframework.stereotype.Component; 15 | import org.springframework.web.client.RestTemplate; 16 | 17 | import com.fasterxml.jackson.databind.DeserializationFeature; 18 | import com.fasterxml.jackson.databind.ObjectMapper; 19 | 20 | @Component 21 | public class CatalogClient { 22 | 23 | public static class ItemPagedResources extends PagedResources { 24 | } 25 | 26 | private final RestTemplate restTemplate; 27 | private final String catalogServiceHost; 28 | private final long catalogServicePort; 29 | 30 | @Autowired 31 | public CatalogClient( 32 | @Value("${catalog.service.host:catalog}") String catalogServiceHost, 33 | @Value("${catalog.service.port:8080}") long catalogServicePort) { 34 | super(); 35 | this.restTemplate = getRestTemplate(); 36 | this.catalogServiceHost = catalogServiceHost; 37 | this.catalogServicePort = catalogServicePort; 38 | } 39 | 40 | protected RestTemplate getRestTemplate() { 41 | ObjectMapper mapper = new ObjectMapper(); 42 | mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, 43 | false); 44 | mapper.registerModule(new Jackson2HalModule()); 45 | 46 | MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); 47 | converter.setSupportedMediaTypes(Arrays.asList(MediaTypes.HAL_JSON)); 48 | converter.setObjectMapper(mapper); 49 | 50 | return new RestTemplate( 51 | Collections.>singletonList(converter)); 52 | } 53 | 54 | public double price(long itemId) { 55 | return getOne(itemId).getPrice(); 56 | } 57 | 58 | public Collection findAll() { 59 | PagedResources pagedResources = restTemplate.getForObject( 60 | catalogURL(), ItemPagedResources.class); 61 | return pagedResources.getContent(); 62 | } 63 | 64 | private String catalogURL() { 65 | return "http://" + catalogServiceHost + ":" + catalogServicePort 66 | + "/catalog/"; 67 | } 68 | 69 | public Item getOne(long itemId) { 70 | return restTemplate.getForObject(catalogURL() + itemId, Item.class); 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-catalog/src/main/java/com/ewolff/microservice/catalog/web/CatalogController.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.catalog.web; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.http.MediaType; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RequestMethod; 9 | import org.springframework.web.bind.annotation.RequestParam; 10 | import org.springframework.web.servlet.ModelAndView; 11 | 12 | import com.ewolff.microservice.catalog.Item; 13 | import com.ewolff.microservice.catalog.ItemRepository; 14 | 15 | @Controller 16 | public class CatalogController { 17 | 18 | private final ItemRepository itemRepository; 19 | 20 | @Autowired 21 | public CatalogController(ItemRepository itemRepository) { 22 | this.itemRepository = itemRepository; 23 | } 24 | 25 | @RequestMapping(value = "/{id}.html", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE) 26 | public ModelAndView Item(@PathVariable("id") long id) { 27 | return new ModelAndView("item", "item", itemRepository.findById(id).get()); 28 | } 29 | 30 | @RequestMapping("/list.html") 31 | public ModelAndView ItemList() { 32 | return new ModelAndView("itemlist", "items", itemRepository.findAll()); 33 | } 34 | 35 | @RequestMapping(value = "/form.html", method = RequestMethod.GET) 36 | public ModelAndView add() { 37 | return new ModelAndView("item", "item", new Item()); 38 | } 39 | 40 | @RequestMapping(value = "/form.html", method = RequestMethod.POST) 41 | public ModelAndView post(Item Item) { 42 | Item = itemRepository.save(Item); 43 | return new ModelAndView("success"); 44 | } 45 | 46 | @RequestMapping(value = "/{id}.html", method = RequestMethod.PUT) 47 | public ModelAndView put(@PathVariable("id") long id, Item item) { 48 | item.setId(id); 49 | itemRepository.save(item); 50 | return new ModelAndView("success"); 51 | } 52 | 53 | @RequestMapping(value = "/searchForm.html", produces = MediaType.TEXT_HTML_VALUE) 54 | public ModelAndView searchForm() { 55 | return new ModelAndView("searchForm"); 56 | } 57 | 58 | @RequestMapping(value = "/searchByName.html", produces = MediaType.TEXT_HTML_VALUE) 59 | public ModelAndView search(@RequestParam("query") String query) { 60 | return new ModelAndView("itemlist", "items", 61 | itemRepository.findByNameContaining(query)); 62 | } 63 | 64 | @RequestMapping(value = "/{id}.html", method = RequestMethod.DELETE) 65 | public ModelAndView delete(@PathVariable("id") long id) { 66 | itemRepository.deleteById(id); 67 | return new ModelAndView("success"); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-order/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 4.0.0 5 | 6 | 7 | com.ewolff 8 | microservice-consuldns-demo 9 | 0.0.1-SNAPSHOT 10 | 11 | 12 | microservice-consuldns-demo-order 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-data-jpa 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-data-rest 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-thymeleaf 29 | 30 | 31 | 32 | nz.net.ultraq.thymeleaf 33 | thymeleaf-layout-dialect 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-actuator 39 | 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-test 44 | test 45 | 46 | 47 | 48 | org.springframework.hateoas 49 | spring-hateoas 50 | 51 | 52 | 53 | javax.xml.bind 54 | jaxb-api 55 | 56 | 57 | 58 | com.jayway.jsonpath 59 | json-path 60 | runtime 61 | 62 | 63 | 64 | org.hsqldb 65 | hsqldb 66 | runtime 67 | 68 | 69 | 70 | commons-lang 71 | commons-lang 72 | 2.6 73 | 74 | 75 | 76 | org.webjars 77 | bootstrap 78 | 3.3.6 79 | 80 | 81 | 82 | org.springframework.boot 83 | spring-boot-devtools 84 | true 85 | 86 | 87 | 88 | net.logstash.logback 89 | logstash-logback-encoder 90 | 4.11 91 | 92 | 93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-order/src/main/java/com/ewolff/microservice/order/clients/CatalogClient.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.order.clients; 2 | 3 | import java.util.Arrays; 4 | import java.util.Collection; 5 | import java.util.Collections; 6 | 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.beans.factory.annotation.Value; 11 | import org.springframework.hateoas.MediaTypes; 12 | import org.springframework.hateoas.PagedResources; 13 | import org.springframework.hateoas.hal.Jackson2HalModule; 14 | import org.springframework.http.converter.HttpMessageConverter; 15 | import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; 16 | import org.springframework.stereotype.Component; 17 | import org.springframework.web.client.RestTemplate; 18 | 19 | import com.fasterxml.jackson.databind.DeserializationFeature; 20 | import com.fasterxml.jackson.databind.ObjectMapper; 21 | 22 | @Component 23 | public class CatalogClient { 24 | 25 | private final Logger log = LoggerFactory.getLogger(CatalogClient.class); 26 | 27 | public static class ItemPagedResources extends PagedResources { 28 | 29 | } 30 | 31 | private RestTemplate restTemplate; 32 | private String catalogServiceHost; 33 | private long catalogServicePort; 34 | 35 | @Autowired 36 | public CatalogClient( 37 | @Value("${catalog.service.host:msconsuldns-catalog.service.consul}") String catalogServiceHost, 38 | @Value("${catalog.service.port:8080}") long catalogServicePort, 39 | @Autowired RestTemplate restTemplate) { 40 | super(); 41 | this.restTemplate = restTemplate; 42 | this.catalogServiceHost = catalogServiceHost; 43 | this.catalogServicePort = catalogServicePort; 44 | } 45 | 46 | protected RestTemplate getRestTemplate() { 47 | ObjectMapper mapper = new ObjectMapper(); 48 | mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 49 | mapper.registerModule(new Jackson2HalModule()); 50 | 51 | MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); 52 | converter.setSupportedMediaTypes(Arrays.asList(MediaTypes.HAL_JSON)); 53 | converter.setObjectMapper(mapper); 54 | 55 | return new RestTemplate(Collections.>singletonList(converter)); 56 | } 57 | 58 | public double price(long itemId) { 59 | return getOne(itemId).getPrice(); 60 | } 61 | 62 | public Collection findAll() { 63 | PagedResources pagedResources = restTemplate.getForObject(catalogURL(), ItemPagedResources.class); 64 | return pagedResources.getContent(); 65 | } 66 | 67 | private String catalogURL() { 68 | String url; 69 | url = String.format("http://%s:%s/catalog/", catalogServiceHost, catalogServicePort); 70 | log.trace("Catalog: URL {} ", url); 71 | return url; 72 | } 73 | 74 | public Item getOne(long itemId) { 75 | return restTemplate.getForObject(catalogURL() + itemId, Item.class); 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-order/src/main/java/com/ewolff/microservice/order/logic/OrderController.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.order.logic; 2 | 3 | import java.util.Collection; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Controller; 7 | import org.springframework.web.bind.annotation.ModelAttribute; 8 | import org.springframework.web.bind.annotation.PathVariable; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.bind.annotation.RequestMethod; 11 | import org.springframework.web.servlet.ModelAndView; 12 | 13 | import com.ewolff.microservice.order.clients.CatalogClient; 14 | import com.ewolff.microservice.order.clients.Customer; 15 | import com.ewolff.microservice.order.clients.CustomerClient; 16 | import com.ewolff.microservice.order.clients.Item; 17 | 18 | @Controller 19 | class OrderController { 20 | 21 | private OrderRepository orderRepository; 22 | 23 | private OrderService orderService; 24 | 25 | private CustomerClient customerClient; 26 | private CatalogClient catalogClient; 27 | 28 | @Autowired 29 | private OrderController(OrderService orderService, 30 | OrderRepository orderRepository, CustomerClient customerClient, 31 | CatalogClient catalogClient) { 32 | super(); 33 | this.orderRepository = orderRepository; 34 | this.customerClient = customerClient; 35 | this.catalogClient = catalogClient; 36 | this.orderService = orderService; 37 | } 38 | 39 | @ModelAttribute("items") 40 | public Collection items() { 41 | return catalogClient.findAll(); 42 | } 43 | 44 | @ModelAttribute("customers") 45 | public Collection customers() { 46 | return customerClient.findAll(); 47 | } 48 | 49 | @RequestMapping("/") 50 | public ModelAndView orderList() { 51 | return new ModelAndView("orderlist", "orders", 52 | orderRepository.findAll()); 53 | } 54 | 55 | @RequestMapping(value = "/form.html", method = RequestMethod.GET) 56 | public ModelAndView form() { 57 | return new ModelAndView("orderForm", "order", new Order()); 58 | } 59 | 60 | @RequestMapping(value = "/line", method = RequestMethod.POST) 61 | public ModelAndView addLine(Order order) { 62 | order.addLine(0, catalogClient.findAll().iterator().next().getItemId()); 63 | return new ModelAndView("orderForm", "order", order); 64 | } 65 | 66 | @RequestMapping(value = "/{id}", method = RequestMethod.GET) 67 | public ModelAndView get(@PathVariable("id") long id) { 68 | return new ModelAndView("order", "order", orderRepository.findById(id).get()); 69 | } 70 | 71 | @RequestMapping(value = "/", method = RequestMethod.POST) 72 | public ModelAndView post(Order order) { 73 | order = orderService.order(order); 74 | return new ModelAndView("success"); 75 | } 76 | 77 | @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) 78 | public ModelAndView post(@PathVariable("id") long id) { 79 | orderRepository.deleteById(id); 80 | 81 | return new ModelAndView("success"); 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-catalog/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 4.0.0 5 | 6 | 7 | com.ewolff 8 | microservice-consuldns-demo 9 | 0.0.1-SNAPSHOT 10 | 11 | 12 | microservice-consuldns-demo-catalog 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-data-jpa 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-data-rest 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-thymeleaf 28 | 29 | 30 | 31 | nz.net.ultraq.thymeleaf 32 | thymeleaf-layout-dialect 33 | 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-web 38 | 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-actuator 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-starter-test 48 | test 49 | 50 | 51 | 52 | org.springframework.hateoas 53 | spring-hateoas 54 | 55 | 56 | 57 | com.jayway.jsonpath 58 | json-path 59 | runtime 60 | 61 | 62 | 63 | org.hsqldb 64 | hsqldb 65 | runtime 66 | 67 | 68 | 69 | commons-lang 70 | commons-lang 71 | 2.6 72 | 73 | 74 | 75 | org.webjars 76 | bootstrap 77 | 3.3.6 78 | 79 | 80 | 81 | org.springframework.boot 82 | spring-boot-devtools 83 | true 84 | 85 | 86 | 87 | javax.xml.bind 88 | jaxb-api 89 | 90 | 91 | 92 | net.logstash.logback 93 | logstash-logback-encoder 94 | 4.11 95 | 96 | 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-customer/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | 6 | com.ewolff 7 | microservice-consuldns-demo 8 | 0.0.1-SNAPSHOT 9 | 10 | 11 | microservice-consuldns-demo-customer 12 | 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-data-jpa 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-data-rest 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-thymeleaf 28 | 29 | 30 | 31 | nz.net.ultraq.thymeleaf 32 | thymeleaf-layout-dialect 33 | 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-web 38 | 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-actuator 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-starter-test 48 | test 49 | 50 | 51 | 52 | org.springframework.hateoas 53 | spring-hateoas 54 | 55 | 56 | 57 | javax.xml.bind 58 | jaxb-api 59 | 60 | 61 | 62 | com.jayway.jsonpath 63 | json-path 64 | runtime 65 | 66 | 67 | 68 | org.hsqldb 69 | hsqldb 70 | runtime 71 | 72 | 73 | 74 | commons-lang 75 | commons-lang 76 | 2.6 77 | 78 | 79 | 80 | org.webjars 81 | bootstrap 82 | 3.3.6 83 | 84 | 85 | 86 | org.springframework.boot 87 | spring-boot-devtools 88 | true 89 | 90 | 91 | 92 | net.logstash.logback 93 | logstash-logback-encoder 94 | 4.11 95 | 96 | 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-customer/src/test/java/com/ewolff/microservice/customer/cdc/CustomerClient.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.customer.cdc; 2 | 3 | import java.util.Arrays; 4 | import java.util.Collection; 5 | import java.util.Collections; 6 | 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.beans.factory.annotation.Value; 9 | import org.springframework.hateoas.MediaTypes; 10 | import org.springframework.hateoas.PagedResources; 11 | import org.springframework.hateoas.hal.Jackson2HalModule; 12 | import org.springframework.http.ResponseEntity; 13 | import org.springframework.http.converter.HttpMessageConverter; 14 | import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; 15 | import org.springframework.stereotype.Component; 16 | import org.springframework.web.client.HttpClientErrorException; 17 | import org.springframework.web.client.RestTemplate; 18 | 19 | import com.fasterxml.jackson.databind.DeserializationFeature; 20 | import com.fasterxml.jackson.databind.ObjectMapper; 21 | 22 | @Component 23 | public class CustomerClient { 24 | 25 | private RestTemplate restTemplate; 26 | private String customerServiceHost; 27 | private long customerServicePort; 28 | 29 | static class CustomerPagedResources extends PagedResources { 30 | 31 | } 32 | 33 | @Autowired 34 | public CustomerClient( 35 | @Value("${customer.service.host:customer}") String customerServiceHost, 36 | @Value("${customer.service.port:8080}") long customerServicePort) { 37 | super(); 38 | this.restTemplate = getRestTemplate(); 39 | this.customerServiceHost = customerServiceHost; 40 | this.customerServicePort = customerServicePort; 41 | } 42 | 43 | public boolean isValidCustomerId(long customerId) { 44 | RestTemplate restTemplate = new RestTemplate(); 45 | try { 46 | ResponseEntity entity = restTemplate.getForEntity( 47 | customerURL() + customerId, String.class); 48 | return entity.getStatusCode().is2xxSuccessful(); 49 | } catch (final HttpClientErrorException e) { 50 | if (e.getStatusCode().value() == 404) 51 | return false; 52 | else 53 | throw e; 54 | } 55 | } 56 | 57 | protected RestTemplate getRestTemplate() { 58 | ObjectMapper mapper = new ObjectMapper(); 59 | mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, 60 | false); 61 | mapper.registerModule(new Jackson2HalModule()); 62 | 63 | MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); 64 | converter.setSupportedMediaTypes(Arrays.asList(MediaTypes.HAL_JSON)); 65 | converter.setObjectMapper(mapper); 66 | 67 | return new RestTemplate( 68 | Collections.>singletonList(converter)); 69 | } 70 | 71 | public Collection findAll() { 72 | PagedResources pagedResources = getRestTemplate() 73 | .getForObject(customerURL(), CustomerPagedResources.class); 74 | return pagedResources.getContent(); 75 | } 76 | 77 | private String customerURL() { 78 | return "http://" + customerServiceHost + ":" + customerServicePort 79 | + "/customer/"; 80 | } 81 | 82 | public Customer getOne(long customerId) { 83 | return restTemplate.getForObject(customerURL() + customerId, 84 | Customer.class); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-catalog/src/test/java/com/ewolff/microservice/catalog/CatalogWebIntegrationTest.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.catalog; 2 | 3 | import static org.hamcrest.Matchers.*; 4 | import static org.junit.Assert.*; 5 | 6 | import java.util.Arrays; 7 | 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | import org.junit.runner.RunWith; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.beans.factory.annotation.Value; 13 | import org.springframework.boot.test.context.SpringBootTest; 14 | import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; 15 | import org.springframework.http.HttpEntity; 16 | import org.springframework.http.HttpHeaders; 17 | import org.springframework.http.HttpMethod; 18 | import org.springframework.http.MediaType; 19 | import org.springframework.http.ResponseEntity; 20 | import org.springframework.test.context.ActiveProfiles; 21 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 22 | import org.springframework.web.client.RestTemplate; 23 | 24 | @RunWith(SpringJUnit4ClassRunner.class) 25 | @SpringBootTest(classes = CatalogApp.class, webEnvironment = WebEnvironment.DEFINED_PORT) 26 | @ActiveProfiles("test") 27 | public class CatalogWebIntegrationTest { 28 | 29 | @Autowired 30 | private ItemRepository itemRepository; 31 | 32 | @Value("${local.server.port}") 33 | private int serverPort; 34 | 35 | private Item iPodNano; 36 | 37 | private RestTemplate restTemplate; 38 | 39 | @Before 40 | public void setup() { 41 | iPodNano = itemRepository.findByName("iPod nano").get(0); 42 | restTemplate = new RestTemplate(); 43 | } 44 | 45 | @Test 46 | public void IsItemReturnedAsHTML() { 47 | String url = catalogURL() + "/" + iPodNano.getId() + ".html"; 48 | String body = getForMediaType(String.class, MediaType.TEXT_HTML, url); 49 | 50 | assertThat(body, containsString("iPod nano")); 51 | assertThat(body, containsString(" T getForMediaType(Class value, MediaType mediaType, 85 | String url) { 86 | HttpHeaders headers = new HttpHeaders(); 87 | headers.setAccept(Arrays.asList(mediaType)); 88 | 89 | HttpEntity entity = new HttpEntity("parameters", 90 | headers); 91 | 92 | ResponseEntity resultEntity = restTemplate.exchange(url, 93 | HttpMethod.GET, entity, value); 94 | 95 | return resultEntity.getBody(); 96 | } 97 | 98 | } 99 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-order/src/main/java/com/ewolff/microservice/order/clients/CustomerClient.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.order.clients; 2 | 3 | import java.util.Arrays; 4 | import java.util.Collection; 5 | import java.util.Collections; 6 | 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.beans.factory.annotation.Value; 11 | import org.springframework.hateoas.MediaTypes; 12 | import org.springframework.hateoas.PagedResources; 13 | import org.springframework.hateoas.hal.Jackson2HalModule; 14 | import org.springframework.http.ResponseEntity; 15 | import org.springframework.http.converter.HttpMessageConverter; 16 | import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; 17 | import org.springframework.stereotype.Component; 18 | import org.springframework.web.client.HttpClientErrorException; 19 | import org.springframework.web.client.RestTemplate; 20 | 21 | import com.fasterxml.jackson.databind.DeserializationFeature; 22 | import com.fasterxml.jackson.databind.ObjectMapper; 23 | 24 | @Component 25 | public class CustomerClient { 26 | 27 | private final Logger log = LoggerFactory.getLogger(CustomerClient.class); 28 | 29 | private RestTemplate restTemplate; 30 | private String customerServiceHost; 31 | private long customerServicePort; 32 | 33 | static class CustomerPagedResources extends PagedResources { 34 | 35 | } 36 | 37 | @Autowired 38 | public CustomerClient( 39 | @Value("${customer.service.host:msconsuldns-customer.service.consul}") String customerServiceHost, 40 | @Value("${customer.service.port:8080}") long customerServicePort, 41 | @Autowired RestTemplate restTemplate) { 42 | super(); 43 | this.restTemplate = restTemplate; 44 | this.customerServiceHost = customerServiceHost; 45 | this.customerServicePort = customerServicePort; 46 | } 47 | 48 | public boolean isValidCustomerId(long customerId) { 49 | RestTemplate restTemplate = new RestTemplate(); 50 | try { 51 | ResponseEntity entity = restTemplate.getForEntity(customerURL() + customerId, String.class); 52 | return entity.getStatusCode().is2xxSuccessful(); 53 | } catch (final HttpClientErrorException e) { 54 | if (e.getStatusCode().value() == 404) 55 | return false; 56 | else 57 | throw e; 58 | } 59 | } 60 | 61 | protected RestTemplate getRestTemplate() { 62 | ObjectMapper mapper = new ObjectMapper(); 63 | mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 64 | mapper.registerModule(new Jackson2HalModule()); 65 | 66 | MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); 67 | converter.setSupportedMediaTypes(Arrays.asList(MediaTypes.HAL_JSON)); 68 | converter.setObjectMapper(mapper); 69 | 70 | return new RestTemplate(Collections.>singletonList(converter)); 71 | } 72 | 73 | public Collection findAll() { 74 | PagedResources pagedResources = getRestTemplate().getForObject(customerURL(), 75 | CustomerPagedResources.class); 76 | return pagedResources.getContent(); 77 | } 78 | 79 | private String customerURL() { 80 | String url; 81 | url = String.format("http://%s:%s/customer/", customerServiceHost, customerServicePort); 82 | log.trace("Customer: URL {} ", url); 83 | return url; 84 | 85 | } 86 | 87 | public Customer getOne(long customerId) { 88 | return restTemplate.getForObject(customerURL() + customerId, Customer.class); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-order/src/test/java/com/ewolff/microservice/order/logic/OrderWebIntegrationTest.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.order.logic; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.util.stream.StreamSupport; 6 | 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.beans.factory.annotation.Value; 12 | import org.springframework.boot.test.context.SpringBootTest; 13 | import org.springframework.http.ResponseEntity; 14 | import org.springframework.test.context.ActiveProfiles; 15 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 16 | import org.springframework.transaction.annotation.Transactional; 17 | import org.springframework.util.LinkedMultiValueMap; 18 | import org.springframework.util.MultiValueMap; 19 | import org.springframework.web.client.RestTemplate; 20 | 21 | import com.ewolff.microservice.order.OrderApp; 22 | import com.ewolff.microservice.order.clients.CatalogClient; 23 | import com.ewolff.microservice.order.clients.Customer; 24 | import com.ewolff.microservice.order.clients.CustomerClient; 25 | import com.ewolff.microservice.order.clients.Item; 26 | 27 | @RunWith(SpringJUnit4ClassRunner.class) 28 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, classes = OrderApp.class) 29 | @ActiveProfiles("test") 30 | public class OrderWebIntegrationTest { 31 | 32 | private RestTemplate restTemplate = new RestTemplate(); 33 | 34 | @Value("${server.port}") 35 | private long serverPort; 36 | 37 | @Autowired 38 | private CatalogClient catalogClient; 39 | 40 | @Autowired 41 | private CustomerClient customerClient; 42 | 43 | @Autowired 44 | private OrderRepository orderRepository; 45 | 46 | private Item item; 47 | 48 | private Customer customer; 49 | 50 | @Before 51 | public void setup() { 52 | item = catalogClient.findAll().iterator().next(); 53 | customer = customerClient.findAll().iterator().next(); 54 | assertEquals("Eberhard", customer.getFirstname()); 55 | } 56 | 57 | @Test 58 | public void IsOrderListReturned() { 59 | try { 60 | Iterable orders = orderRepository.findAll(); 61 | assertTrue(StreamSupport 62 | .stream(orders.spliterator(), false) 63 | .noneMatch( 64 | o -> (o.getCustomerId() == customer.getCustomerId()))); 65 | ResponseEntity resultEntity = restTemplate.getForEntity( 66 | orderURL(), String.class); 67 | assertTrue(resultEntity.getStatusCode().is2xxSuccessful()); 68 | String orderList = resultEntity.getBody(); 69 | assertFalse(orderList.contains("Eberhard")); 70 | Order order = new Order(customer.getCustomerId()); 71 | order.addLine(42, item.getItemId()); 72 | orderRepository.save(order); 73 | orderList = restTemplate.getForObject(orderURL(), String.class); 74 | assertTrue(orderList.contains("Eberhard")); 75 | } finally { 76 | orderRepository.deleteAll(); 77 | } 78 | } 79 | 80 | private String orderURL() { 81 | return "http://localhost:" + serverPort; 82 | } 83 | 84 | @Test 85 | public void IsOrderFormDisplayed() { 86 | ResponseEntity resultEntity = restTemplate.getForEntity( 87 | orderURL() + "/form.html", String.class); 88 | assertTrue(resultEntity.getStatusCode().is2xxSuccessful()); 89 | assertTrue(resultEntity.getBody().contains(" map = new LinkedMultiValueMap(); 97 | map.add("submit", ""); 98 | map.add("customerId", Long.toString(customer.getCustomerId())); 99 | map.add("orderLine[0].itemId", Long.toString(item.getItemId())); 100 | map.add("orderLine[0].count", "42"); 101 | restTemplate.postForLocation(orderURL(), map, String.class); 102 | assertEquals(before + 1, orderRepository.count()); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/microservice-consuldns-demo-customer/src/test/java/com/ewolff/microservice/customer/CustomerWebIntegrationTest.java: -------------------------------------------------------------------------------- 1 | package com.ewolff.microservice.customer; 2 | 3 | import static org.hamcrest.Matchers.*; 4 | import static org.junit.Assert.*; 5 | 6 | import java.util.Arrays; 7 | import java.util.stream.StreamSupport; 8 | 9 | import org.junit.Before; 10 | import org.junit.Test; 11 | import org.junit.runner.RunWith; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.beans.factory.annotation.Value; 14 | import org.springframework.boot.test.context.SpringBootTest; 15 | import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; 16 | import org.springframework.http.HttpEntity; 17 | import org.springframework.http.HttpHeaders; 18 | import org.springframework.http.HttpMethod; 19 | import org.springframework.http.MediaType; 20 | import org.springframework.http.ResponseEntity; 21 | import org.springframework.test.context.ActiveProfiles; 22 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 23 | import org.springframework.transaction.annotation.Transactional; 24 | import org.springframework.util.LinkedMultiValueMap; 25 | import org.springframework.util.MultiValueMap; 26 | import org.springframework.web.client.RestTemplate; 27 | 28 | @RunWith(SpringJUnit4ClassRunner.class) 29 | @SpringBootTest(classes = CustomerApp.class, webEnvironment = WebEnvironment.DEFINED_PORT) 30 | @ActiveProfiles("test") 31 | public class CustomerWebIntegrationTest { 32 | 33 | @Autowired 34 | private CustomerRepository customerRepository; 35 | 36 | @Value("${server.port}") 37 | private int serverPort; 38 | 39 | private RestTemplate restTemplate; 40 | 41 | private T getForMediaType(Class value, MediaType mediaType, 42 | String url) { 43 | HttpHeaders headers = new HttpHeaders(); 44 | headers.setAccept(Arrays.asList(mediaType)); 45 | 46 | HttpEntity entity = new HttpEntity("parameters", 47 | headers); 48 | 49 | ResponseEntity resultEntity = restTemplate.exchange(url, 50 | HttpMethod.GET, entity, value); 51 | 52 | return resultEntity.getBody(); 53 | } 54 | 55 | @Test 56 | public void IsCustomerReturnedAsHTML() { 57 | 58 | Customer customerWolff = customerRepository.findByName("Wolff").get(0); 59 | 60 | String body = getForMediaType(String.class, MediaType.TEXT_HTML, 61 | customerURL() + customerWolff.getId() + ".html"); 62 | 63 | assertThat(body, containsString("Wolff")); 64 | assertThat(body, containsString(" customers = customerRepository.findAll(); 88 | assertTrue(StreamSupport.stream(customers.spliterator(), false) 89 | .noneMatch(c -> (c.getName().equals("Hoeller1")))); 90 | ResponseEntity resultEntity = restTemplate.getForEntity( 91 | customerURL() + "/list.html", String.class); 92 | assertTrue(resultEntity.getStatusCode().is2xxSuccessful()); 93 | String customerList = resultEntity.getBody(); 94 | assertFalse(customerList.contains("Hoeller1")); 95 | customerRepository.save(new Customer("Juergen", "Hoeller1", 96 | "springjuergen@twitter.com", "Schlossallee", "Linz")); 97 | 98 | customerList = restTemplate.getForObject(customerURL() + "/list.html", 99 | String.class); 100 | assertTrue(customerList.contains("Hoeller1")); 101 | 102 | } 103 | 104 | private String customerURL() { 105 | return "http://localhost:" + serverPort + "/"; 106 | } 107 | 108 | @Test 109 | public void IsCustomerFormDisplayed() { 110 | ResponseEntity resultEntity = restTemplate.getForEntity( 111 | customerURL() + "/form.html", String.class); 112 | assertTrue(resultEntity.getStatusCode().is2xxSuccessful()); 113 | assertTrue(resultEntity.getBody().contains(" map = new LinkedMultiValueMap(); 121 | map.add("firstname", "Juergen"); 122 | map.add("name", "Hoeller"); 123 | map.add("street", "Schlossallee"); 124 | map.add("city", "Linz"); 125 | map.add("email", "springjuergen@twitter.com"); 126 | 127 | restTemplate.postForObject(customerURL() + "form.html", map, 128 | String.class); 129 | assertEquals(1, customerRepository.findByName("Hoeller").size()); 130 | } 131 | 132 | } 133 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Microservice Consul Sample 2 | =================== 3 | 4 | [Deutsche Anleitung zum Starten des Beispiels](WIE-LAUFEN.md) 5 | 6 | 7 | This sample is like the sample that you can find at 8 | https://github.com/ewolff/microservice-consul . 9 | 10 | This demo uses [Hashicorp Consul](https://www.consul.io) for service 11 | discovery and Apache httpd as a reverse proxy to route and load balance 12 | calls to services. It uses Consul as a DNS server for service discovery. 13 | Service names are postfixed with `.service.consul` on registration with Consul 14 | through [Registrator](https://github.com/gliderlabs/registrator) 15 | which monitors the Unix Socket `docker.sock` and automatically registers and deregisteres 16 | all Docker Containers running on a host. Services resolve their 17 | communication partner's current IP addresses over ordinary DNS resolution, 18 | thereby making service implementations independent 19 | of any Consul-specific APIs. 20 | 21 | This project creates a complete microservice demo system in Docker 22 | containers. The services are implemented in Java using Spring and 23 | Spring Cloud. 24 | 25 | It uses three microservices: 26 | - `Order` to process orders. (http://localhost:8080 when started locally) 27 | - `Customer` to handle customer data. (http://localhost:8080) 28 | - `Catalog` to handle the items in the catalog. (http://localhost:8080) 29 | 30 | Consul 31 | ------ 32 | 33 | Consul has a Web UI. You can access it at port 8500 of your Docker 34 | host. Also the homepage at port 8080 contains a link to the Consul UI 35 | 36 | Also you can use Consul's DNS interface with e.g. dig: 37 | 38 | ``` 39 | dig @localhost msconsuldns_order.service.consul. 40 | dig @localhost msconsuldns_order.service.consul. ANY 41 | dig @localhost msconsuldns_order.service.consul. SRV 42 | ``` 43 | 44 | Note that the demo uses the original 45 | [Consul Docker image](https://hub.docker.com/_/consul/) provided by 46 | Hashicorp. However, the demo does not use a Consul cluster and only 47 | stores the data in memory i.e. it is certainly not fit for production. 48 | 49 | The Consul DNS interface is mapped to port 53 on the Docker Host. The 50 | Docker containers are configured with to use the IP adress of the 51 | Docker Host as the DNS server. 52 | 53 | [Registrator](https://github.com/gliderlabs/registrator) registers all 54 | Docker containers including the Spring Cloud microservices (customer, 55 | catalog and order) 56 | 57 | Apache HTTP Load Balancer 58 | ------------------------ 59 | 60 | Apache HTTP is used to provide the web page of the demo at 61 | port 8080. It also forwards HTTP requests to the microservices whose ports 62 | are not exposed! Apache HTTP is configured as a reverse proxy for this - and 63 | as a load balancer i.e. if you start multiple instances of a microservices 64 | e.g. via `docker-compose scale catalog=2`, Apache will recognize the new instance. 65 | 66 | To configure this Apache HTTP needs to get all registered services from 67 | Consul. [Consul Template](https://github.com/hashicorp/consul-template) 68 | is used for this. It uses a template for the Apache HTTP 69 | configuration and fills in the IP addresses of the registered services. 70 | 71 | The Docker container therefore runs two processes: Apache HTTP and 72 | Consul Template. Consul Template starts Apache httpd and also restarts 73 | Apache httpd when new services are registered in the Consul server. 74 | 75 | Please refer to the subdirectory `apache` to see how this works. 76 | 77 | 78 | Technologies 79 | ------------ 80 | 81 | - Consul for Lookup/ Discovery 82 | - Apache as a reverse proxy to route calls to the appropriate SCS. 83 | 84 | How To Run 85 | ---------- 86 | 87 | The demo can be run with [Docker Machine and Docker 88 | Compose](docker/README.md) via `docker-compose up` 89 | 90 | See [How to run](HOW-TO-RUN.md) for details. 91 | 92 | Remarks on the Code 93 | ------------------- 94 | 95 | The servers for the infrastructure components are pretty simple thanks to Spring Cloud: 96 | 97 | The microservices are: 98 | 99 | - [microservice-consuldns-demo-catalog](microservice-consuldns-demo/microservice-consuldns-demo-catalog) is the application to take care of items. 100 | - [microservice-consuldns-demo-customer](microservice-consuldns-demo/microservice-consuldns-demo-customer) is responsible for customers. 101 | - [microservice-consuldns-demo-order](microservice-consuldns-demo/microservice-consuldns-demo-order) does order processing. It uses microservice-demo-catalog and microservice-demo-customer. 102 | 103 | 104 | The microservices have a Java main application in `src/test/java` to run them stand alone. `microservice-demo-order` uses a stub for the other services then. Also there are tests that use _consumer-driven contracts_. That is why it is ensured that the services provide the correct interface. These CDC tests are used in microservice-demo-order to verify the stubs. In `microservice-demo-customer` and `microserivce-demo-catalog` they are used to verify the implemented REST services. 105 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven2 Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM set title of command window 39 | title %0 40 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' 41 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 42 | 43 | @REM set %HOME% to equivalent of $HOME 44 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 45 | 46 | @REM Execute a user defined script before this one 47 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 48 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 49 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 50 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 51 | :skipRcPre 52 | 53 | @setlocal 54 | 55 | set ERROR_CODE=0 56 | 57 | @REM To isolate internal variables from possible post scripts, we use another setlocal 58 | @setlocal 59 | 60 | @REM ==== START VALIDATION ==== 61 | if not "%JAVA_HOME%" == "" goto OkJHome 62 | 63 | echo. 64 | echo Error: JAVA_HOME not found in your environment. >&2 65 | echo Please set the JAVA_HOME variable in your environment to match the >&2 66 | echo location of your Java installation. >&2 67 | echo. 68 | goto error 69 | 70 | :OkJHome 71 | if exist "%JAVA_HOME%\bin\java.exe" goto init 72 | 73 | echo. 74 | echo Error: JAVA_HOME is set to an invalid directory. >&2 75 | echo JAVA_HOME = "%JAVA_HOME%" >&2 76 | echo Please set the JAVA_HOME variable in your environment to match the >&2 77 | echo location of your Java installation. >&2 78 | echo. 79 | goto error 80 | 81 | @REM ==== END VALIDATION ==== 82 | 83 | :init 84 | 85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 86 | @REM Fallback to current working directory if not found. 87 | 88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 90 | 91 | set EXEC_DIR=%CD% 92 | set WDIR=%EXEC_DIR% 93 | :findBaseDir 94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 95 | cd .. 96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 97 | set WDIR=%CD% 98 | goto findBaseDir 99 | 100 | :baseDirFound 101 | set MAVEN_PROJECTBASEDIR=%WDIR% 102 | cd "%EXEC_DIR%" 103 | goto endDetectBaseDir 104 | 105 | :baseDirNotFound 106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 107 | cd "%EXEC_DIR%" 108 | 109 | :endDetectBaseDir 110 | 111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 112 | 113 | @setlocal EnableExtensions EnableDelayedExpansion 114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 116 | 117 | :endReadAdditionalConfig 118 | 119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 120 | 121 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 122 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 123 | 124 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 125 | if ERRORLEVEL 1 goto error 126 | goto end 127 | 128 | :error 129 | set ERROR_CODE=1 130 | 131 | :end 132 | @endlocal & set ERROR_CODE=%ERROR_CODE% 133 | 134 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 135 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 136 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 137 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 138 | :skipRcPost 139 | 140 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 141 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 142 | 143 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 144 | 145 | exit /B %ERROR_CODE% 146 | -------------------------------------------------------------------------------- /microservice-consuldns-demo/mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven2 Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 58 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 59 | if [ -z "$JAVA_HOME" ]; then 60 | if [ -x "/usr/libexec/java_home" ]; then 61 | export JAVA_HOME="`/usr/libexec/java_home`" 62 | else 63 | export JAVA_HOME="/Library/Java/Home" 64 | fi 65 | fi 66 | ;; 67 | esac 68 | 69 | if [ -z "$JAVA_HOME" ] ; then 70 | if [ -r /etc/gentoo-release ] ; then 71 | JAVA_HOME=`java-config --jre-home` 72 | fi 73 | fi 74 | 75 | if [ -z "$M2_HOME" ] ; then 76 | ## resolve links - $0 may be a link to maven's home 77 | PRG="$0" 78 | 79 | # need this for relative symlinks 80 | while [ -h "$PRG" ] ; do 81 | ls=`ls -ld "$PRG"` 82 | link=`expr "$ls" : '.*-> \(.*\)$'` 83 | if expr "$link" : '/.*' > /dev/null; then 84 | PRG="$link" 85 | else 86 | PRG="`dirname "$PRG"`/$link" 87 | fi 88 | done 89 | 90 | saveddir=`pwd` 91 | 92 | M2_HOME=`dirname "$PRG"`/.. 93 | 94 | # make it fully qualified 95 | M2_HOME=`cd "$M2_HOME" && pwd` 96 | 97 | cd "$saveddir" 98 | # echo Using m2 at $M2_HOME 99 | fi 100 | 101 | # For Cygwin, ensure paths are in UNIX format before anything is touched 102 | if $cygwin ; then 103 | [ -n "$M2_HOME" ] && 104 | M2_HOME=`cygpath --unix "$M2_HOME"` 105 | [ -n "$JAVA_HOME" ] && 106 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 107 | [ -n "$CLASSPATH" ] && 108 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 109 | fi 110 | 111 | # For Mingw, ensure paths are in UNIX format before anything is touched 112 | if $mingw ; then 113 | [ -n "$M2_HOME" ] && 114 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 115 | [ -n "$JAVA_HOME" ] && 116 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 117 | # TODO classpath? 118 | fi 119 | 120 | if [ -z "$JAVA_HOME" ]; then 121 | javaExecutable="`which javac`" 122 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 123 | # readlink(1) is not available as standard on Solaris 10. 124 | readLink=`which readlink` 125 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 126 | if $darwin ; then 127 | javaHome="`dirname \"$javaExecutable\"`" 128 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 129 | else 130 | javaExecutable="`readlink -f \"$javaExecutable\"`" 131 | fi 132 | javaHome="`dirname \"$javaExecutable\"`" 133 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 134 | JAVA_HOME="$javaHome" 135 | export JAVA_HOME 136 | fi 137 | fi 138 | fi 139 | 140 | if [ -z "$JAVACMD" ] ; then 141 | if [ -n "$JAVA_HOME" ] ; then 142 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 143 | # IBM's JDK on AIX uses strange locations for the executables 144 | JAVACMD="$JAVA_HOME/jre/sh/java" 145 | else 146 | JAVACMD="$JAVA_HOME/bin/java" 147 | fi 148 | else 149 | JAVACMD="`which java`" 150 | fi 151 | fi 152 | 153 | if [ ! -x "$JAVACMD" ] ; then 154 | echo "Error: JAVA_HOME is not defined correctly." >&2 155 | echo " We cannot execute $JAVACMD" >&2 156 | exit 1 157 | fi 158 | 159 | if [ -z "$JAVA_HOME" ] ; then 160 | echo "Warning: JAVA_HOME environment variable is not set." 161 | fi 162 | 163 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 164 | 165 | # traverses directory structure from process work directory to filesystem root 166 | # first directory with .mvn subdirectory is considered project base directory 167 | find_maven_basedir() { 168 | 169 | if [ -z "$1" ] 170 | then 171 | echo "Path not specified to find_maven_basedir" 172 | return 1 173 | fi 174 | 175 | basedir="$1" 176 | wdir="$1" 177 | while [ "$wdir" != '/' ] ; do 178 | if [ -d "$wdir"/.mvn ] ; then 179 | basedir=$wdir 180 | break 181 | fi 182 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 183 | if [ -d "${wdir}" ]; then 184 | wdir=`cd "$wdir/.."; pwd` 185 | fi 186 | # end of workaround 187 | done 188 | echo "${basedir}" 189 | } 190 | 191 | # concatenates all lines of a file 192 | concat_lines() { 193 | if [ -f "$1" ]; then 194 | echo "$(tr -s '\n' ' ' < "$1")" 195 | fi 196 | } 197 | 198 | BASE_DIR=`find_maven_basedir "$(pwd)"` 199 | if [ -z "$BASE_DIR" ]; then 200 | exit 1; 201 | fi 202 | 203 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 204 | if [ "$MVNW_VERBOSE" = true ]; then 205 | echo $MAVEN_PROJECTBASEDIR 206 | fi 207 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 208 | 209 | # For Cygwin, switch paths to Windows format before running java 210 | if $cygwin; then 211 | [ -n "$M2_HOME" ] && 212 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 213 | [ -n "$JAVA_HOME" ] && 214 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 215 | [ -n "$CLASSPATH" ] && 216 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 217 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 218 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 219 | fi 220 | 221 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 222 | 223 | exec "$JAVACMD" \ 224 | $MAVEN_OPTS \ 225 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 226 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 227 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 228 | -------------------------------------------------------------------------------- /HOW-TO-RUN.md: -------------------------------------------------------------------------------- 1 | # How to Run 2 | 3 | This is a step-by-step guide how to run the example: 4 | 5 | ## Installation 6 | 7 | * The example is implemented in Java. See 8 | https://www.java.com/en/download/help/download_options.xml . The 9 | examples need to be compiled so you need to install a JDK (Java 10 | Development Kit). A JRE (Java Runtime Environment) is not 11 | sufficient. After the installation you should be able to execute 12 | `java` and `javac` on the command line. 13 | 14 | * The example run in Docker Containers. You need to install Docker 15 | Community Edition, see https://www.docker.com/community-edition/ 16 | . You should be able to run `docker` after the installation. 17 | 18 | * The example need a lot of RAM. You should configure Docker to use 4 19 | GB of RAM. Otherwise Docker containers might be killed due to lack 20 | of RAM. On Windows and macOS you can find the RAM setting in the 21 | Docker application under Preferences/ Advanced. 22 | 23 | * After installing Docker you should also be able to run 24 | `docker-compose`. If this is not possible, you might need to install 25 | it separately. See https://docs.docker.com/compose/install/ . 26 | 27 | ## Build 28 | 29 | Change to the directory `microservice-consul-demo` and run `./mvnw clean 30 | package` or `mvnw.cmd clean package` (Windows). This will take a while: 31 | 32 | ``` 33 | [~/microservice-consuldns/microservice-consuldns-demo]./mvnw clean package 34 | .... 35 | [INFO] 36 | [INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ microservice-consuldns-demo-order --- 37 | [INFO] Building jar: /Users/wolff/microservice-consuldns/microservice-consuldns-demo/microservice-consuldns-demo-order/target/microservice-consuldns-demo-order-0.0.1-SNAPSHOT.jar 38 | [INFO] 39 | [INFO] --- spring-boot-maven-plugin:1.4.5.RELEASE:repackage (default) @ microservice-consuldns-demo-order --- 40 | [INFO] ------------------------------------------------------------------------ 41 | [INFO] Reactor Summary: 42 | [INFO] 43 | [INFO] microservice-consuldns-demo ........................... SUCCESS [ 1.401 s] 44 | [INFO] microservice-consuldns-demo-customer .................. SUCCESS [ 25.636 s] 45 | [INFO] microservice-consuldns-demo-catalog ................... SUCCESS [ 36.618 s] 46 | [INFO] microservice-consuldns-demo-order ..................... SUCCESS [ 27.781 s] 47 | [INFO] ------------------------------------------------------------------------ 48 | [INFO] BUILD SUCCESS 49 | [INFO] ------------------------------------------------------------------------ 50 | [INFO] Total time: 01:35 min 51 | [INFO] Finished at: 2017-09-07T18:08:13+02:00 52 | [INFO] Final Memory: 52M/416M 53 | [INFO] ------------------------------------------------------------------------ 54 | ``` 55 | 56 | If this does not work: 57 | 58 | * Ensure that `settings.xml` in the directory `.m2` in your home 59 | directory contains no configuration for a specific Maven repo. If in 60 | doubt: delete the file. 61 | 62 | * The tests use some ports on the local machine. Make sure that no 63 | server runs in the background. 64 | 65 | * Skip the tests: `./mvnw clean package 66 | -Dmaven.test.skip=true` or `mvnw.cmd clean package 67 | -Dmaven.test.skip=true` (Windows). 68 | 69 | * In rare cases dependencies might not be downloaded correctly. In 70 | that case: Remove the directory `repository` in the directory `.m2` 71 | in your home directory. Note that this means all dependencies will 72 | be downloaded again. 73 | 74 | ## Run the containers 75 | 76 | First you need to build the Docker images. Change to the directory 77 | `docker` and run `docker-compose build`. This will download some base 78 | images, install software into Docker images and will therefore take 79 | its time: 80 | 81 | ``` 82 | [~/microservice-consuldns/docker]docker-compose build 83 | .... 84 | Removing intermediate container 1d59f8227b12 85 | Step 4/4 : EXPOSE 8989 86 | ---> Running in 11e7fbacfa01 87 | ---> 9cfa7772986f 88 | Removing intermediate container 11e7fbacfa01 89 | Successfully built 9cfa7772986f 90 | ``` 91 | 92 | Afterwards the Docker images should have been created. They have the prefix 93 | `msconsuldns`: 94 | 95 | ``` 96 | [~/microservice-consuldns/docker]docker images 97 | REPOSITORY TAG IMAGE ID CREATED SIZE 98 | msconsuldns_order latest 12b279e78975 52 seconds ago 225MB 99 | msconsuldns_apache latest 22fac099ba93 55 seconds ago 255MB 100 | msconsuldns_catalog latest c23c535ecaf6 2 minutes ago 225MB 101 | msconsuldns_customer latest a780e4f49bac 2 minutes ago 225MB 102 | ``` 103 | 104 | Figure out the IP adress of your system and set it to the environment 105 | variable CONSUL_HOST. This is needed to ensure that the containers are 106 | able to use Consul as a DNS server. 107 | 108 | ``` 109 | [~/microservice-consuldns/docker]ifconfig 110 | .... 111 | en5: flags=8863 mtu 1500 112 | .... 113 | inet 192.168.1.152 netmask 0xffffff00 broadcast 192.168.1.255 114 | .... 115 | [~/microservice-consuldns/docker]export CONSUL_HOST=192.168.1.152 116 | ``` 117 | 118 | 119 | Now you can start the containers using `docker-compose up -d`. The 120 | `-d` option means that the containers will be started in the 121 | background and won't output their stdout to the command line: 122 | 123 | ``` 124 | [~/microservice-consuldns/docker]docker-compose up -d 125 | Creating network "msconsuldns_default" with the default driver 126 | Pulling consul (consul:0.7.2)... 127 | 0.7.2: Pulling from library/consul 128 | b7f33cc0b48e: Already exists 129 | a4ca795d20eb: Pull complete 130 | 76bc5ef06918: Pull complete 131 | 965e633cb8c2: Pull complete 132 | 64e424fcbe65: Pull complete 133 | Digest: sha256:ce15f85417a0cf121d943563dedb873c7d6c26e9b1e8b47bc2f1b5a3e27498e1 134 | Status: Downloaded newer image for consul:0.7.2 135 | Creating msconsuldns_consul_1 ... 136 | Creating msconsuldns_consul_1 ... done 137 | Creating msconsuldns_order_1 ... 138 | Creating msconsuldns_catalog_1 ... 139 | Creating msconsuldns_customer_1 ... 140 | Creating msconsuldns_apache_1 ... 141 | Creating msconsuldns_order_1 142 | Creating msconsuldns_catalog_1 143 | Creating msconsuldns_apache_1 144 | Creating msconsuldns_apache_1 ... done 145 | ``` 146 | 147 | As you can see the Consul Docker image is downloaded now. 148 | 149 | Check wether all containers are running: 150 | 151 | ``` 152 | [~/microservice-consuldns/docker]docker ps 153 | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 154 | 2697aa91700b msconsuldns_apache "/bin/sh -c '/usr/..." 4 minutes ago Up 4 minutes 0.0.0.0:8080->80/tcp msconsuldns_apache_1 155 | 948f2576b0b0 msconsuldns_customer "/bin/sh -c '/usr/..." 4 minutes ago Up 4 minutes 8080/tcp msconsuldns_customer_1 156 | 0574e8dc5b11 msconsuldns_order "/bin/sh -c '/usr/..." 4 minutes ago Up 4 minutes 8080/tcp msconsuldns_order_1 157 | 144542583a05 msconsuldns_catalog "/bin/sh -c '/usr/..." 4 minutes ago Up 4 minutes 8080/tcp msconsuldns_catalog_1 158 | c28d2a38f657 consul:0.7.2 "docker-entrypoint..." 4 minutes ago Up 4 minutes 8300-8302/tcp, 8400/tcp, 8600/tcp, 8301-8302/udp, 0.0.0.0:8500->8500/tcp, 0.0.0.0:8600->8600/udp msconsuldns_consul_1 159 | ``` 160 | `docker ps -a` also shows the terminated Docker containers. That is 161 | useful to see Docker containers that crashed rigth after they started. 162 | 163 | If one of the containers is not running, you can look at its logs using 164 | e.g. `docker logs msconsuldns_apache_1`. The name of the container is 165 | given in the last column of the output of `docker ps`. Looking at the 166 | logs even works after the container has been 167 | terminated. If the log says that the container has been `killed`, you 168 | need to increase the RAM assigned to Docker to e.g. 4GB. On Windows 169 | and macOS you can find the RAM setting in the Docker application under 170 | Preferences/ Advanced. 171 | 172 | If you need to do more trouble shooting open a shell in the container 173 | using e.g. `docker exec -it msconsuldns_catalog_1 /bin/sh` or execute 174 | command using `docker exec msconsuldns_catalog_1 /bin/ls`. 175 | 176 | You can access the microservices at http://localhost:8080/ 177 | and the Consul dashboard 178 | at http://localhost:8500 . 179 | 180 | You can terminate all containers using `docker-compose down`. 181 | 182 | If the order container cannot find the other containers, make sure 183 | that no firewall is running on the Docker host. At least on Mac OS X 184 | the firewall blocks the DNS requests. 185 | -------------------------------------------------------------------------------- /WIE-LAUFEN.md: -------------------------------------------------------------------------------- 1 | # Beispiel starten 2 | 3 | Die ist eine Schritt-für-Schritt-Anleitung zum Starten der Beispiele. 4 | Informationen zu Maven und Docker finden sich im 5 | [Cheatsheet-Projekt](https://github.com/ewolff/cheatsheets-DE). 6 | 7 | ## Installation 8 | 9 | * Die Beispiele sind in Java implementiert. Daher muss Java 10 | installiert werden. Die Anleitung findet sich unter 11 | https://www.java.com/en/download/help/download_options.xml . Da die 12 | Beispiele kompiliert werden müssen, muss ein JDK (Java Development 13 | Kit) installiert werden. Das JRE (Java Runtime Environment) reicht 14 | nicht aus. Nach der Installation sollte sowohl `java` und `javac` in 15 | der Eingabeaufforderung möglich sein. 16 | 17 | * Die Beispiele laufen in Docker Containern. Dazu ist eine 18 | Installation von Docker Community Edition notwendig, siehe 19 | https://www.docker.com/community-edition/ . Docker kann mit 20 | `docker` aufgerufen werden. Das sollte nach der Installation ohne 21 | Fehler möglich sein. 22 | 23 | * Die Beispiele benötigen zum Teil sehr viel Speicher. Daher sollte 24 | Docker ca. 4 GB zur Verfügung haben. Sonst kann es vorkommen, dass 25 | Docker Container aus Speichermangel beendet werden. Unter Windows 26 | und macOS findet sich die Einstellung dafür in der Docker-Anwendung 27 | unter Preferences/ Advanced. 28 | 29 | * Nach der Installation von Docker sollte `docker-compose` aufrufbar 30 | sein. Wenn Docker Compose nicht aufgerufen werden kann, ist es nicht 31 | als Teil der Docker Community Edition installiert worden. Dann ist 32 | eine separate Installation notwendig, siehe 33 | https://docs.docker.com/compose/install/ . 34 | 35 | ## Build 36 | 37 | Wechsel in das Verzeichnis `microservice-consuldns-demo` und starte `./mvnw clean 38 | package` bzw. `mvnw.cmd clean package` (Windows). Das wird einige Zeit dauern: 39 | 40 | ``` 41 | [~/microservice-consuldns/microservice-consuldns-demo]./mvnw clean package 42 | .... 43 | [INFO] 44 | [INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ microservice-consuldns-demo-order --- 45 | [INFO] Building jar: /Users/wolff/microservice-consuldns/microservice-consuldns-demo/microservice-consuldns-demo-order/target/microservice-consuldns-demo-order-0.0.1-SNAPSHOT.jar 46 | [INFO] 47 | [INFO] --- spring-boot-maven-plugin:1.4.5.RELEASE:repackage (default) @ microservice-consuldns-demo-order --- 48 | [INFO] ------------------------------------------------------------------------ 49 | [INFO] Reactor Summary: 50 | [INFO] 51 | [INFO] microservice-consuldns-demo ........................... SUCCESS [ 1.401 s] 52 | [INFO] microservice-consuldns-demo-customer .................. SUCCESS [ 25.636 s] 53 | [INFO] microservice-consuldns-demo-catalog ................... SUCCESS [ 36.618 s] 54 | [INFO] microservice-consuldns-demo-order ..................... SUCCESS [ 27.781 s] 55 | [INFO] ------------------------------------------------------------------------ 56 | [INFO] BUILD SUCCESS 57 | [INFO] ------------------------------------------------------- ----------------- 58 | [INFO] Total time: 01:35 min 59 | [INFO] Finished at: 2017-09-07T18:08:13+02:00 60 | [INFO] Final Memory: 52M/416M 61 | [INFO] ------------------------------------------------------------------------ 62 | ``` 63 | 64 | Weitere Information zu Maven gibt es im 65 | [Maven Cheatsheet](https://github.com/ewolff/cheatsheets-DE/blob/master/MavenCheatSheet.md). 66 | 67 | Falls es dabei zu Fehlern kommt: 68 | 69 | * Stelle sicher, dass die Datei `settings.xml` im Verzeichnis `.m2` 70 | in deinem Heimatverzeichnis keine Konfiguration für ein spezielles 71 | Maven Repository enthalten. Im Zweifelsfall kannst du die Datei 72 | einfach löschen. 73 | 74 | * Die Tests nutzen einige Ports auf dem Rechner. Stelle sicher, dass 75 | im Hintergrund keine Server laufen. 76 | 77 | * Führe die Tests beim Build nicht aus: `./mvnw clean package 78 | -Dmaven.test.skip=true` bzw. `mvnw.cmd clean package 79 | -Dmaven.test.skip=true`. 80 | 81 | * In einigen selten Fällen kann es vorkommen, dass die Abhängigkeiten 82 | nicht korrekt heruntergeladen werden. Wenn du das Verzeichnis 83 | `repository` im Verzeichnis `.m2` löscht, werden alle Abhängigkeiten 84 | erneut heruntergeladen. 85 | 86 | ## Docker Container starten 87 | 88 | Weitere Information zu Docker gibt es im 89 | [Docker Cheatsheet](https://github.com/ewolff/cheatsheets-DE/blob/master/DockerCheatSheet.md). 90 | 91 | Zunächst musst du die Docker Images bauen. Wechsel in das Verzeichnis 92 | `docker` und starte `docker-compose build`. Das lädt die Basis-Images 93 | herunter und installiert die Software in die Docker Images: 94 | 95 | ``` 96 | [~/microservice-consuldns/docker]docker-compose build 97 | .... 98 | Removing intermediate container 1d59f8227b12 99 | Step 4/4 : EXPOSE 8989 100 | ---> Running in 11e7fbacfa01 101 | ---> 9cfa7772986f 102 | Removing intermediate container 11e7fbacfa01 103 | Successfully built 9cfa7772986f 104 | ``` 105 | 106 | Danach sollten die Docker Images erzeugt worden sein. Sie haben das 107 | Präfix `msconsul`: 108 | 109 | ``` 110 | [~/microservice-consuldns/docker]docker images 111 | REPOSITORY TAG IMAGE ID CREATED SIZE 112 | msconsuldns_order latest 12b279e78975 52 seconds ago 225MB 113 | msconsuldns_apache latest 22fac099ba93 55 seconds ago 255MB 114 | msconsuldns_catalog latest c23c535ecaf6 2 minutes ago 225MB 115 | msconsuldns_customer latest a780e4f49bac 2 minutes ago 225MB 116 | ``` 117 | 118 | Ermittle die IP-Adresse deines Systems und weise sie der Umgebungsvariable 119 | CONSUL_HOST. Diese Umgebungsvariable ist notwendig, damit die Docker 120 | Container Consul für die DNS-Anfragen nutzen können: 121 | 122 | ``` 123 | [~/microservice-consuldns/docker]ifconfig 124 | .... 125 | en5: flags=8863 mtu 1500 126 | .... 127 | inet 192.168.1.152 netmask 0xffffff00 broadcast 192.168.1.255 128 | .... 129 | [~/microservice-consuldns/docker]export CONSUL_HOST=192.168.1.152 130 | ``` 131 | 132 | 133 | Nun kannst Du die Container mit `docker-compose up -d` starten. Die 134 | Option `-d` bedeutet, dass die Container im Hintergrund gestartet 135 | werden und keine Ausgabe auf der Kommandozeile erzeugen. 136 | 137 | ``` 138 | [~/microservice-consuldns/docker]docker-compose up -d 139 | Creating network "msconsuldns_default" with the default driver 140 | Pulling consul (consul:0.7.2)... 141 | 0.7.2: Pulling from library/consul 142 | b7f33cc0b48e: Already exists 143 | a4ca795d20eb: Pull complete 144 | 76bc5ef06918: Pull complete 145 | 965e633cb8c2: Pull complete 146 | 64e424fcbe65: Pull complete 147 | Digest: sha256:ce15f85417a0cf121d943563dedb873c7d6c26e9b1e8b47bc2f1b5a3e27498e1 148 | Status: Downloaded newer image for consul:0.7.2 149 | Creating msconsuldns_consul_1 ... 150 | Creating msconsuldns_consul_1 ... done 151 | Creating msconsuldns_order_1 ... 152 | Creating msconsuldns_catalog_1 ... 153 | Creating msconsuldns_customer_1 ... 154 | Creating msconsuldns_apache_1 ... 155 | Creating msconsuldns_order_1 156 | Creating msconsuldns_catalog_1 157 | Creating msconsuldns_apache_1 158 | Creating msconsuldns_apache_1 ... done 159 | ``` 160 | 161 | Wie man sieht, werden nun auch noch Docker Images heruntergeladen. 162 | 163 | Du kannst nun überprüfen, ob alle Docker Container laufen: 164 | 165 | ``` 166 | [~/microservice-consuldns/docker]docker ps 167 | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 168 | 2697aa91700b msconsuldns_apache "/bin/sh -c '/usr/..." 4 minutes ago Up 4 minutes 0.0.0.0:8080->80/tcp msconsuldns_apache_1 169 | 948f2576b0b0 msconsuldns_customer "/bin/sh -c '/usr/..." 4 minutes ago Up 4 minutes 8080/tcp msconsuldns_customer_1 170 | 0574e8dc5b11 msconsuldns_order "/bin/sh -c '/usr/..." 4 minutes ago Up 4 minutes 8080/tcp msconsuldns_order_1 171 | 144542583a05 msconsuldns_catalog "/bin/sh -c '/usr/..." 4 minutes ago Up 4 minutes 8080/tcp msconsuldns_catalog_1 172 | c28d2a38f657 consul:0.7.2 "docker-entrypoint..." 4 minutes ago Up 4 minutes 8300-8302/tcp, 8400/tcp, 8600/tcp, 8301-8302/udp, 0.0.0.0:8500->8500/tcp, 0.0.0.0:8600->8600/udp msconsuldns_consul_1 173 | ``` 174 | `docker ps -a` zeigt auch die terminierten Docker Container an. Das 175 | ist nützlich, wenn ein Docker Container sich sofort nach dem Start 176 | wieder beendet.. 177 | 178 | Wenn einer der Docker Container nicht läuft, kannst du dir die Logs 179 | beispielsweise mit `docker logs msconsuldns_apache_1` anschauen. Der Name 180 | der Container steht in der letzten Spalte der Ausgabe von `docker 181 | ps`. Das Anzeigen der Logs funktioniert auch dann, wenn der Container 182 | bereits beendet worden ist. Falls im Log steht, dass der Container 183 | `killed` ist, dann hat Docker den Container wegen Speichermangel 184 | beendet. Du solltest Docker mehr RAM zuweisen z.B. 4GB. Unter Windows 185 | und macOS findet sich die RAM-Einstellung in der Docker application 186 | unter Preferences/ Advanced. 187 | 188 | Um einen Container genauer zu untersuchen, kannst du eine Shell in dem 189 | Container starten. Beispielsweise mit `docker exec -it 190 | msconsuldns_catalog_1 /bin/sh` oder du kannst in dem Container ein 191 | Kommando mit `docker exec msconsuldns_catalog_1 /bin/ls` ausführen. 192 | 193 | Du kannst auf die Microservices unter http://localhost:8080/ 194 | zugreifen und 195 | auf das Consul Dashboard unter http://localhost:8500 . 196 | 197 | Mit `docker-compose down` kannst Du alle Container beenden. 198 | 199 | Wenn der Order-Container die anderen Container nicht finden kann, 200 | schalte die Firewall auf dem Docker Host aus. Zumindest unter Mac OS X 201 | unterbindet die Firewall die DNS-Zugriffe. 202 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. --------------------------------------------------------------------------------