要实现远程调用,主要需要三个module:一个注册中心、一个服务提供者、一个服务消费者,然后进行各自的配置和编码,详细内容如下:

1、建一个空的project,创建3各module

  a、注册中心模块 eureka-server,依赖Eureka Server

  b、服务提供者 provider-ticket,依赖Eureka Discovery Client

  c、服务消费者 consumer-user,依赖Eureka Discovery Client

 2、注册中心相关

  a、编写application.yml

  b、在主类上加上注解@EnableEurekaServer

3、编写服务提供者

  a、编写TicketService类

  b、编写TicketController类

  c、配置application.yml

  d、启动应用,访问controller的方法看是否成功

  e、8001端口用maven打包一次,8002端口用maven打包一次,然后在cmd窗口分别用java -jar启动两个jar包,可在注册中心中查看到注册了两个服务,

    此步骤是为了服务消费者调用的时候能看到负载均衡的效果

4、编写服务消费者

  a、配置application.yml

  b、在主类中使用@EnableDiscoveryClient注解来发现注册中心的服务,并且在主类中将RestTemplate类以@Bean的形式注册到容器中,使用

    @LoadBalanced注解来达到远程调用服务提供者的时候使用负载均衡机制

  c、编写UserController类,该类中使用RestTemplate来调用远程服务

2、3、4步骤具体如下:

2、注册中心相关

  a、编写application.yml

#访问端口8761
server:
port: 8761
eureka:
instance:
hostname: eureka-server #注册中心实例名
client:
register-with-eureka: false #不注册自己
fetch-registry: false #不发现自己
service-url:
defaultZone: http://localhost:8761/eureka/ #服务提供者和消费者需要绑定的注册中心地址

  b、在主类上加上注解@EnableEurekaServer

package com.example;

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

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

}

3、编写服务提供者

  a、编写TicketService类

package com.example.providerticket.service;

import org.springframework.stereotype.Service;

@Service
public class TicketService {
public String getTicket(){
System.out.println("端口是:8002");
return "《厉害了,我的国》";
}
}  

  b、编写TicketController类

package com.example.providerticket.controller;

import com.example.providerticket.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TicketController {
@Autowired
TicketService ticketService;
//消费方调用该方法时也使用该路径
@GetMapping("/ticket")
public String getTicket(){
return ticketService.getTicket();
}
}
启动服务,并访问如下:

  c、配置application.yml

server:
port: 8002 #访问端口8002,打不同端口包时可改为8001
spring:
application:
name: provider-ticket #名字可以任意取
eureka:
instance:
prefer-ip-address: true #使用ip地址
client:
service-url:
defaultZone: http://localhost:8761/eureka/ #与注册中心填写的内容相同

  d、启动应用,访问controller的方法看是否成功

  e、8001端口用maven打包一次,8002端口用maven打包一次,然后在cmd窗口分别用java -jar启动两个jar包,可在注册中心中查看到注册了两个服务,

    此步骤是为了服务消费者调用的时候能看到负载均衡的效果

4、编写服务消费者

  a、配置application.yml

server:
port: 8200
spring:
application:
name: consumer-user
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:8761/eureka/

  b、在主类中使用@EnableDiscoveryClient注解来发现注册中心的服务,并且在主类中将RestTemplate类以@Bean的形式注册到容器中,使用

@LoadBalanced注解来达到远程调用服务提供者的时候使用负载均衡机制

package com.example.consumeruser;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

//加上@EnableDiscoveryClient注解,消费者才能发现注册中心的服务
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerUserApplication {

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

//RestTemplate是消费者访问远程服务的工具,所以需要先注册到容器中,用的时候使用@Autowired取出来就可以用
//@LoadBalanced注解用于说明调用远程服务时使用负载均衡机制
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}

}

  c、编写UserController类,该类中使用RestTemplate来调用远程服务

package com.example.consumeruser.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class UserController {
@Autowired
RestTemplate restTemplate;
@GetMapping("buy")
public String buyTicket(String name){
//http://后不需要写ip地址和端口号,这样才能根据服务提供方的应用名字去负载均衡到不同的ip和端口
//provider-ticket(不区分大小写) 为服务提供方在注册中心注册的application名字
//ticket为服务提供方访问对应方法时使用的路径
String str = restTemplate.getForObject("http://provider-ticket/ticket",String.class);
return name + "购买了" + str;
}
}

启动消费者服务,并访问测试:

说明消费者能够调用到服务提供者的方法,且观察控制台,轮流打印8001端口和8002端口,说明负载均衡也生效了。

如有理解不到之处,望指正。

12-26 10:50