Professional Documents
Culture Documents
===================================
Step1: Project name: Create a Maven Project with the name: Microservice-
DiscoveryServiceServer
-----
<groupId>com.ofss</groupId>
<artifactId>MicroserviceDSS</artifactId>
<version>Version1</version>
<packaging>jar</packaging>
<name>discovery-microservice-server</name>
<description>Microservice discovery server using Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-
8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Step 3:
-------
src/main/resources/application.yml:
====================================
Step 4:
-------
Create MicroServiceDiscoveryServiceServerApplication.java:
==========================================================
package com.ofss;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class MicroServiceDiscoveryServiceServerApplication {
SpringApplication.run(MicroServiceDiscoveryServiceServerApplication.class,
args);
System.out.println("Discovery Service Server started");
}
}
Step 5:
-------
Step 6:
------
Step 7:
-------
Now, let's create another Java project for producing HelloWorld microservice and
registers in Eureka server.
<groupId>com.ofss</groupId>
<artifactId>HelloWorldMicroservice</artifactId>
<version>Version_1</version>
<packaging>jar</packaging>
<name>helloworld-microservice</name>
<description>Hello World microservice</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-
8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Step 8:
=======
Create the controller class in this project now:
HelloWorldController.java:
==========================
package com.ofss;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@RequestMapping("/hello")
public String sayHello()
{
return "Hello World Microservice";
}
}
Step 9:
=======
src/main/resources/application.yml:
====================================
Step 10:
=======
HelloWorldMicroserviceApplication.java:
=======================================
package com.ofss;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class HelloWorldMicroserviceApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldMicroserviceApplication.class, args);
}
}
Step 11:
========
Right click this main program and run as Spring Boot application
Step 12:
========
Refresh the browser which already pointing to the URL: http://localhost:4567/
After few seconds, you would see that HELLOWORLD-MICROSERVICE is registered with
Eureka and see the listing
Step 13:
========
http://hp:4568/hello
You now can see "Hello World Microservice" message in the browser
------
Program 2:
=========
Creating Customer Microservice
Maven Project Name: For service discovery server, you can keep the same name or
create another one
It's up to you.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class MicroserviceDiscoveryServerApplication {
pom.xml:
=======
<groupId>com.ofss</groupId>
<artifactId>Microservice-Customer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>customer-microservice-server</name>
<description>It is microservice server of customers using Spring
Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-
8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
MicroServiceCustomerServerApplication.java:
==========================================
package com.ofss;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class MicroserviceCustomerServerApplication {
/src/resources/application.yml:
================================
package com.ofss;
class Address
{
int flatNo;
String streetName;
String city;
long pinCode;
CustomerController.java:
========================
package com.ofss;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CustomerController {
ArrayList<Customer> allCustomers=new
ArrayList(Arrays.asList(c1,c2,c3,c4,c5));
@RequestMapping(value="/customers",method=RequestMethod.GET)
public ArrayList<Customer> getCustomersList()
{
// Complete the coding
}
@RequestMapping(value="/customers/{customerid}",method=RequestMethod.GET)
public Customer getCustomer(@PathVariable(value="customerid") int customerid)
{
// Complete the coding
}
// Incomplete code
public Customer addACustomer()
{
// Complete the coding
}
@RequestMapping(value="/info",method=RequestMethod.GET)
public String info()
{
String msg="<h1>This is Customer microservice";
msg+="<br>This contains info about all customers";
msg+="<br>URI: /info to see this information";
msg+="<br>URI: /customers to see list of customers";
msg+="<br>URI: /customers/custid to see a specific customer<h1>";
return msg;
}
pom.xml:
Same as above
package com.ofss;
....
}
StockController.java:
=====================
@RequestMapping("/stocks")
public ArrayList<Stock> listAllStocks()
{
// write your logic
}
@RequestMapping("/stocks/{stockId}")
public Stock getAStock(@PathVariable("stockId") int stockId)
{
// write your logic
}
MicroServiceStockServerApplication.java:
=========================================
It has the main method
Same as above
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
public RemoteStockRepository accountRepository(){
return new RemoteStockRepository(STOCKS_SERVICE_URL);
}
package com.ofss;
import java.util.ArrayList;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;
@Autowired
protected RestTemplate restTemplate;
@Autowired
RemoteStockRepository remoteStockRepository;
Add this new method, which is reponsible for fetching all Stocks for a given
customer id
@RequestMapping(value="/customers/{customerid}/stocks",method=RequestMethod.GET)
public List<Stock> getCustomerStocks(@PathVariable(value="customerid") int
customerid)
{
System.out.println("Entering getCustomerStocks");
List<Stock> allStocks=new ArrayList();
Customer cust=null;
}
int stockIds[]=cust.getStockIds();