msa
Practical-2-
Create a simple microservice using spring boot
package
com.example.Microservices_demo;
import
org.springframework.boot.SpringApplication;
@SpringBootApplication
public class
MicroservicesDemoApplication {
public
static void main(String[] args) {
SpringApplication.run(MicroservicesDemoApplication.class, args);
}
}
Practical-3-
Implement Restful APIs in microservice
package
com.example.Microservices_demo.demo;
import
org.springframework.web.bind.annotation.GetM@apping;
import
org.springframework.web.bind.annotation.RestController;
@RestController
public class
MyController {
@GetMapping(“/home”)
public
String home() {
return “This is home page”;
}
}
Practical-4-
Implement Synchronous Communication using REST
package
com.example.Microservices_Demo.demo;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.http.HttpMethod;
import
org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import
org.springframework.web.bind.annotation.PathVariable;
import
org.springframework.web.bind.annotation.RestController;
import
org.springframework.web.client.RestTemplate;
@RestController
public class
mycontroller {
public
String home() {
@Autowired
private
RestTemplate restTemplate;
@GetMapping(“/{id}”)
public
ResponseEntity<String>getUserDetails(@PathVariable(“id”) String id) {
ResponseEntity<String>
result=restTemplate.exchange(“http://localhost:8081/salary/name”+id,HttpMethod.GET,null,String.class);
ResponseEntity<String>
result = null;
System.out.println(“====”+result.getBody()+”==”);
return
result;
}
}
package
salary;
import
org.springframework.web.bind.annotation.GetMapping;
import
org.springframework.web.bind.annotation.PathVariable;
public class
MyController {
@GetMapping(“/{id}”)
public
String getSalary(@PathVariable(“id”) String id) {
if(id.equals(“User_1”))
return “RS
7000”;
return id;
}
}
Practical-5-
Set up and configure a service registry(eg- eureka,consul)
package
com.example.registry_demo;
import
org.springframework.boot.SpringApplication;
import
org.springframework.boot.autoconfigure.SpringBootApplication;
import
org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class
EurekaDemoApplication {
public
static void main(String[] args)
{
SpringApplication.run(EurekaDemoApplication.class,
args);
}
}
spring.application.name=registry_demo
server.port=8761
Practical-6-
Implement client-side service discovery in microservice
package
com.example.pract6;
import
org.springframework.boot.SpringApplication;
@SpringBootApplication
@EanbleEurekaServer
@EnableDiscoveryClient
public class
PractApplication {
public
static void main(String[] args) {
SpringApplication.run(Pract6Application.class,
args);
}
}
Practical-7-
Implement an API Gateway using zuul or spring cloud gateway
package
com.example.pract8;
import org.springframework.boot.SpringApplication;
@SpringBootApplication
@EnableDiscoveryClient
@EnableEurekaServer
public class
Pract8Application {
public
static void main(String[] args) {
SpringApplication.run(Pract8Application.class,
args);
}
}
eureka:
client:
serviceUrl:
defaultzone:
http://localhost:8761/eureka/
Practical-9-
set up centralized configuration management using spring cloud config or consul
Practical-10-
Externalize microservice configuration and manage them centrally
package
com.example.pract_9;
import
org.springframework.boot.SpringApplication;
@SpringBootApplication
public class
Pract9Pract9Application {
public
static void main(String[] args) {
SpringApplication.run(Pract9Application.class,
args);
}
}
spring:
colude:
config:
server:
git:
url:https://github.com/Namita2002/config-repo
application:
name:
config-server
server:
port:9001
eureka:
client:
serviceUrl:
defaultZone:
htpp://localhost:8761/eureka/
Comments
Post a Comment