Part 1 to install Eureka Server and let it discover the Eureka client
application.properties
server.port=8080 spring.application.name=consumer-service
POM.xml
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.4.RELEASE com.example Tuto10EurekaClientConsumer 0.0.1-SNAPSHOT Tuto10EurekaClientConsumer Demo project for Spring Boot 1.8 Greenwich.SR1 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-devtools runtime org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
Tuto10EurekaClientConsumerApplication
package com.example.clientConsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
public class Tuto10EurekaClientConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(Tuto10EurekaClientConsumerApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
ClientConsumerController
package com.example.clientConsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ClientConsumerController {
@Autowired
RestTemplate restTemplate;
@RequestMapping("/consumer")
public String getClientMessage() {
String url = "http://localhost:8081/hello";
String url1 = "http://myclient-service/hello";
String res = restTemplate.getForObject(url1, String.class);
return "Message : "+res;
}
}
This is just a trick, hope it paved your way out.
No comments:
Post a Comment