Update: T05/搭建service-consumer模块
This commit is contained in:
+11
@@ -0,0 +1,11 @@
|
||||
package cn.hezhaohui.serviceconsumer;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ServiceConsumerApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ServiceConsumerApplication.class, args);
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package cn.hezhaohui.serviceconsumer.config;
|
||||
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@Configuration
|
||||
public class RestTemplateConfig {
|
||||
|
||||
@Bean
|
||||
@LoadBalanced
|
||||
public RestTemplate restTemplate () {
|
||||
return new RestTemplate();
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package cn.hezhaohui.serviceconsumer.controller;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
public class ConsumeController {
|
||||
|
||||
@Resource
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@GetMapping("/print")
|
||||
public String print() {
|
||||
String number = restTemplate.getForObject("http://service-producer/api/number", String.class);
|
||||
return "[Generate]: " + number;
|
||||
}
|
||||
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package cn.hezhaohui.serviceconsumer.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
public class HealthController {
|
||||
|
||||
@Value("${test.message}")
|
||||
private String testMessage;
|
||||
|
||||
@GetMapping("/health")
|
||||
public String health() {
|
||||
return "Wow, nice to meet you!" + "\n" + testMessage;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user