首页 > 其他分享 >eclipse微服务续

eclipse微服务续

时间:2023-02-16 17:22:37浏览次数:35  
标签:网关 服务 spring eclipse server 路由 gateway cloud

四、Hystrix延迟和容错库

 SpringCloud默认已为Feign整合了hystrix,所以添加Feign依赖后就不用在添加hystrix依赖了。

1.在Fegin中开启hystrix

修改consumer_server_12000工程的application.properties文件,开启hystrix熔断机制

 #在feign中开启hystrix熔断机制
feign.hystrix.enabled=true

2.创建FeignClient接口实现类

在spc_consumer_server_12000的com.test.feign包中创建实现CourseFeignClient.java的类CourseFeignError.java

 

 

 CourseFeignError.java

package com.test.feign;

import org.springframework.stereotype.Component;

import com.test.po.Course;
import com.test.po.ResponseBean;

//启动Hystrix熔断后回退功能,大体相当于try...catch中catch语句作用
@Component
public class CourseFeignError implements CourseFeignClient {

    @Override
    public ResponseBean<Course> getCourse(String cno) {
        // TODO Auto-generated method stub
        ResponseBean<Course> rb=new ResponseBean<Course>(700,"后端业务节点异常");
        return rb;
    }

}

3.在CourseFeignClient.java中做点修改,给@FeignClient注解后边括号加fallback=CourseFeignError.class

测试

五、Gateway微服务网关

1.新建网关项目模块

在spbcloud_demo父类项目下新建spc_gateway_server_14000

 

 

 2.添加依赖

<!--加入gateway依赖 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>

    <!--热部署 gav -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>

 

 

 3.添加主启动类

 

 

 

 4.添加属性配置文件application.properties

application.properties

server.port=14000
spring.application.name=gateway-server
#这里起名用驼峰法则,自动会解析成consumer-server
spring.cloud.gateway.routes[0].id=consumerServer
#如果前端访问网关中的路由中Path,那么网关会按照路由信息来匹配相应的uri,实际访问路径就会变成http://127.0.0.1:12000/course/getCourse/100
spring.cloud.gateway.routes[0].uri=http://127.0.0.1:12000
#此路由暴露给前端程序,前端访问时用的路径是http://localhost:14000/course/getCourse/100
spring.cloud.gateway.routes[0].predicates[0]=Path=/course/getCourse/**

 

 

 测试

 

5.动态路由(面向服务路由)

Gateway支持与Eureka整合开发,根据服务名自动从注册中心中获取服务地址并转发请求。这样做不仅能够通过单个端点来访问应用中的所有服务,而且在添加、移除或修改服务时,不用修改Gateway的路由配置。这就是动态路由(面向服务的路由)。

 5.1将网关注册到Eureka中,在spc_gateway_server_14000 子工程中添加Eureka Client的依赖

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

5.2修改spc_gateway_server_14000 的application.properties,将原来的非动态路由写法改成动态路由写法,并且将网关注册到Eureka中

application.properties

server.port=14000
spring.application.name=gateway-server
#这里起名用驼峰法则,自动会解析成consumer-server
spring.cloud.gateway.routes[0].id=consumerServer

#如果前端访问网关中的路由中Path,那么网关会按照路由信息来匹配相应的uri,实际访问路径就会变成http://127.0.0.1:12000/course/getCourse/100
#非动态路由服务写法,无法适应微服务模块真实IP发生变更的情况
#spring.cloud.gateway.routes[0].uri=http://127.0.0.1:12000

#动态路由服务的写法
spring.cloud.gateway.routes[0].uri=lb://consumer-server

#此路由暴露给前端程序,前端访问时用的路径是http://localhost:14000/course/getCourse/100
spring.cloud.gateway.routes[0].predicates[0]=Path=/course/getCourse/**

#让服务API网关注册到eureka服务端
eureka.client.service-url.defaultZone=http://eurekaServer13000:13000/eureka,http://eurekaServer13001:13001/eureka
#使用ip地址向Eureka注册
eureka.instance.prefer-ip-address=true
#上面的配置已经可以使用ip注册了,但显示的还是主机名,所以这里设置显示的注册名
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
#续约时间间隔(秒)
eureka.instance.lease-renewal-interval-in-seconds=10
#续约到期时间(秒)
eureka.instance.lease-expiration-duration-in-seconds=20

测试

 

6.网关熔断处理

不论是服务消费者调用服务提供者时、还是通过网关调用微服务时,都需要进行熔断处理。所以,Gateway也需要使用Hystrix进行熔断处理。也就是Gateway集成Hystrix。

6.1在pom.xml文件中添加Hystrix依赖:spc_gateway_server_14000

<!--加入hystrix的依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

6.2新建com.test.controller包,在里边建FallBackController类 spc_gateway_server_14000

 

 在将ResponseBean放进来

 

 FallBackController.java

package com.test.controller;

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

import com.test.po.ResponseBean;

@RestController
@RequestMapping("/fallback")
public class FallBackController {

    @GetMapping("/error")
    public ResponseBean back(){
        ResponseBean rb=new ResponseBean<>(700,"网关熔断");
        return rb;
    }
}

6.3在application.properties中添加配置spc_gateway_server_14000

#部署熔断处理的过滤器
spring.cloud.gateway.routes[0].filters[0].name=Hystrix
spring.cloud.gateway.routes[0].filters[0].args.name=fallbackcmd
spring.cloud.gateway.routes[0].filters[0].args.fallbackUri=forward:/fallback/error

 

 测试

 

标签:网关,服务,spring,eclipse,server,路由,gateway,cloud
From: https://www.cnblogs.com/liweimingbk/p/17126840.html

相关文章