SpringCloud 入门2(服务消费)

上一篇完成了注册中心,服务提供者,这篇来完成服务消费,当然,不管是服务提供者还是消费者,对于注册中心来说,就是注册在注册中心的一个微服务。

1. rabbon方式微服务消费

新建springboot工程,选择支持如下,其中Discovery和Server任选一个就可以了


配置文件:

eureka:
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://localhost:8000/eureka/
server:
  port: 8002
spring:
  application:
    name: ribbon-client
  cloud:
    config:
      discovery: 
        enabled: true
        serviceId: eureka-server

主类添加支持

@EnableEurekaClient
@SpringBootApplication
public class RibbonClientApplication {

	public static void main(String[] args) {
		SpringApplication.run(RibbonClientApplication.class, args);
	}
	
	
    @Bean
    @LoadBalanced  //负载均衡方式调用
    RestTemplate restTemplate() {    //rest调用模板
        return new RestTemplate();
    }
}

编写一个service调用服务提供者 的服务:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    public String hiService(String name) {
        return restTemplate.getForObject("http://EUREKA-CLIENT/hello?name="+name,String.class);
    }

}

对外提供一个接口测试:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloControler {

    @Autowired
    HelloService helloService;
    @RequestMapping(value = "/hello")
    public String hi(@RequestParam String name){
        return helloService.hiService(name);
    }

}

启动程序。观察注册中心页面注册成功


调用8002的接口,8001服务提供者返回数据:


注意:客户端使用的是@EnableEurekaClient,而不是@EnableDiscoveryClient,

否则可能无法注册到注册中心,调用服务时报错:No instances available for xxxx

原因:https://blog.csdn.net/u012734441/article/details/78256256?locationNum=1&fps=1

2. feign方式微服务消费


配置文件同rabbon一样

主运行类:

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class FeignClientApplication {

	public static void main(String[] args) {
		SpringApplication.run(FeignClientApplication.class, args);
	}
}

调用服务提供接口

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "eureka-client")
public interface HelloService {
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    String restFromClientOne(@RequestParam(value = "name") String name);
}

测试接口:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    HelloService helloService;
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String sayHi(@RequestParam String name){
        return helloService.restFromClientOne(name);
    }
}

测试方法同rabbon

3. 负载均衡

目前为止,我们有4个工程:

注册中心:eurekaServer

服务提供者:eurekaClient

服务消费者:ribbonClient 和feignClient

负载测试方案:(1)eurekaServer启动不变 

                      (2)eurekaClient 启动三个实例(把eurekaClient启动三次),注意每次分别改动启动端口为:8001,8002, 8003

                       (3)选取ribbonClient或者feignClient之一,启动端口改为8010

全部启动后,注册中心实例列表 如下:


调用测试接口:http://localhost:8010/hello?name=gray,不断刷新,发现接口提供者在三个实例之前切换,效果如下:



这也是我们在消费时使用服务名而非ip+端口的形式访问,因为后者或锁定实例,失去负载均衡的意义。


目录:
SpringCloud 入门1(服务注册和服务发现)

SpringCloud 入门2(服务消费)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值