Springcloud微服务注册中心-Eureka 2.X(二)——服务消费者调用服务提供者

1.Eureka服务注册中心运行原理

 

2.创建服务提供者,服务消费者

  • 创建基本的Eureka-client工程,
app-eureka-clientOrder是服务消费者  app-eureka-clientMember是服务提供者  Springcloud-Eureka是服务注册中心
  • 服务提供者app-eureka-clientMember(服务别名)启动服务:
package com.eureka.eurekaclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {

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

}
  • 服务提供者app-eureka-clientMember(服务别名)服务接口:
package com.eureka.eurekaclient.Member;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MemberController {
    @RequestMapping("/getMember")
    public String getMember() {
        return "this is getMember";
    }
}
  • 服务消费者app-eureka-clientOrder(服务别名)启动消费者服务:
package com.eureka.eurekaclient;

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 EurekaClientApplication {

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

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }


}
  • 服务消费者app-eureka-clientOrder(服务别名)使用Rest方式调用服务:
package com.eureka.eurekaclient.Order;

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 OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/getOrder")
    public String getOrder() {
        // order 使用rpc 远程调用技术 调用 会员服务
        String memberUrl = "http://app-eureka-clientMember/getMember";
        String result = "会员服务调用订单服务" + restTemplate.getForObject(memberUrl, String.class);
        System.out.println("会员服务调用订单服务,result:" + result);
        return result;
    }
}

以服务别名方式调用服务提供者接口,这个需要依赖于负载均衡器Ribbon,这就需要在启动类中加上注解@LoadBalanced

 

3.服务调用实测

服务注册中心:127.0.0.1:8100

服务提供者 :127.0.0.1:8003

服务消费者:127.0.0.1:8002

  • 访问服务提供者:127.0.0.1:8003

  • 访问服务消费者:127.0.0.1:8002

预期结果

        String memberUrl = "http://app-eureka-clientMember/getMember";
        String result = "会员服务调用订单服务" + restTemplate.getForObject(memberUrl, String.class);

项目源代码

链接:https://pan.baidu.com/s/1bxP7ZL6fDJdcJ6zscUdN6g 
提取码:zmpi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值