提交 c6736811 编写于 作者: Q qinyingjie

fix:自定义负载均衡

上级 437f9531
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
<module>springcloud-service-config</module> <module>springcloud-service-config</module>
<module>springcloud-service-config-client</module> <module>springcloud-service-config-client</module>
<module>springcloud-service-feign</module> <module>springcloud-service-feign</module>
<module>springcloud-service-zuul</module>
<module>springcloud-service-eureka</module> <module>springcloud-service-eureka</module>
</modules> </modules>
......
...@@ -45,6 +45,10 @@ ...@@ -45,6 +45,10 @@
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId> <artifactId>spring-cloud-commons</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-loadbalancer</artifactId>
</dependency>
</dependencies> </dependencies>
......
package com.kwan.springcloud.config; package com.kwan.springcloud.config;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -13,4 +15,12 @@ public class RestConfig { ...@@ -13,4 +15,12 @@ public class RestConfig {
public RestTemplate restTemplate() { public RestTemplate restTemplate() {
return new RestTemplate(); return new RestTemplate();
} }
/**
* 自定义负载均衡
*/
@Bean
public IRule microIrule() {
return new RandomRule();
}
} }
\ No newline at end of file
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> </dependency>
</dependencies> </dependencies>
......
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.kwan.springcloud</groupId>
<artifactId>springcloud-service-parent</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>springcloud-service-zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-service-zuul</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.kwan.springcloud</groupId>
<artifactId>springcloud-service-common</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package com.kwan.springcloud;
@EnableZuulProxy
@SpringBootApplication
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
#端口号
server:
port: 8888
spring:
application:
name: zuul-service #服务名称
#服务提供者
eureka:
client:
service-url:
defaultZone: http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/
\ No newline at end of file
package com.kwan.springcloud;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class PortalApplicationTests {
@Test
void contextLoads() {
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册