Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 68 additions & 68 deletions 16.Microservices-Registering-Service-With-Eureka/README.md
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
* Create a Project with following dependencies


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

* Create Person class

public class Person {
private int id;
private String firstName;
private String lastName;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(int id, String firstName, String lastName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Person [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
public class Person {
private int id;
private String firstName;
private String lastName;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(int id, String firstName, String lastName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Person [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}

}
}

* Create a RestController


@RestController
public class PersonController {
@RequestMapping("/person/{id}")
public Person getPerson(@PathVariable int id){

switch(id){
case 1: return new Person(1, "Justin", "Bieber");
case 2: return new Person(2, "Taylor", "Swift");
case 3: return new Person(3, "Ed", "Sheeran");
@RestController
public class PersonController {
@RequestMapping("/person/{id}")
public Person getPerson(@PathVariable int id){

switch(id){
case 1: return new Person(1, "Justin", "Bieber");
case 2: return new Person(2, "Taylor", "Swift");
case 3: return new Person(3, "Ed", "Sheeran");
}
return null;
}
}
return null;
}
}

* Start the server and test the Service in browser
*http://localhost:8080/person/3*
Expand All @@ -77,24 +77,24 @@

* Annotate Main class with @EnableEurekaClient

@EnableEurekaClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@EnableEurekaClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

* Create bootstrap.properties file in resource folder and add following code to it

spring.application.name=person-service
spring.application.name=person-service

* Add following code to application.properties

server.port=0
# Runs the app on available random port
#eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.instance.instance-id=${spring.application.name}:${random.int}
eureka.instance.hostname=localhost
server.port=0
# Runs the app on available random port
#eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.instance.instance-id=${spring.application.name}:${random.int}
eureka.instance.hostname=localhost
138 changes: 69 additions & 69 deletions 17.Microservices-Discovering-Service-With-Eureka/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,85 +21,85 @@

* application.properties

server.port=8082
#eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
server.port=8082
#eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true

* Main class


@EnableEurekaClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@EnableEurekaClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

* Controller Class


@Controller
public class PersonController {
@Autowired
private RestTemplate restTemplate;
@LoadBalanced
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@RequestMapping(path="/persondetails", params={"personid"})
public String getPersonDetails(@RequestParam String personid, Model m){
Person p = restTemplate.getForObject("http://person-service/person/" + personid, Person.class);
m.addAttribute("person", p);
return "console";
}
@RequestMapping("/")
public @ResponseBody String hello(){
return "hello";
}
}
@Controller
public class PersonController {
@Autowired
private RestTemplate restTemplate;
@LoadBalanced
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@RequestMapping(path="/persondetails", params={"personid"})
public String getPersonDetails(@RequestParam String personid, Model m){
Person p = restTemplate.getForObject("http://person-service/person/" + personid, Person.class);
m.addAttribute("person", p);
return "console";
}
@RequestMapping("/")
public @ResponseBody String hello(){
return "hello";
}
}

* Person class

public class Person {
private int id;
private String firstName;
private String lastName;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(int id, String firstName, String lastName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Person [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
}
public class Person {
private int id;
private String firstName;
private String lastName;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(int id, String firstName, String lastName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Person [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
}