Spring-Cloud-Eureka-Client

1.Eureka-server就是一个注册中心,其他的Eureka-Client都是一个个的微服务,彼此间相互调用。
2.服务的提供仅仅需要引用spring-cloud-starter-netflix-eureka-client
3.服务的消费方式
1):feign,一个通过本地接口的形式来进行调用服务的,其中Feign中默认引入了Ribbon,以接口的形式进行调用服务,看起来简洁,而且Feign中还可以增加熔断器,来进行服务的熔断和降级,防止服务调用中的服务的雪崩
需要引用spring-cloud-starter-netflix-eureka-client和spring-cloud-starter-openfeign,
spring-cloud-starter-netflix-eureka-client讲此模块注册到Eureka-server
spring-cloud-starter-openfeign,调用远程的服务
2):ribbon,一个基于Http端的负载均衡,通过在Configuration中配置RestTemplate来进行调用,可以自定义负载均衡的方式,需要在配置Bean:RestTemplate时加上@LoadBalanced

一个仅仅提供服务的模块(不调用其他的服务)

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>spring-clould-Official</artifactId>
        <groupId>com.wx</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Eureka-Client</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-starter-actuator</artifactId>
        </dependency>
    </dependencies>

</project>

功效备注:

spring-cloud-starter-netflix-eureka-client

声明这是一个eureka客户端,要注册到eureka的服务之一

spring-boot-starter-web

使用@RestController等等注解,搭建MVC系统

spring-boot-starter-actuator

服务健康检查

application.yml


eureka:
  client:
    serviceUrl:
      defaultZone: http://192.168.88.1:8761/eureka/
    healthcheck:
      enabled: true
#   By default, the EurekaClient bean is refreshable, meaning the Eureka client properties can be changed and refreshed. When a refresh occurs clients will be unregistered from the Eureka server and there might be a brief moment of time where all instance of a given service are not available. One way to eliminate this from happening is to disable the ability to refresh Eureka clients. To do this set eureka.client.refresh.enable=false.
    refresh:
      enable: false
  instance:
    instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
    leaseRenewalIntervalInSeconds: 30
server:
  port: 9001

spring:
  application:
    name: client01


EurekaCliApplication01.java(启动类)

package com.wx.client01;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
//EnableEurekaClient可省略
//@EnableEurekaClient
public class EurekaCliApplication01 {

    public static void main(String[] args) {
        new SpringApplicationBuilder(EurekaCliApplication01.class).web(true).run(args);
    }
}

提供的服务

package com.wx.client01.controller;

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

@RestController
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping("/sayhi")
    public String sayHi(@RequestParam(value = "name" ,defaultValue = "Lin") String name){
        return "Hi,"+name;
    }
}

一个既可以提供服务,又可以调用其他服务的模块

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>spring-clould-Official</artifactId>
        <groupId>com.wx</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Eureka-Client-02</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-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

</project>

application.yml

server:
  port: 9002

eureka:
  client:
    serviceUrl:
      defaultZone: http://192.168.88.1:8761/eureka/
    healthcheck:
      enabled: true
#   By default, the EurekaClient bean is refreshable, meaning the Eureka client properties can be changed and refreshed. When a refresh occurs clients will be unregistered from the Eureka server and there might be a brief moment of time where all instance of a given service are not available. One way to eliminate this from happening is to disable the ability to refresh Eureka clients. To do this set eureka.client.refresh.enable=false.
    refresh:
      enable: false
  instance:
    instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
    leaseRenewalIntervalInSeconds: 30

spring:
  application:
    name: client01


启动类

package com.wx.client01;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
//EnableEurekaClient可省略
//@EnableEurekaClient
@EnableFeignClients
public class EurekaCliApplication02 {

    public static void main(String[] args) {
        new SpringApplicationBuilder(EurekaCliApplication02.class).web(true).run(args);
    }
}

MVC view

package com.wx.client01.controller;

import com.wx.client01.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {
    @Autowired
    private HelloService helloService;

    @RequestMapping("/sayhi")
    public String sayHi(@RequestParam(value = "name" ,defaultValue = "opopopop") String name){

        return helloService.sayHi(name);
    }
}

feign 调用远程服务

package com.wx.client01.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Service
@FeignClient(value = "client01",name = "client01")
public interface HelloService {

    @RequestMapping(value = "/hello/sayhi")
    public String sayHi(@RequestParam("name") String name);

}

Ribbon调用

1.@LoadBalanced配置RestTemplate(没有@LoadBalanced会报错找不到主机)
package com.wx.client02.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 RestTemplateConfig {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

}

2.调用
package com.wx.client02.controller;

import com.wx.client02.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/sayRest/{name}")
    public String sayRest(@PathVariable("name") String name){
        String serviceRest = restTemplate.getForObject("http://client01/hello/sayhi?name=" + name, String.class);
        return serviceRest;
    }
}

3.Ribbon 自带的负载均衡策略
  • RoundRobinRule:轮询。
  • RandomRule:随机。
  • AvailabilityFilteringRule:先过滤掉由于多次访问故障而处于断路器跳闸状态的服务,以及并发连接数超过阈值的服务,剩下的服务,使用轮询策略。
  • WeightedResponseTimeRule:根据平均响应时间计算所有服务的权重,响应越快的服务权重越高,越容易被选中。一开始启动时,统计信息不足的情况下,使用轮询。
  • RetryRule:先轮询,如果获取失败则在指定时间内重试,重新轮询可用的服务。
  • BestAvailableRule:先过滤掉由于多次访问故障而处于断路器跳闸状态的服务,然后选择一个并发量最小的服务。
  • ZoneAvoidanceRule:复合判断 server 所在区域的性能和 server 的可用性选择服务器
使用方法 new 相应的策略即可
package com.wx.client02.config;

import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.RoundRobinRule;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration;
import org.springframework.cloud.client.loadbalancer.RestTemplateCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Configuration
public class RestTemplateConfig {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    @Bean
    public RoundRobinRule randomRule(){
        return new RoundRobinRule();
    }


}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值