400 028 6601

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

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

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

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

创建父项目


    
        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
            
        
    

创建注册中心


        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
    
#端口号
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         #是否从注册中心拉取服务列表
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);
    }
}

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

#服务名
spring:
  application:
    name: eurekaDemo
#端口号
server:
  port: 10086
#注册地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10087/eureka
    register-with-eureka: true   #是否注册自己到注册中心,默认值true(可省略)
    fetch-registry: true         #是否从注册中心拉取服务列表,默认值true(可省略)
#端口号
server:
  port: 10087
#注册地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka
    register-with-eureka: true   #是否注册自己到注册中心
    fetch-registry: true         #是否从注册中心拉取服务列表
#服务名
spring:
  application:
    name: eurekaDemo
  profiles:
    active: 10086

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

#服务名
spring:
  application:
    name: eurekaDemo
  profiles:
    active: 10087

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

IDEA中配置多启动

步骤一:创建 spring boot启动项

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

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

步骤三:启动

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

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

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

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

server:
  port: 8081

server:
  port: 8081

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());
    }
}

创建服务调用方

  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);
    }
}
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();
    }
}
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://www.bluegullmedia.com/article/jpgpjh.html

其他资讯

让你的专属顾问为你服务

0.0618s