Microservices Development
Microservices Development
Microservices Development
spring:
application:
name: 01_Service_Registry
server:
port: 8761
eureka:
client:
register-with-eureka: false
Note-2 : If service-registry project running on any other port number then we have
to register clients with service-registry manually.
4) Once application started we can access Eureka Dashboard using below URL
URL : http://localhost:8761/
======================================
Steps to develop Spring Admin-Server
======================================
spring:
application:
name: 02_Admin_Server
server:
port: 1111
5) Access application URL in browser (We can see Admin Server UI)
URL : http://localhost:1111/
======================================
Steps to work with Zipkin Server
======================================
URL : https://zipkin.io/pages/quickstart.html
URL : http://localhost:9411/
#################################
Steps to develop WELCOME-API
#################################
- eureka-discovery-client
- admin-client
- zipkin
- starter-web
- devtools
- actuator
@RestController
public class WelcomeRestController {
@GetMapping("/welcome")
public String getWelcomeMsg() {
String msg = "Welcome To Ashok IT..!!";
return msg;
}
}
-----------------------------------------------------
spring:
application:
name: 04_Welcome_Service
boot:
admin:
client:
url: http://localhost:1111/
server:
port: 8081
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
management:
endpoints:
web:
exposure:
include: '*'
--------------------------------------------------------
5) Run the application and check in Eureka Dashboard (It should display in eureka
dashboard)
6) Check Admin Server Dashboard (It should display) (we can access application
details from here)
Ex: Beans, loggers, heap dump, thred dump, metrics, mappings etc...
#################################
Steps to develop GREET-API
#################################
- eureka-discovery-client
- admin-client
- zipkin
- starter-web
- devtools
- actuator
- openfeign
@RestController
public class GreetRestController {
@GetMapping("/greet")
public String getGreetMsg() {
String msg = "Good Morning";
return msg;
}
}
5) Run the application and check in Eureka Dashboard (It should display in eureka
dashboard)
6) Check Admin Server Dashboard (It should display) (we can access application
details from here)
Ex: Beans, loggers, heap dump, thred dump, metrics, mappings etc...
==============================
Interservice communication
==============================
@FeignClient(name = "WELCOME-API")
public interface WelcomeApiClient {
@GetMapping("/welcome")
public String invokeWelcomeMsg();
@RestController
public class GreetRestController {
@Autowired
private WelcomeApiClient welcomeClient;
@GetMapping("/greet")
public String getGreetMsg() {
return greetMsg.concat(welcomeMsg);
}
==================
Load Balancing
==================
=> If we run our application in one server then burden will be increased on that
server.
2) Burden on server
3) Response delay
=> To overcome above problems we will run our application in multiple servers so
that we can distribute the requests to multiple servers.
===============================
Load Balancing For Welcome API
===============================
@RestController
public class WelcomeRestController {
@Autowired
private Environment env;
@GetMapping("/welcome")
public String getWelcomeMsg() {
String port = env.getProperty("server.port");
String msg = "Welcome To Ashok IT..!! (" + port + ")";
return msg;
}
}
3) Right click => Run as => run configuration => select welcome-api => VM Arguments
=> -Dserver.port=8081 and apply and run it.
4) Right click => Run as => run configuration => select welcome-api => VM Arguments
=> -Dserver.port=8082 and apply and run it.
5) Right click => Run as => run configuration => select welcome-api => VM Arguments
=> -Dserver.port=8083 and apply and run it.
Note: With this our api will run in 3 servers with 3 diff port numbers.
========================================
Working with Spring Cloud API Gateway
========================================
-> eureka-client
-> cloud-reactive-gateway
-> devtools
spring:
application:
name: API-Gateway
cloud:
gateway:
routes:
- id: api-1
uri: lb://WELCOME-SERVICE
predicates:
- Path=/welcome
- id: api-2
uri: lb://GREET-SERVICE
predicates:
- Path=/greet
server:
port: 3333
4) Create Filter to validate incoming request
Secret=ashokit@123
@Component
public class MyFilter implements GlobalFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain
chain) {
// validate request
if(!keySet.contains("Secret")) {
throw new RuntimeException("Invalid Request");
}
return chain.filter(exchange);
}
}
================================
What is Cloud Config Server
================================
=> If we want to make any changes to properties then we have to re-package our
application and we have to re-deploy our application.
Note: If any changes required in config properties then We have to repeat the
complete project build & deployment which is time consuming process.
=> To avoid this problem, we have to seperate our project source code and project
config properties files.
=> To externalize config properties from the application we can use Spring Cloud
Config Server.
Note: Application config properties files we will maintain in git hub repo and
config server will load them and will give to our application based on our
application-name.
=> Our microservices will get config properties from Config server and config
server will load them from git hub repo.
================================
Developing Config Server App
================================
1) Create Git Repository and keep ymls files required for microservices
a) Config Server
b) devtools
spring:
application:
name: 07_Cloud_Config_Server
cloud:
config:
server:
git:
uri: https://github.com/ashokitschool/configuration_properties
server:
port: 9093
5) Run Config Server application
=================================
Config Client Development
=================================
a) web-starter
b) config-client
c) dev-tools
d) actuators
@RestController
public class MsgRestController {
@Value("${msg}")
private String msg;
@GetMapping("/")
public String getMsg() {
return msg;
}
spring:
application:
name: welcome
config:
import: optional:configserver:http://localhost:9093
==============================================
How to reload config properties dynamically
==============================================