SpringCloud使用Eureka注册中心和Ribbon负载均衡

在之前的工程上创建Eureka模块

添加依赖

<?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">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>com.study</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</project>

填写yml配置文件

server:
  port: 7001
eureka:
  instance:
    hostname: localhost # 服务端的实例名称
  client:
    register-with-eureka: false #表示是否想eureka注册中心注册自己
    fetch-registry: false #设置false 表示自己是一个注册中心
    service-url:
      defaultZone: http://${eureka.instance/hostname}:${server.port}/eureka/

最后添加一个启动类

package com.study.eureka;

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

/**
 * @description:
 * 启动之后访问http://localhost:7001/ 可以进入控制中心
 * @author: Leo
 * @createDate: 2020/2/11
 * @version: 1.0
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaApp7001
{
    public static void main(String[] args)
    {
        SpringApplication.run(EurekaApp7001.class,args);
    }
}

主启动类加上@EnableEurekaServer,表示他是eureka服务端
启动之后访问http://localhost:7001/ 可以进入控制中心

继续添加两个模块

可以叫Eureka7002和Eureka7003,因为我们要模仿集群环境,所以需要增加几个。
这里有一个注意点,就是7001和7002和7003这三个服务器怎么才能让他们产生交集呢?
我们要对配置文件进行修改:

server:
  port: 7001
eureka:
  instance:
    hostname: localhost # 服务端的实例名称
  client:
    register-with-eureka: false #表示是否想eureka注册中心注册自己
    fetch-registry: false #设置false 表示自己是一个注册中心
    service-url:
      defaultZone: http://localhost:7002/eureka/,http://localhost:7003/eureka/

server:
  port: 7001
eureka:
  instance:
    hostname: localhost # 服务端的实例名称
  client:
    register-with-eureka: false #表示是否想eureka注册中心注册自己
    fetch-registry: false #设置false 表示自己是一个注册中心
    service-url:
      defaultZone: http://localhost:7001/eureka/,http://localhost:7003/eureka/

server:
  port: 7001
eureka:
  instance:
    hostname: localhost # 服务端的实例名称
  client:
    register-with-eureka: false #表示是否想eureka注册中心注册自己
    fetch-registry: false #设置false 表示自己是一个注册中心
    service-url:
      defaultZone: http://localhost:7002/eureka/,http://localhost:7001/eureka/

改造服务提供端

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
 

那么最后我们要改哪里?与我们之前打eureka服务端的次序一样,添加依赖加上注解加上配置文件,最后我们要改的地方就是配置文件了

服务消费者的改造

改造起来异曲同工,只是在消费者端需要加上一个负载均衡实现的依赖ribbon。

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
 

老样子,改主启动类:

好了,那么怎么简单实现负载均衡呢?

package com.study.cloud.config;

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;

/**
 * @description:
 * @author: Leo
 * @createDate: 2020/2/10
 * @version: 1.0
 */
@Configuration //这是springboot中的注解,他的作用相当于spring中的applicationContext.xml
public class ConfigBean
{
    //配置RestTemplate
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

}

只在原来配置类里的RestTemplate 方法上加上 @LoadBalanced就可以了
最后配置文件老样子

server:
  port: 80
eureka:
  client:
    register-with-eureka: false #不向eureka中注册自己
    service-url:
      defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/

因为现在提供端也注册到了服务中心,现在服务中心有三个,所以我们只要去请求服务中心的对应服务提供端的名字就可以了。

发布了515 篇原创文章 · 获赞 10 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/xiamaocheng/article/details/104291461