SpringCloud中配置高可用注册中心集群和ribbon-负载均衡

本篇文章为大家展示了Spring Cloud中配置高可用注册中心集群和ribbon-负载均衡,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

成都创新互联公司专业提供成都主机托管四川主机托管成都服务器托管四川服务器托管,支持按月付款!我们的承诺:贵族品质、平民价格,机房位于中国电信/网通/移动机房,多线服务器托管服务有保障!

创建父项目

  • 步骤一:父项目 could_parent

  • 步骤二:修改pom.xml文件,配置 spring boot 版本,spring cloud版本,锁定cloud依赖,确定cloud私有仓库


    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
    

    
    
        UTF-8
        1.8
        Greenwich.RELEASE
    

    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud-release.version}
                pom
                import
            
        
    

    
    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
            
                false
            
        
    

创建注册中心

  • 步骤一:创建项目 eureka_demo

  • 步骤二:修改pom.xml,配置 web 和 eureka server 依赖


        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
    
  • 步骤三:创建yml文件,配置端口号、服务名、注册地址

#端口号
server:
  port: 10086
#服务名
spring:
  application:
          #服务名不可以使用下划线 列如:eureka_demo  在后面的调用中下划线会变成空格从而导致找不到该服务
    name: eurekaDemo3
#注册地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka
    register-with-eureka: false   #是否注册自己到注册中心
    fetch-registry: false         #是否从注册中心拉取服务列表
  • 步骤四:创建启动类,添加开启eureka server注解 @EnableEurekaServer

package com.czxy;

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


@SpringBootApplication
@EnableEurekaServer     //开启eureka service
public class EurekaDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaDemoApplication.class,args);
    }
}

配置高可用的注册中心:集群

  • 步骤一:修改注册中心(eureka_demo) 核心yml文件,application.yml,只配置服务名(共享内容)

#服务名
spring:
  application:
    name: eurekaDemo
  • 步骤二:创建 application-10086.yml,配置10086端口和注册路径(10087)

#端口号
server:
  port: 10086
#注册地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10087/eureka
    register-with-eureka: true   #是否注册自己到注册中心,默认值true(可省略)
    fetch-registry: true         #是否从注册中心拉取服务列表,默认值true(可省略)
  • 步骤三:创建 application-10087.yml,配置10087端口和注册路径(10086)

#端口号
server:
  port: 10087
#注册地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka
    register-with-eureka: true   #是否注册自己到注册中心
    fetch-registry: true         #是否从注册中心拉取服务列表
  • 步骤四:修改核心yml文件,激活10086配置,并启动程序,  此时控制台抛异常,10087还没有启动,等10087启动后异常自动消失。

#服务名
spring:
  application:
    name: eurekaDemo
  profiles:
    active: 10086
  • 步骤五:修改核心yml文件,激活10087配置,并启动程序(idea不支持多启动 需要手动设置,不同版本不同设置)

Spring Cloud中配置高可用注册中心集群和ribbon-负载均衡

#服务名
spring:
  application:
    name: eurekaDemo
  profiles:
    active: 10087
  • 所有注册中心yml

Spring Cloud中配置高可用注册中心集群和ribbon-负载均衡

IDEA中配置多启动

步骤一:创建 spring boot启动项

Spring Cloud中配置高可用注册中心集群和ribbon-负载均衡

步骤二:配置10087启动 同配置10086一样(此处省略)

步骤三:启动

创建服务提供方 eureka_service4 (集群)  

  • pom文件:

 
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
    

同一个名称的服务,只要提供多个实例,注册到eureka中,就可以自动形成集群。

  • 步骤一:创建 application-8081.yml文件,并配置端口号8081

Spring Cloud中配置高可用注册中心集群和ribbon-负载均衡

server:
  port: 8081
  • 步骤二:创建 application-8082.yml文件,并配置端口号8082

server:
  port: 8081
  • 步骤三:为两个yml文件,分别配置启动项 (同上10086,10087配置一样)

Spring Cloud中配置高可用注册中心集群和ribbon-负载均衡

  • 步骤四:启动,测试

Spring Cloud中配置高可用注册中心集群和ribbon-负载均衡

在服务提供方 eureka_service3 中添加controller方便测试数据

package com.czxy.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/test")
public class TestController {
    @GetMapping                          
    public ResponseEntity test(HttpServletRequest request){
                                               //端口号
        return ResponseEntity.ok("测试数据" + request.getServerPort());
    }
}

创建服务调用方

  • 步骤一:创建项目 eureka_client3

  • 步骤二:核心3步,pom文件,yml文件,启动类

  pom文件:


        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
    

yml文件:

server:
  port: 9090
spring:
  application:
    name: eurekaClient
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka

启动类 :

package com.czxy;

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


@SpringBootApplication
@EnableEurekaClient
public class ClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class,args);
    }
}
  • 步骤三:编写配置类,配置RestTemplate实例 ,让RestTemplate支持“服务名”访问机制   需要添加注解 @LoadBalanced

package com.czxy.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;


@Configuration
public class HttpConfig {
    @Bean
    @LoadBalanced       //让RestTemplate支持负载均衡,也就是说支持“服务名”访问
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}
  • 步骤四:修改dao,使用RestTemplate进行远程调用时,使用“服务名”进行调用即可。

package com.czxy.dao;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;


@Component
public class DataDao {
    @Resource
    private RestTemplate restTemplate;

    public ResponseEntity data(){
//        String url = "http://localhost:8081/test";
        String url = "http://service3/test";
        return restTemplate.getForEntity(url,String.class);
    }
}
  • 测试结果:

Spring Cloud中配置高可用注册中心集群和ribbon-负载均衡

上述内容就是Spring Cloud中配置高可用注册中心集群和ribbon-负载均衡,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


名称栏目:SpringCloud中配置高可用注册中心集群和ribbon-负载均衡
网页地址:http://myzitong.com/article/jpgpjh.html