Spring Cloud 实操笔记 - Eureka、服务提供与调用

一、POM配置

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

二、application.properties

spring.application.name=spring-cloud-eureka

server.port=8000
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

eureka.client.serviceUrl.defaultZone=http://localhost:${
    
    server.port}/eureka/

三、EurekaApplication

package com.xxx.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
    
    

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

}

四、高可用

Eureka的高可用是通过互相注册的方式实现的。

spring.application.name=spring-cloud-eureka

server.port=8000
eureka.instance.hostname=peer1
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.serviceUrl.defaultZone=http://peer2:8001/eureka/

eureka.server.eviction-interval-timer-in-ms=5000
spring.application.name=spring-cloud-eureka

server.port=8001
eureka.instance.hostname=peer2
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.serviceUrl.defaultZone=http://peer1:8000/eureka/

eureka.server.eviction-interval-timer-in-ms=5000

五、服务提供与调用

  1. 服务提供者
<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>
            <version>2.2.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
spring.application.name=spring-cloud-producer
server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
package com.geebox.producer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {
    
    

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

}

package com.geebox.producer.controller;

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

@RestController
public class HelloController {
    
    

    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
    
    
        return "hello "+name+",this is first messge";
    }
}
  1. 服务调用者
<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>
            <version>2.2.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
spring.application.name=spring-cloud-consumer
server.port=9001
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
package com.geebox.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient//启用服务注册与发现
@EnableFeignClients//启用feign进行远程调用
public class ConsumerApplication {
    
    

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

}
package com.geebox.consumer.api;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name = "spring-cloud-producer")
public interface HelloRemote {
    
    
    @RequestMapping(value = "/hello")
    public String hello(@RequestParam(value = "name") String name);
}
package com.geebox.consumer.controller;

import com.geebox.consumer.api.HelloRemote;
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.RestController;

@RestController
public class ConsumerController {
    
    
    @Autowired
    HelloRemote helloRemote;

    @RequestMapping("/hello/{name}")
    public String index(@PathVariable("name") String name){
    
    
        return helloRemote.hello(name);
    }
}

  1. 当启动多个producer时,则consumer调用的时候用feign会自动负载均衡,轮询调用
    在这里插入图片描述

六、熔断器

Feign已经集成了Hystrix:

  1. 配置文件
feign.hystrix.enabled=true
  1. 创建回调类
package com.geebox.consumer.api;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

@Component
public class HelloRemoteHystrix implements HelloRemote {
    
    
    @Override
    public String hello(@RequestParam(value = "name") String name) {
    
    
        return "hello" +name+", this messge send failed ";
    }
}

  1. interface添加回调配置fallback
package com.geebox.consumer.api;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name = "spring-cloud-producer", fallback = HelloRemoteHystrix.class)
public interface HelloRemote {
    
    
    @RequestMapping(value = "/hello")
    public String hello(@RequestParam(value = "name") String name);
}

  1. 测试
    在这里插入图片描述

七、熔断监控Hystrix Dashboard - 单个应用

  1. pom
<!-- Hystrix Dashboard -->
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
         <version>2.2.2.RELEASE</version>
     </dependency>
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
         <version>2.2.2.RELEASE</version>
     </dependency>
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-actuator</artifactId>
     </dependency>
<!-- Hystrix Dashboard -->
  1. 启动类加上注解
package com.geebox.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient//启用服务注册与发现
@EnableFeignClients//启用feign进行远程调用
@EnableHystrixDashboard//熔断监控Hystrix Dashboard
@EnableCircuitBreaker//熔断监控Hystrix Dashboard
public class ConsumerApplication {
    
    

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

}
  1. 因为用的是spring boot2.0+,所以需要配置如下Bean
@Bean
    public ServletRegistrationBean getServlet() {
    
    
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
  1. 测试
    http://localhost:9001/hystrix
    在这里插入图片描述

八、Turbine - 多个应用监控

  1. pom
<!-- Turbine -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-turbine</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>
<!-- Turbine -->
  1. 配置,记得一定要加instanceUrlSuffix的配置,因为Springboot2.x里面路径不一样了
spring.application.name=hystrix-dashboard-turbine
server.port=7001
turbine.appConfig=node01,node02
turbine.aggregator.clusterConfig=default
turbine.clusterNameExpression=new String("default")
turbine.instanceUrlSuffix:/hystrix.stream
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
  1. Application
package com.geebox.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;

@SpringBootApplication
@EnableHystrixDashboard//熔断监控Hystrix Dashboard
@EnableTurbine//启用Turbine
public class DashboardApplication {
    
    

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

}
  1. 启动,测试
    http://localhost:7001/hystrix/monitor?stream=http%3A%2F%2Flocalhost%3A7001%2Fturbine.stream
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/hudmhacker/article/details/107630905