欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Cloud zuul 负载均衡

程序员文章站 2022-07-15 10:53:26
...

不多BB 直接上代码 首先创建三个项目

Eureka的pom文件

<!-- 服务端发现模块 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <!-- 服务端登录验证模块:进入 eureka 的网页时候需要输入登录密码的模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

然后是配置

spring:
  application:
    name: eureka
server:
  port: 8088
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:8088/eureka
    instance:
      hostname: ${spring.cloud.client.ipAddress}
security:
  basic:
    enabled: false
  user:
    name: admin
    password: admin

启动类

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}

然后是服务
pom文件

<!-- 客户端发现模块 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

配置文件

server:
	// 不要用随机端口
  port:  8085
spring:
  application:
    name: xajr-service
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8088/eureka
  instance:
    hostname: ${spring.cloud.client.ipAddress}
    prefer-ip-address: false
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}

启动类

@SpringBootApplication
@EnableEurekaClient
public class ServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceApplication.class, args);
    }
}

service的 测试代码

@RestController
public class HelloController {

	// 获取端口
    @Value("${server.port}")
    private String port;

    @RequestMapping(value = "/hello/hi", method = RequestMethod.GET)
    public String hello() {
        return "Hello " + port;
    }
}

zuul

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>
        <!-- 客户端发现模块 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>

配置文件

spring:
  application:
    name: zuul
server:
  port: 8087
eureka:
  instance:
    hostname: ${spring.cloud.client.ipAddress}
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
  client:
    service-url:
      defaultZone: http://localhost:8088/eureka
zuul:
  routes:
    hello:
    	// 这是在访问路径上要加的
      path: /test/**
      serviceId: xajr-service
#下面配置的超时时间   解决com.netflix.zuul.exception.ZuulException: Forwarding error报错问题
  host:
    connect-timeout-millis: 10000
    socket-timeout-millis: 60000
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 60000
ribbon:
  ReadTimeout: 60000
  ConnectTimeout: 60000

配置已经完成。
然后依次启动 eureka 、service(两个)、zuul
Spring Cloud zuul 负载均衡
下面进行测试
Spring Cloud zuul 负载均衡
Spring Cloud zuul 负载均衡
然后zuul
Spring Cloud zuul 负载均衡
多访问几次,就可以看到效果。

zuul 负载均衡就完成了、