简单实践 java spring cloud 负载均衡

1 概要

1.1 实现一个最简单的微服务。远程调用+负载均衡,基本上完成了最核心的微服务框架。

远程调用:RestTemplate

注册中心:eureka

负载均衡:Ribbon

1.2 要点

1.2.1 依赖

1.2.1.1 主框架依赖
  • spring boot 依赖
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
        </dependency>
  •  spring cloud 依赖
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
 1.2.1.2  eureka依赖
  • 服务端依赖
<groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  • 客户端依赖 
<groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

 1.2.2 配置文件

  • 服务设置
server:
  port: 10086
spring:
  application:
    name: eureka server
  •  服务注册
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka/

1.2 技术关键词

  • spring-cloud-starter
    
  • spring-cloud-dependencies
  • spring-cloud-starter-netflix-eureka-server
  • spring-cloud-starter-netflix-eureka-client
  • spring-boot-starter-web
  • spring:
      application:
        name: eureka server
  • eureka:
      client:
        service-url:
          defaultZone: http://127.0.0.1:10086/eureka/
  • @SpringBootApplication
    @EnableEurekaServer
  • @Bean
    @LoadBalanced
    public RestTemplate
  • @Autowired
    RestTemplate restTemplate;
  • @RestController
  • return "函数2"+restTemplate.getForObject(url,String.class);
  • SpringApplication.run(Main.class);

 

2 代码

2.1 父工程

 <?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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.xjc.springcloundtest</groupId>
        <artifactId>demo8</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>untitled</artifactId>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
</project>

2.2 注册中 eureka

2.2.1 工程文件

 <?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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.xjc.springcloundtest</groupId>
        <artifactId>demo8</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>untitled</artifactId>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
</project>

2.2.2 配置文件

server:
  port: 10086
spring:
  application:
    name: eureka server
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka/

2.2.3 主函数

package com.xjc.springcloundtest;

import com.netflix.discovery.shared.Application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class);
        System.out.println("Hello world!");
    }
}

2.2.4 运行效果

2.3 服务工程

2.2.1 工程文件

<?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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.xjc.springcloundtest</groupId>
        <artifactId>demo8</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>untitled1</artifactId>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <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>
    </dependencies>

</project>

2.3.2 配置文件

spring:
  application:
    name: server1
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka/

2.3.3 主函数

package com.xjc.springcloundtest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class);
        System.out.println("Hello world!");
    }
}

2.3.4 控制器

package com.xjc.springcloundtest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @RequestMapping("/fun")
    public String fun(){
        return "函数1";
    }
}

2.3.5 运行效果

 

2.4 消费者

2.4.1 工程文件

<?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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.xjc.springcloundtest</groupId>
        <artifactId>demo8</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>untitled2</artifactId>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <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>
    </dependencies>

</project>

2.4.2 配置文件

server:
  port: 8081
spring:
  application:
    name: server2
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka/

2.4.3 主函数

package com.xjc.springcloundtest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class);
        System.out.println("Hello world!");
    }
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(RestTemplateBuilder builder ){
        return builder.build();
    }
}

2.4.4 消费者

package com.xjc.springcloundtest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class TestController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/fun")
    public String fun(){
        //String url = "http://localhost:8080/fun";
        String url = "http://server1/fun";
        return "函数2"+restTemplate.getForObject(url,String.class);
    }
}

2.4.5 运行效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值