美文网首页
SpringCloud - 服务消费者 + 负载均衡

SpringCloud - 服务消费者 + 负载均衡

作者: 尼尔君 | 来源:发表于2018-08-28 15:52 被阅读0次

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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.boolib</groupId>
    <artifactId>server_order</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>server_order</name>
    <description>服务消费者</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

启动类

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class ServerOrderApplication {

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

}

配置参数

#lease-renewal-interval-in-seconds
# 每间隔1s,向服务端发送一次心跳,证明自己依然”存活“
eureka.instance.lease-renewal-interval-in-seconds=1

#lease-expiration-duration-in-seconds
# 告诉服务端,如果我2s之内没有给你发心跳,就代表我“死”了,将我踢出掉。
eureka.instance.lease-expiration-duration-in-seconds=5


server.port=8867
spring.application.name=service-order
eureka.client.service-url.defaultZone=http://www.ubiubiu.com:8888/eureka/

feign.hystrix.enabled=false





远程调用

@Service
@FeignClient("service-provide")
public interface OrderService {


    @GetMapping("/list")
    List<String> getList();


}

Controller调用



@Slf4j
@RestController
public class WebController {

    @Autowired
    private  OrderService orderService;

    @GetMapping("/list")
    public List<String> getList(){

        return orderService.getList();
    }
}


使用的是feign,默认负载均衡

普通rpc负载均衡


    @Bean
    @LoadBalanced
    public RequestTemplate requestTemplate(){
        return  new RequestTemplate();
    }

相关文章

网友评论

      本文标题:SpringCloud - 服务消费者 + 负载均衡

      本文链接:https://www.haomeiwen.com/subject/hkxqwftx.html