Ribbon调用
程序员文章站
2022-05-24 14:36:33
...
一. 创建微服务的提供者工程
- 编辑main方法
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
@EnableEurekaClient
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- 编辑配置信息application.yml
spring:
application:
name: eureka-client-producer
server:
port: 8080
eureka:
client:
service-url:
defaultZone: http://admin:[email protected]:8761/eureka/
- 编写Controller接口
package com.qf.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@GetMapping("get")
public String get(@RequestParam(value = "name", required = true) String name) {
return String.format("你好, %s. 我是GET方法", name);
}
@PostMapping("post")
public String post(@RequestParam(value = "name", required = true) String name) {
return String.format("你好, %s. 我是POST方法", name);
}
}
二. 创建微服务的消费者工程
- 编辑main方法
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
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 Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
- 编辑application.yml配置文件
spring:
application:
name: eureka-client-consumer
server:
port: 80
eureka:
client:
service-url:
defaultZone: http://admin:[email protected]:8761/eureka/
- 编写Controller接口
package com.qf.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class UserController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("get")
public String get() {
String response = restTemplate.getForObject("http://EUREKA-CLIENT-PRODUCER/get?name=微服务之SpringCloud", String.class);
return response;
}
@RequestMapping("post")
public String post() {
MultiValueMap<String, Object> parameters = new LinkedMultiValueMap<>();
parameters.add("name", "微服务之SpringCloud");
String response = restTemplate.postForObject("http://EUREKA-CLIENT-PRODUCER/post", parameters, String.class);
return response;
}
}