上文地址
spring cloud Eureka 高可用注册中心Demo
上文我们完成了高可用注册中心,现在我们用Ribbon完成服务的发现与消费
在上一文中我们只有一个服务helloServer ,但我们要体现Ribbon 客户端的负载均衡,还要新建一个helloServer,我们就把上一文的helloServer复制一份在在8001端口中启动即可。
然后我们在新建一个工程实现服务的消费,我们给这个工程起名为hello-service
下面是代码
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>demo02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo02</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.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>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</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-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka</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>
</dependencies>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Application.java
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 Demo02Application {
public static void main(String[] args) {
SpringApplication.run(Demo02Application.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
其中@EnableDiscoveryClient注解 是为了注册Eureka成客户端,以获取服务发现的能力
@LoadBalanced注解 是为了开启客户端的负载均衡
然后新建一个Controller
package com.example.demo02.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
import java.util.List;
@RestController
public class helloController {
private final Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
RestTemplate restTemplate;
@RequestMapping(value = "/sayHello",method = RequestMethod.GET)
public String sayHello(){
logger.info("=================");
return restTemplate.getForEntity("http://HELLO-SERVER/hello",String.class).getBody();
}
}
这个Controller主要是完成sayHello接口的
其中RestTemplate主要是对我们的hello-server服务的hello方法进行调用的
application.yml
eureka:
client:
serviceUrl:
defaultZone: http://peer2:1112/eureka/
spring:
application:
name: hello-service
server:
port: 9000
好了,到这里我们分别启动两个注册中心(peer1、peer2),两个服务(端口为8000 的hello-server和端口为8001的hello-server),消费者(hello-service)然后登陆注册中心,可以看到这样的画面
现在我们访问http://localhost:9000/sayHello
快速刷新可以发现内容的端口在8000和8001之间切换。