在分布式系统中,各个服务可能处于不同主机,但是服务之间不可避免的需要相互调用,这个称为远程调用。
之前我们在项目的开发过程中,只需要创建一个工程,将所有功能放入一个项目进行开发,管理,变异,测试,打包。

所以以前的项目都被称为单体架构 。
但由于单体架构存在明显的缺陷,这时我们需要用到分布式系统。

远程过程调用(RPC):RPC是由Sun发明的远程过程调用协议,是第一种真正的分布式应用模型。

RPC远程调用框架:
(1)RMI实现
(2)Hessian
(3)thrift
(4)SpringCloud
(5) Dubbo
下面我们将使用socket原生的httpclient进行远程调用,具体案例代码如下:

一:eureka的服务端配置

1.创建一个工程,在该工程下创建三个module

怎么远程调用其他服务java 远程系统调用_java


2.在eureka-server中导入eureka的依赖

<properties>
    <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
    <java.version>1.8</java.version>
</properties>


<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

3.spring boot启动类上加入@EnableEurekaServer

package com.zyc.server;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}

4.配置eureka服务端
application.properties

#eureka的服务端
server.port=8888


eureka.instance.hostname=localhost
#客户端调用eureka服务端的url
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

# eureka server 没必要自己把自己注册上去,所以可以设置成 false
eureka.client.register-with-eureka=false
# 是否从Eureka Server上获取注册信息,默认为true,此处建议修改成 false
# (单机设置的意义不大,如果设置成 true 启动会去抓取一次注册表,获取不到更新缓存就会出错(该错误不影响 eureka 正常使用))
eureka.client.fetch-registry=false

5.在浏览器访问http:localhost:8888,出现如下页面:

怎么远程调用其他服务java 远程系统调用_java_02

二:eureka的客户端的配置

1.加入eureka客户端依赖

<properties>
    <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
    <java.version>1.8</java.version>
</properties>


<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

2.spring boot启动类上加入@EnableEurekaClient

ServiceOrderApplication

package com.zyc.order;

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

    public static void main(String[] args) {

        SpringApplication.run(ServiceOrderApplication.class, args);
    }

    @Bean
    @LoadBalanced  //负载均衡  ribbon
    public RestTemplate restTemplate(){
        return new RestTemplate();//httpClient
    }

}

注意:RestTemplate进行远程调用在RestTemplate的Bean注解上加入
@LoadBalanced 负载均衡 ribbon ,否者远程调用无法根据服务的名称找到服务

ServiceProductApplication

package com.zyc.product;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ServiceProductApplication {

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

}

3.配置eureka客户端
application.properties(product):

server.port=8081

#需要配置到eurekaserver的服务名称
spring.application.name=service-product

#服务注册到eureka注册中心的地址
eureka.instance.hostname=localhost

eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/


###因为该应用为服务提供者,是eureka的一个客户端,需要注册到注册中心
eureka.client.register-with-eureka=true
###是否需要从eureka上检索服务
eureka.client.fetch-registry=true

application.yml(order):

#注册中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/
    register-with-eureka: true
    fetch-registry: true
spring:
  application:
    name: service-order

4.远程调用
OrderService

package com.zyc.order.service;

import com.zyc.order.entity.Order;
import com.zyc.order.entity.Product;
import com.zyc.order.entity.Order;
import com.zyc.order.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;


@Service
public class OrderService {

    // Spring框架对RESTful方式的http请求做了封装,来简化操作  (底层 httpClient)
    @Autowired
    private RestTemplate restTemplate;


    public Order createOrder(){
        //1.创建订单对象
        Order order= new Order();
        order.setOid(100010);
        order.setUid(3309);
        //2.调用商品服务--查询所有商品的信息----
        //Product[] productArray = restTemplate.getForObject("http://127.0.0.1:8081/product", Product[].class);

        Product[] productArray = restTemplate.getForObject("http://service-product/product", Product[].class);
        order.setProductList(Arrays.asList(productArray));
        //2.返回
        return order;
    }

}

OrderController

package com.zyc.order.controller;


import com.zyc.order.entity.Order;
import com.zyc.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/order")
public class OrderController {
     @Autowired
     private OrderService orderService;

    @PostMapping
    public Order create(){
        Order order = orderService.createOrder();
        return order;
    }
}

ProductController

package com.zyc.product.controller;

import com.zyc.product.entity.Product;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RestController  //http+restful+json
@RequestMapping("/product")
public class ProductController {


    @GetMapping
    public List<Product> list() {
        List<Product> products = getDateFromDB();
        return products;
    }

    private List<Product> getDateFromDB() {
        List<Product> products = new ArrayList<>();
        products.add(new Product(1, "豹纹围巾", new BigDecimal("99.9")));
        products.add(new Product(2, "豹纹袜子", new BigDecimal("9.9")));
        products.add(new Product(3, "豹纹手套", new BigDecimal("89.9")));
        products.add(new Product(4, "豹纹鞋子", new BigDecimal("45.9")));
        return products;

    }


}