Ribbon调用

一. 创建微服务的提供者工程

  1. 编辑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);
    }
}
  1. 编辑配置信息application.yml

spring:
  application:
    name: eureka-client-producer
server:
  port: 8080
eureka:
  client:
    service-url:
      defaultZone: http://admin:123456@127.0.0.1:8761/eureka/
  1. 编写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);
    }
}

二. 创建微服务的消费者工程

  1. 编辑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();
    }
}
  1. 编辑application.yml配置文件

spring:
  application:
    name: eureka-client-consumer
server:
  port: 80
eureka:
  client:
    service-url:
      defaultZone: http://admin:123456@127.0.0.1:8761/eureka/
  1. 编写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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值