一、服务注册中心部署和服务提供方部署参考“Spring Cloud Eureka 部署高可用注册中心”https://blog.csdn.net/lixiaxin200319/article/details/80867425
在 Spring Cloud Eureka 的架构中,有三个角色分别是服务注册中心、服务提供方、服务消费者。
1. 把服务提供方工程 server1 打包成 JAR 包。右击服务提供方工程(弹出右键菜单)-->选择 Run as -->Maven install 。
使用 JAVA 命令启动 JAR 包,该 JAR 没配置监听的端口,所以监听的是默认端口 8080。
启动 JAR 包的命令 : java -jar server1-0.0.1-SNAPSHOT.jar
2. 在 server1 工程的 application.properties 文件中配置监听 8090端口。
1.创建名为 ribbon-consumer 的 maven 工程。
<?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>ribbon-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ribbon-consumer</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version> <!-- sprong boot 版本不能高于 1.5.13 否则会与spring-cloud-starter-eureka 1.4.4 版本不兼容 -->
<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>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<!-- <version>${log4j.version}</version> -->
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
-->
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
src/main/resources/application.properties:
spring.application.name=ribbon-consumer
eureka.client.serviceUrl.defaultZone=http://localhost:1112/eureka/,http://localhost:1111/eureka/
eureka.client.registry-fetch-interval-seconds=5
server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:1112/eureka/,http://localhost:1111/eureka/。配置服务消费者向localhost:1112 的注册节点注册,当localhost:1112 节点故障后,再向 localhost:1111 的节点注册。
ConsumerApplication:
package com.example.server1;
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;
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
}
创建应用主类 ConsumerApplication,通过 @EnableDiscoveryClient 注解让该应用注册为 Eureka 客户端应用,以获得服务发现的能力。同时,使用 @Bean 在 restTemplate 方法为该主类创建 RestTemplate 的 Spring Bean 实例,并通过 @LoadBalanced 注解开启客户端负载均衡。
ConsumerController:
package com.example.server1;
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 helloConsumber() {
return restTemplate.getForEntity("http://hello-service/hello", String.class).getBody();
}
}
创建 ConsumerController 类并实现 /ribbon-consumer 接口。在该接口中,通过在上面创建的 RestTemplate Spring Bean 来实现对 hello-service 服务提供的 /hello 接口进行调用。可以看到这里访问的地址是服务名 hello-service ,而不是一个具体的地址。当多次访问 /ribbon-consumer 接口时,会轮询访问服务名为 hello-service 服务的不同节点。
在 Eclipse 中启动 HellApplication,通过 http://localhost:9000/hello 访问服务消费者,会在浏览器中打印出 HellWord。