启动 eureka-server-1(服务提供者)的服务,以不同端口来启动
先修改application.xml文件,将注册中心改为8080提供的
eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka/
java -jar eureka-server-1-0.0.1-SNAPSHOT.jar --server.port=8081
java -jar eureka-server-1-0.0.1-SNAPSHOT.jar --server.port=8082
启动 eureka-server(服务注册中心)的服务,可以看到hello-service出现了两个单元实例。
创建eureka-ribbon-consumer来实现服务消费者
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion><groupId>com.example</groupId>
<artifactId>eureka-server-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging><name>eureka-server-1</name>
<description>Demo project for Spring Boot</description><parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent><properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!--添加ribbon依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies><build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动类EurekaRibbonConsumerApplication
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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;@SpringBootApplication
//让该应用注册为eureka客户端应用,来获得服务发现的能力
@EnableDiscoveryClient
public class EurekaRibbonConsumerApplication {@Bean
//开启客户端负载均衡
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}public static void main(String[] args) {
SpringApplication.run(EurekaRibbonConsumerApplication.class, args);
}
}
新建ConsumerController
package com.example.demo;
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.RestController;
import org.springframework.web.client.RestTemplate;@RestController
public class ConsumerController {
@Autowired
RestTemplate restTemplate;@RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET)
public String helloConsumer() {
//创建的RestTemplate实现对HELLO-SERVICE服务提供/helo接口进行调用
return restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody();
}
}
application.properties
server.port=9000
spring.application.name=ribbon-consumer
eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka/
启动 eureka-ribbon-consumer服务
运行http://localhost:9000/ribbon-consumer来调用提供者的/hello接口,实现对hello-service服务的负载均衡,可以查看控制台是否是交替打印以下信息