1.改造服务提供者

在项目Eureka Provider和Eureka Provider-another中src/main/java/com.lz下新建controller包,并创建PortController类,该类能返回当前项目的端口号,代码如下:

package controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PortController {
    @Value("${server.port}")
    String port;
    @RequestMapping("/port")
    public String getPort() {
        return "Hello,I'm from port:"+port;
    }
}

分别启动服务器Eureka Server,Eureka Server-another和服务提供者Eureka Provider,Eureka Provider-another 打开浏览器访问http://localhost:7006/port和http://localhost:7007/port,出现如下界面

alt

alt

2.搭建含有Ribbon的服务消费者
(1)创建Eureka Ribbon-client的模块

如下所示:

alt

alt

alt

(2)添加Eureka ribbon-client的依赖

在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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.example</groupId>
    <artifactId>eureka-ribbon-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Eureka ribbon-client</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>
    <dependencies>
        <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-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>
(3)配置全局文件application.yml

在Eureka ribbon-client模块下,找到src/main/resources下创建文件application.yml,复制如下代码进行配置。。

spring:
  application:
    name: eureka-ribbon-client
server:
  port: 8764
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7000/eureka/
(4)启动EurekaRibbonClientApplication

在src/main/java下创建包com.lz,在该包中创建启动类EurekaRibbonClientApplication的Java文件。

package com.lz;

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

@EnableEurekaClient
@SpringBootApplication
public class EurekaRibbonClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaRibbonClientApplication.class,args);
    }
}

(5)创建配置类

在com.lz下创建config包,并在该包下创建RibbonConfig类。代码如下:

package com.lz.config;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RibbonConfig {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(RestTemplateBuilder builder){
        return builder.build();
    }
}

(6)创建Service类

在com.lz下创建service包,并在该包下创建RibbonService类。代码如下:

package com.lz.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class RibbonService {
    @Autowired
    RestTemplate restTemplate;
    public String hi(){
        return restTemplate.getForObject("http://eureka-provider/port",String.class);
    }
}

(7)创建Controller类

在com.lz下创建controller包,并在该包下创建RibbonController类。代码如下:

package com.lz.controller;

import com.lz.service.RibbonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RibbonController {
    @Autowired
    RibbonService ribbonService;
    @RequestMapping("/hi")
    public String hi(){
        return ribbonService.hi();
    }
}

3.测试运行

启动服务器Eureka Server和Euraka Server-another,服务提供者Eureka Provide和Eureka Provide-another,服务消费者Eureka Ribbon-client,在浏览器中访问 http://server1:7000/http://server2:7009/

alt

alt

从上图所示:两个服务提供者和一个服务消费者已经全部注册到Euraka上,实现了高可用。

alt

alt

上图看出,当访问 localhost:8764/hi 时,浏览器页面会轮流显示两个服务提供者的端口号,说明负载均衡起到了效果,负载均衡器会轮流请求两个服务提供者中的‘hi’接口。。