Program 1: HelloWorld Microservice:
===================================
Step1: Project name: Create a Maven Project with the name: Microservice-
DiscoveryServiceServer
-----
Step 2: Add the following dependencies in [Link]
------
[Link]:
========
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]
<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</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>[Link]</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath/>
</parent>
<properties>
<[Link]>UTF-8</[Link]>
<[Link]>UTF-
8</[Link]>
<[Link]>1.8</[Link]>
</properties>
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<!-- Eureka registration server -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Step 3:
-------
Create [Link] file under /main/resources folder:
src/main/resources/[Link]:
====================================
# Configure this Discovery Server
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
# HTTP (Tomcat) port
server:
port: 4567
Step 4:
-------
Create [Link]:
==========================================================
package [Link];
import [Link];
import [Link];
import [Link];
@SpringBootApplication
@EnableEurekaServer
public class MicroServiceDiscoveryServiceServerApplication {
public static void main(String[] args) {
[Link]([Link],
args);
[Link]("Discovery Service Server started");
}
}
Step 5:
-------
Right click this and run as Spring Boot Application
Step 6:
------
Open the browser and point to the URL [Link]
You should see the Eureka home page
Step 7:
-------
Now, let's create another Java project for producing HelloWorld microservice and
registers in Eureka server.
Create Apache Maven project: Project name: HelloWorldMicroservice
Add the following dependency in [Link]
========================================
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]
<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>HelloWorldMicroservice</artifactId>
<version>Version_1</version>
<packaging>jar</packaging>
<name>helloworld-microservice</name>
<description>Hello World microservice</description>
<parent>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<[Link]>UTF-8</[Link]>
<[Link]>UTF-
8</[Link]>
<[Link]>1.8</[Link]>
</properties>
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Step 8:
=======
Create the controller class in this project now:
[Link]:
==========================
package [Link];
import [Link];
import [Link];
@RestController
public class HelloWorldController {
@RequestMapping("/hello")
public String sayHello()
{
return "Hello World Microservice";
}
}
Step 9:
=======
src/main/resources/[Link]:
====================================
### Spring properties
# Service registers under this name
spring:
application:
name: helloworld-microservice
# Discovery Server Access
eureka:
client:
serviceUrl:
defaultZone: [Link]
# HTTP Server (Tomcat) Port
server:
port: 4568
Step 10:
=======
Create the main Program:
[Link]:
=======================================
package [Link];
import [Link];
import [Link];
import [Link];
@SpringBootApplication
@EnableDiscoveryClient
public class HelloWorldMicroserviceApplication {
public static void main(String[] args) {
[Link]([Link], 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: [Link]
After few seconds, you would see that HELLOWORLD-MICROSERVICE is registered with
Eureka and see the listing
Step 13:
========
Click on the home page for this microservice HELLOWORLD-MICROSERVICE
It will be: [Link]
But, in the browser, you would see that there is no URI mapping for this in the
controller class
Just change the url to:
[Link]
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.
Class Name: Microservice-DiscoveryServer
========================================
package [Link];
import [Link];
import [Link];
import [Link];
@SpringBootApplication
@EnableEurekaServer
public class MicroserviceDiscoveryServerApplication {
public static void main(String[] args) {
[Link]([Link],
args);
[Link]("Discovery Server started");
}
}
Now, create Customer microservice:
Create Maven project: Microservice-Customer
[Link]:
=======
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]
<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</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>[Link]</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<[Link]>UTF-8</[Link]>
<[Link]>UTF-
8</[Link]>
<[Link]>1.8</[Link]>
</properties>
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
[Link]:
==========================================
package [Link];
import [Link];
import [Link];
import [Link];
@SpringBootApplication
@EnableDiscoveryClient
public class MicroserviceCustomerServerApplication {
public static void main(String[] args) {
[Link]([Link],
args);
}
}
/src/resources/[Link]:
================================
### Spring properties
# Service registers under this name
spring:
application:
name: customers-microservice
# Discovery Server Access
eureka:
client:
serviceUrl:
defaultZone: [Link]
# HTTP Server (Tomcat) Port
server:
port: 1235
[Link] (incomplete this code):
===================================
package [Link];
public class Customer {
int customerId;
String customerName;
Address address;
// Generate default and parameterized constructors and setters/getters
}
[Link] (incomplete this code):
===================================
class Address
{
int flatNo;
String streetName;
String city;
long pinCode;
// Generate default and parameterized constructors and setters/getters
}
[Link]:
========================
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@RestController
public class CustomerController {
Address a1=new Address(5,"Golden Globe Apt","Bangalore",560102L);
Customer c1=new Customer(1, "Guru", a1);
Address a2=new Address(23,"Vaswani Techno","Bangalore",560155L);
Customer c2=new Customer(2, "Peter", a2);
Address a3=new Address(14,"SRH Building","Mumbai",234552L);
Customer c3=new Customer(1, "Mukul", a3);
Address a4=new Address(500,"Mantri Apt","Bangalore",560105L);
Customer c4=new Customer(1, "Deepak", a4);
Address a5=new Address(104,"Shobha Apt","Bangalore",560155L);
Customer c5=new Customer(1, "Pradeep", a5);
ArrayList<Customer> allCustomers=new
ArrayList([Link](c1,c2,c3,c4,c5));
@RequestMapping(value="/customers",method=[Link])
public ArrayList<Customer> getCustomersList()
{
// Complete the coding
}
@RequestMapping(value="/customers/{customerid}",method=[Link])
public Customer getCustomer(@PathVariable(value="customerid") int customerid)
{
// Complete the coding
}
// Incomplete code
public Customer addACustomer()
{
// Complete the coding
}
@RequestMapping(value="/info",method=[Link])
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;
}
Program 3: Create Stock microservice:
======================================
Maven Project name: Microservice-stock
[Link]:
Same as above
[Link]: (incomplete code)
=============================
package [Link];
public class Stock
{
int stockId;
String stockName;
double stockPrice;
....
}
[Link]:
=====================
Create 5 stock objects and add into an ArrayList
@RequestMapping("/stocks")
public ArrayList<Stock> listAllStocks()
{
// write your logic
}
@RequestMapping("/stocks/{stockId}")
public Stock getAStock(@PathVariable("stockId") int stockId)
{
// write your logic
}
[Link]:
=========================================
It has the main method
Same as above
Right click this class and run as Spring boot application
You should now see two microservices got registered in Eureka server
Program 4: Microservices communication:
=======================================
In [Link], add the following:
public static final String STOCKS_SERVICE_URL = "[Link]
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
public RemoteStockRepository accountRepository(){
return new RemoteStockRepository(STOCKS_SERVICE_URL);
}
Create a new java class RemoteStockRepository as follows:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class RemoteStockRepository {
@Autowired
protected RestTemplate restTemplate;
protected String serviceUrl;
Stock stocks[];
ArrayList<Stock> stockList;
public RemoteStockRepository(String serviceUrl) {
[Link] = [Link]("http") ? serviceUrl
: "[Link] + serviceUrl;
}
public void getAllStocks() {
stocks = [Link](serviceUrl+"/stocks",
Stock[].class);
stockList=new ArrayList<Stock>([Link](stocks));
public Stock getStock(int stockId) {
return [Link](serviceUrl + "/stocks/{stockId}",
[Link], stockId);
}
In [Link], do the following changes:
@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=[Link])
public List<Stock> getCustomerStocks(@PathVariable(value="customerid") int
customerid)
{
[Link]("Entering getCustomerStocks");
List<Stock> allStocks=new ArrayList();
Customer cust=null;
for (Customer c:allCustomers)
{
if ([Link]()==customerid)
{
cust=c;
}
}
int stockIds[]=[Link]();
for (int i=0;i<[Link];i++)
{
Stock stock=[Link](stockIds[i]);
[Link](stock);
}
return allStocks;