美文网首页Java框架
springcloud-consumer=>集成eureka,

springcloud-consumer=>集成eureka,

作者: 刘小刀tina | 来源:发表于2020-02-04 09:40 被阅读0次

    1.pom.xml

        <dependencies>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </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>
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
    
            <!--添加uereka client 依赖包-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
    
            <!--Actuator 开启健康检查依赖的包-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
            <!--添加ribbon依赖,spring Retry重试依赖-->
            <dependency>
                <groupId>org.springframework.retry</groupId>
                <artifactId>spring-retry</artifactId>
            </dependency>
    
    
            <!--添加Feigin依赖-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
    
            <!--添加Hystrix 依赖包-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            </dependency>
    
            <!--Hystris监控依赖所需要的actuator包-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
            <!--整合Dashboard-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
                <version>2.0.2.RELEASE</version>
            </dependency>
    
    
        </dependencies>
    
        <!--添加SpringCloud的依赖-->
        <dependencyManagement>
    
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Finchley.SR2</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
    
        </dependencyManagement>
    
    

    2.启动类

    
    @SpringCloudApplication //相当于@SpringBootApplication+@EnableDiscoveryClient+@EnableCircuitBreaker
    //@SpringBootApplication// 表明是启动类
    //@EnableDiscoveryClient //表明是Eurekak客户端
    
    //开发Feigin远程调用的功能,属性basePackages 指定路径
    @EnableFeignClients(basePackages = "com.tina.springcloud.springcloud.consumer7001.*")
    
    //@EnableCircuitBreaker //开启熔断组件的功能 相当于@EnableHystrixDashboard+@EnableHystrix
    //@EnableHystrixDashboard
    //@EnableHystrix
    public class SpringcloudConsumer7001Application {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringcloudConsumer7001Application.class, args);
        }
    
    }
    
    1. 配置文件
    server:
      port: 7001
    
    spring:
      application:
        name: eureka-consumer
    eureka:
      client:
        service-url:
          defaultZone: http://tina:123456@localhost:8001/eureka/
        healthcheck:
          #开启健康检查
          enabled: true
      instance:
        #采用IP注册
        prefer-ip-address: true
        #定义实例ID的格式
        instance.id: ${spring.application.name}:${sping.cloud.client.ip.address}:${server.port}
        #自定义实例跳转链接
        status-page-url: http://tina:123456@localhost:7001/consumer/getById/{id}
        #表示eureka client 发送心跳给server端的频率
        lease-renewal-interval-in-seconds: 5
        #表示eureka client 发送心跳给server端频率超过如下设置时间,service端则移除该实例
        lease-expiration-duration-in-seconds: 5
    
    #配置ribbon
    ribbon:
      eager-load:
        # 开启饥饿加载模式
        enabled: true
        # 指定需要饥饿加载的服务名
        clients:  http://tina:123456@localhost:8001/eureka/,http://tina:123456@localhost:8002/eureka/
      # 设置ribbon 最大连接数
      MaxTotalConnections: 500
      # 设置ribbon 每个host 最大的连接数
      MaxConnectionsPerHost: 500
      #对当前实例重试的次数
      maxAutoRetries: 5
      #切换实例的重试次数
      maxAutoRetriesNextServer: 5
      #对所有的操作请求都进行重试
      okToRetryOnAllOperations: true
      #Http响应码进行重试
      retryableStatusCodes: 500,404,502
    ### 针对单个服务的 Ribbon 配置
    eureka-provider:
      ribbon:
        # 基于配置文件形式的 针对单个服务的 Ribbon指定负载均衡策略
        NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #随机
    
    
    #配置feign中 RemoteClient设置的日志级别
    logging:
      level:
        com.tina.eureka.consumer.RemoteClient: info
    feign:
      hystrix:
        enabled: true #默认是不开启,设置为true 是开启熔断的功能
     #配置熔断的超时时间
      command:
        default:
          execution:
            isolation:
              thread:
                timeoutInMillisecond: 50000 # 熔断超时时长:10000ms
    
    
    
    1. config
    @Component
    public class HystrixFallBack implements FallbackFactory<UserClient> {
        private Logger logger= LoggerFactory.getLogger(HystrixFallBack.class);
        @Override
        public UserClient create(final Throwable cause) {
            logger.error("HystrixFallBack回退的原因",cause);
            return new UserClient(){
                @Override
                public String getById(String id) {
                    return "fail 调用失败了!!";
                }
            };
        }
    }
    =======================================================
    @Configuration
    public class FeignConfiguration {
        @Bean
        Logger.Level feiginLoggerLevel(){
            return Logger.Level.FULL;
        }
    }
    =======================================================
    @FeignClient(name = "eureka-provider",
            configuration = FeignConfiguration.class,
            fallbackFactory = HystrixFallBack.class
    )
    public interface UserClient {
    
        @GetMapping("/user/getById/{id}")
        String getById(@PathVariable String id);
    
    
    }
    
    
    
    @RestController
    @RequestMapping(value ="/consumer")
    public class UserController{
    
        //使用Feign远程调用
        @Autowired
        private UserClient userClient;
    
    
        @GetMapping("/getById/{id}")
        public Object getById(@PathVariable  String id){
            return  userClient.getById(id).toString();
        }
    
    
        /**
         * 利用RestTemplate调用
         * @Autowired
         *     private RestTemplate restTemplate;
         *
         *     @GetMapping(value ="/consumer/getById/{id}")
         *     public String getById(@PathVariable  String id){
         *        return restTemplate.getForObject("http://tina:123456@eureka-provider/user/getById/"+id,String.class);
         *     }
         *
         */
    
    }
    
    

    相关文章

      网友评论

        本文标题:springcloud-consumer=>集成eureka,

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