栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

每日总结SpringCloud(eureka,Ribbon负载均衡)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

每日总结SpringCloud(eureka,Ribbon负载均衡)

Eureka:

导入依赖:

 //服务端
 
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-server
    
//客户端
 
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-clent
    

添加application.yml文件

#服务端
server:
	port: 8888 #端口号
spring:
  application:
    name: name #服务名称

eureka:
	instance:
    	hostname: localhost 
    client:
    fetch-registry: false  #是否提取服务
    register-with-eureka: false #是否向注册中心注册
    service-url:
      defaultZone: http://localhost:7001/eureka #监视地址
#客户端
server:
	port: 8888 #端口号
spring:
  application:
    name: name #服务名称

eureka:
    client:
    fetch-registry: true #是否提取服务
    register-with-eureka: true #是否向注册中心注册
    service-url:
      defaultZone: http://localhost:7001/eureka #注册地址

最后在启动类上开启服务
@EnableEurekaServer 服务端
@EnableEurekaClent 客户端

Ribbon负载均衡

1.com.netflix.loadbalancer.RoundRobinRule 轮询,默认策略。
2.com.netflix.loadbalancer.RandomRule 随机

需要在启动类之外的包中写入配置类

@Configuration
public class RuleConfig {

    @Bean
    public IRule getRule()
    {
        return new RandomRule(); //策略
    }
}

再从启动类中开启负载均衡策略

@SpringBootApplication
@EnableEurekaClient
//name:需要负载均衡的服务名,configuration:自定义策略的字节码文件
@RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration = RuleConfig.class)
public class FeignOrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(FeignOrderMain80.class,args);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/969499.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号