服务的负载均衡

pom文件

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud</artifactId>
        <groupId>com.home</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>pay8001</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>
                mybatis-spring-boot-starter
            </artifactId>
        </dependency>
        <dependency>
            <artifactId>common</artifactId>
            <groupId>com.home</groupId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--<dependency>-->

            <!--<groupId>com.alibaba</groupId>-->
            <!--<artifactId>druid-spring-boot-starter</artifactId>-->
            <!--<version>1.1.9</version>-->
        <!--</dependency>-->
        <!--<dependency>-->
            <!--<groupId>mysql</groupId>-->
            <!--<artifactId>mysql-connector-java</artifactId>-->
        <!--</dependency>-->
        <!--<dependency>-->
            <!--<groupId>org.springframework.boot</groupId>-->
            <!--<artifactId>spring-boot-starter-jdbc</artifactId>-->
        <!--</dependency>-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
    </dependencies>


</project>

两个服务的pom是一样的

yml 配置

server:
  port: 8001
spring:
  application:
    name: pay-service

#    datasource.type=com.alibaba.druid.pool.DruidDataSource
#    spring.datasource.url = jdbc:mysql://127.0.0.1:3306/demo
#    spring.datasource.username = root
#    spring.datasource.password = 123456
#    spring.datasource.driverClassName = com.mysql.jdbc.Driver
#  datasource:
#    type:
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
#      defaultZone: http://localhost:7001/eureka
      defaultZone: http://eureka2.com:7002/eureka,http://eureka.com:7001/eureka

两个服务之间就是改动了 端口号

主启动类

package home;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
@EnableEurekaClient
public class Order80 {
    public static void main(String[] args) {
        SpringApplication.run(Order80.class, args);
    }
}

主要是开启eureka的客户端,以上我们将服务就是集群了。

下面是调用方。
controller

package home.controller;

import home.entity.MyResult;
import home.entity.Pay;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@Slf4j
public class OrderCon {
    @Autowired
    RestTemplate restTemplate;

    @GetMapping(value = "/order/create")
    public MyResult create(Pay pay) {//pay/create
        log.info(pay + "");

//    "http://localhost:8001/pay/create"  之前我们是写死,现在是多台的集群了。不能写死
        return restTemplate.postForObject("http://PAY-SERVICE/pay/create", pay, MyResult.class);


    }

    @GetMapping(value = "/order/get/{id}")
    public MyResult get(@PathVariable(value = "id") int id) {
        return restTemplate.getForObject("http://PAY-SERVICE/pay/get/" + id, MyResult.class);

    }
}

主要是将原来的主机地址和端口号换成了服务名

需要把resttemplate也加上注解

package home.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 RestTem {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

主要就是以上几个问题。 这样其实就默认使用了轮询的负载均衡了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值