SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)

时间:2025-03-14 15:03:55

先建立父工程

SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)

..

SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)

..一路next

搭建注册中心(需要建立三个工程,端口不一样)

SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)

..

SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)

..

SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)

..

修改入口类

package com.cloud.eurekaserver1111;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication
@EnableEurekaServer
public class EurekaServer1111Application { public static void main(String[] args) {
SpringApplication.run(EurekaServer1111Application.class, args);
}
}

修改属性文件(一共建立三个Eureka-Server服务,端口分别为1111,2222,3333)

server.port=1111

eureka.instance.hostname=server.one.com
eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://server.two.com:2222/eureka,http://server.three.com:3333/eureka

..

server.port=2222

eureka.instance.hostname=server.two.com
eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.three.com:3333/eureka

..

server.port=3333

eureka.instance.hostname=server.three.com
eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.two.com:2222/eureka

..

SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)

修改hosts文件(C:\Windows\System32\drivers\etc)

127.0.0.1 server.one.com
127.0.0.1 server.two.com
127.0.0.1 server.three.com

修改pom文件,把parent改成父工程

    <parent>
<groupId>com.cloud</groupId>
<artifactId>cloud-parent-two</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

 建立服务提供者-8081

SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)

..

SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)

..

SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)

..

修改pom

    <parent>
<groupId>com.cloud</groupId>
<artifactId>cloud-parent-two</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

修改入口类:

package com.cloud.bookservice8081;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class BookService8081Application { public static void main(String[] args) {
SpringApplication.run(BookService8081Application.class, args);
}
}

修改属性文件

server.port=8081
# 服务名
spring.application.name=BookService # 注册地址
eureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.two.com:2222/eureka,http://server.three.com:3333/eureka
# 注册名
eureka.instance.instance-id=book-service:8081
eureka.instance.prefer-ip-address=true

新建controller

package com.cloud.bookservice8081.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.List; @Slf4j
@RestController
public class BookController { @Autowired
private DiscoveryClient discoveryClient; @RequestMapping("/book")
public String index(){
List<String> services = discoveryClient.getServices();
services.forEach(e -> log.info("book-service:8081:" + e));
List<ServiceInstance> list = discoveryClient.getInstances("BOOKSERVICEPROVIDER");
list.forEach(e -> {
log.info("book-service:8081:" + e.getServiceId() + "," + e.getHost() + "," + e.getPort() + "," + e.getUri());
});
return "{\n" +
" \"bookName\": \"Apache Kafka实战\",\n" +
" \"bookSize\": \"16开\",\n" +
" \"pack\": \"平装\",\n" +
" \"isbn\": \"9787121337765\",\n" +
" \"publisher\": \"电子工业出版社\",\n" +
" \"publishTime\": \"2018-05-01\",\n" +
" \"service\": \"book-service:8081\"\n" +
"}";
}
}

建立服务提供者-8082

SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)

..

SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)

..

修改pom

    <parent>
<groupId>com.cloud</groupId>
<artifactId>cloud-parent-two</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

修改入口类

package com.cloud.bookservice8082;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class BookService8082Application { public static void main(String[] args) {
SpringApplication.run(BookService8082Application.class, args);
}
}

修改属性文件

server.port=8082
# 服务名
spring.application.name=BookService # 注册地址
eureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.two.com:2222/eureka,http://server.three.com:3333/eureka
# 注册名
eureka.instance.instance-id=book-service:8082
eureka.instance.prefer-ip-address=true

增加controller

package com.cloud.bookservice8082.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.List; @Slf4j
@RestController
public class BookController { @Autowired
private DiscoveryClient discoveryClient; @RequestMapping("/book")
public String index(){
List<String> services = discoveryClient.getServices();
services.forEach(e -> log.info("book-service:8082:" + e));
List<ServiceInstance> list = discoveryClient.getInstances("BOOKSERVICEPROVIDER");
list.forEach(e -> {
log.info("book-service:8082:" + e.getServiceId() + "," + e.getHost() + "," + e.getPort() + "," + e.getUri());
});
return "{\n" +
" \"bookName\": \"Apache Kafka实战\",\n" +
" \"bookSize\": \"16开\",\n" +
" \"pack\": \"平装\",\n" +
" \"isbn\": \"9787121337765\",\n" +
" \"publisher\": \"电子工业出版社\",\n" +
" \"publishTime\": \"2018-05-01\",\n" +
" \"service\": \"book-service:8082\"\n" +
"}";
}
}

最后建立消费者-8080

SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)

..

SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)

..

修改pom

    <parent>
<groupId>com.cloud</groupId>
<artifactId>cloud-parent-two</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

修改入口类(配置了@LoadBalanced注解的Bean

package com.cloud.bookconsumer8080;

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.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableDiscoveryClient
@EnableEurekaClient
public class BookConsumer8080Application { @Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(BookConsumer8080Application.class, args);
}
}

属性文件

server.port=8080

eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.two.com:2222/eureka,http://server.three.com:3333/eureka

controller

package com.cloud.bookconsumer8080.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; @RestController
public class BookController { private static final String PREFIX = "http://BOOKSERVICE"; // 微服务名字 @Autowired
private RestTemplate restTemplate; @RequestMapping("consumeBook")
public String index(){
return restTemplate.getForEntity(PREFIX + "/book",String.class).getBody();
}
}

与上一节不同,这次指向的是服务名

目录结构

SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)

 下面开始运行

SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)

访问 http://server.one.com:1111/ 出现

SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)

可以看见当前Eureka连接另外两个Eureka,证明注册中心高可用集群搭建成功。

再看下面的服务,有两个,证明服务已经注册进来了

下面访问消费者 http://localhost:8080/consumeBook

SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)

刷新

SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)

证明负载均衡也成功了

GitHub