springboot2.0和springcloud Finchley版项目搭建(包含eureka,gateWay,Freign,Hystrix)

网友投稿 271 2023-01-06


springboot2.0和springcloud Finchley版项目搭建(包含eureka,gateWay,Freign,Hystrix)

前段时间spring boot 2.0发布了,与之对应的spring cloud Finchley版本也随之而来了,两者之间的关系和版本对应详见我这边文章:spring boot和spring cloud对应的版本关系

项目地址:spring-cloud-demo

spring boot 1.x和spring cloud Dalston和Edgware版本搭建的微服务项目现在已经很流行了,现在很多企业都已经在用了,这里就不多说了。

使用版本说明:

spring boot 2.0.x

spring cloud Finchley.RELEASE

jdk 1.8

maven 3.9

Eureka 注册中心

spring cloud Finchley在支持spring 2.0时修改了eureka的jar包,把之前netflix系列的jar引入的时候都加上了netflix

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

新建一个eureka-service注册中心服务,pom.xml 文件内容如下:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.sunvalley

eureka-service

0.0.1-SNAPSHOT

jar

eureka-service

Demo project for Spring Boot

com.sunvalley.springcloud

spring-cloud-demo

0.0.1-SNAPSHOT

UTF-8

UTF-8

1.8

Finchley.RELEASE

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

org.springframework.boot

spring-boot-starter-test

test

org.springframework.cloud

spring-cloud-dependencies

${spring-cloud.version}

pom

import

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.sunvalley

eureka-service

0.0.1-SNAPSHOT

jar

eureka-service

Demo project for Spring Boot

com.sunvalley.springcloud

spring-cloud-demo

0.0.1-SNAPSHOT

UTF-8

UTF-8

1.8

Finchley.RELEASE

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

org.springframework.boot

spring-boot-starter-test

test

org.springframework.cloud

spring-cloud-dependencies

${spring-cloud.version}

pom

import

org.springframework.boot

spring-boot-maven-plugin

eureka-service服务的配置文件,application.yml

spring:

application:

name: eureka-service

server:

port: 5000

eureka:

instance:

hostname: localhost

client:

registerWithEureka: false

fetchRegistry: false

serviceUrl:

defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

eureka-service服务的配置文件,EurekaServickslLiALteApplication

@EnableEurekaServer

@SpringBootApplication

public class EurekaServiceApplication {

public static void main(String[] args) {

SpringApplication.run(EurekaServiceApplication.class, args);

}

}

使用IDEA启动eureka-service服务,本地访问 http://localhost:5000/ 即可看到注册中心内容。

服务消费者consumer和提供者provider

实际工作中大多数一个服务既是其它服务的消费者又有可能是服务的提供者,所以我们也就不用刻意的取区分开。

新建一个order-service服务,pom.xml文件

注意:除了eureka-client,openfeign等jar包外,记得引入spring-boot-starter-web不然会出现启动报错。

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.sunvalley

order-service

0.0.1-SNAPSHOT

jar

order-service

Demo project for Spring Cloud

com.sunvalley.springcloud

spring-cloud-demo

0.0.1-SNAPSHOT

UTF-8

UTF-8

1.8

Finchley.RELEASE

org.springframework.boot

spring-boot-starter

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.cloud

spring-cloud-starter-openfeign

org.springframework.cloud

spring-cloud-starter-netflix-hystrix

org.springframework.boot

spring-boot-starter-web

org.projectlombok

lombok

true

org.springframework.boot

spring-boot-starter-test

test

org.springframework.cloud

spring-cloud-dependencies

${spring-cloud.version}

pom

import

org.springframework.boot

spring-boot-maven-plugin

spring-snapshots

Spring Snapshots

https://repo.spring.io/snapshot

true

spring-milestones

Spring Milestones

https://repo.spring.io/milestone

false

spring-snapshots

Spring Snapshots

https://repo.spring.io/snapshot

true

spring-milestones

Spring Milestones

https://repo.spring.io/milestone

false

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.sunvalley

order-service

0.0.1-SNAPSHOT

jar

order-service

Demo project for Spring Cloud

com.sunvalley.springcloud

spring-cloud-demo

0.0.1-SNAPSHOT

UTF-8

UTF-8

1.8

Finchley.RELEASE

org.springframework.boot

spring-boot-starter

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.cloud

spring-cloud-starter-openfeign

org.springframework.cloud

spring-cloud-starter-netflix-hystrix

org.springframework.boot

spring-boot-starter-web

org.projectlombok

lombok

true

org.springframework.boot

spring-boot-starter-test

test

org.springframework.cloud

spring-cloud-dependencies

${spring-cloud.version}

pom

import

org.springframework.boot

spring-boot-maven-plugin

spring-snapshots

Spring Snapshots

https://repo.spring.io/snapshot

true

spring-milestones

Spring Milestones

https://repo.spring.io/milestone

false

spring-snapshots

Spring Snapshots

https://repo.spring.io/snapshot

true

spring-milestones

Spring Milestones

https://repo.spring.io/milestone

false

order-service服务的配置文件,application.yml

spring:

application:

name: order-service

server:

port: 5100

eureka:

client:

service-url:

defaultZone: http://localhost:5000/eureka/

feign:

hystrix:

enabled: true

order-service服务的启动类,OrderServiceApplication

@EnableCircuitBreaker

@EnableFeignClients

@EnableEurekaClient

@SpringBootApplication

public class OrderServiceApplication {

public static void main(String[] args) {

SpringApplication.run(OrderServiceApplication.class, args);

}

}

order-service服务的接口,OrderController

@RestController

@RequestMapping("/order")

public class OrderController {

@Value("${server.port}")

private String port;

/**

* 获取服务端口号

* @return

*/

@GetMapping("/getOrderPort")

public String getOrderPort() {

return "order-service port:" + port;

}

}

新建一个user-service服务,pom.xml文件跟order-service一样的这里就不列出来了

user-service服务的配置文件,application.yml

spring:

application:

name: user-service

server:

port: 5200

eureka:

client:

service-url:

defaultZone: http://localhost:5000/eureka/

feign:

hystrix:

enabled: true

user-service服务的启动类也跟order-service一样的

在user-service工程目录下新建一个package,再新建一个OrderRemote接口使用feign调用order-service的方法

@FeignClient(value = "order-service", fallback = OrderRemoteHystrix.class)

public interface OrderRemote {

@GetMapping("/order/getOrderPort")

String getOrderPort();

}

熔断机制提示类

@Component

public class OrderRemoteHystrix implements OrderRemote {

@Override

public String getOrderPort() {

return "order service 调用失败!";

}

}

user-service服务的接口,UserController

@RestController

@RequestMapping("/user")

public class UserController {

@Autowired

OrderRemote orderRemote;

@Value("${server.port}")

String port;

/**

* 获取用户服务的端口

* @return

*/

@GetMapping("/getUserPort")

public String getUserPort() {

return "user-service port:" + port;

}

/**

* 获取订单服务的端口

* @return

*/

@GetMapping("/getOrderPort")

public String getOrderPort() {

return "user-order-service port:" + orderRemote.getOrderPort();

}

}

测试,分别启动eureka-service,order-service,user-service就可以在注册中心看到服务

在浏览器访问 http://localhost:5100/order/getOrderPort 调用order-service的接口

在浏览器访问 http://localhost:5200/user/getOrderPort 通过user-service使用feign声明式调用order-service的接口

关闭order-service,再次调用就会发现熔断机制起了作用

spring cloud gateway

Spring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技术开发的网关,Spring Cloud Gateway旨在为微服务架构提供一种简单而有效的统一的API路由管理方式。Spring Cloud Gateway作为Spring Cloud生态系中的网关,目标是替代Netflix ZUUL,其不仅提供统一的路由方式,并且基于Filter链的方式提供了网关基本的功能,例如:安全,监控/埋点,和限流等。

新建一个gateway-service服务,pom.xml文件如下

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.sunvalley

gateway-service

0.0.1-SNAPSHOT

jar

gateway-service

Demo project for Spring Cloud

com.sunvalley.springcloud

spring-cloud-demo

0.0.1-SNAPSHOT

UTF-8

UTF-8

1.8

Finchley.RELEASE

org.springframework.boot

spring-boot-starter-actuator

org.springframework.cloud

spring-cloud-starter-gateway

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.cloud

spring-cloud-starter-netflix-hystrix

org.springframework.boot

spring-boot-devtools

runtime

org.projectlombok

lombok

true

org.springframework.boot

spring-boot-starter-test

test

org.springframework.cloud

spring-cloud-dependencies

${spring-cloud.version}

pom

import

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.sunvalley

gateway-service

0.0.1-SNAPSHOT

jar

gateway-service

Demo project for Spring Cloud

com.sunvalley.springcloud

spring-cloud-demo

0.0.1-SNAPSHOT

UTF-8

UTF-8

1.8

Finchley.RELEASE

org.springframework.boot

spring-boot-starter-actuator

org.springframework.cloud

spring-cloud-starter-gateway

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.cloud

spring-cloud-starter-netflix-hystrix

org.springframework.boot

spring-boot-devtools

runtime

org.projectlombok

lombok

true

org.springframework.boot

spring-boot-starter-test

test

org.springframework.cloud

spring-cloud-dependencies

${spring-cloud.version}

pom

import

org.springframework.boot

spring-boot-maven-plugin

gateway-service的配置文件,application.yml

spring:

application:

name: gateway-service

cloud: # spring cloud gateway 路由配置方式

gateway:

discovery: #是否与服务发现组件进行结合,通过 serviceId(必须设置成大写) 转发到具体的服务实例。默认为false,设为true便开启通过服务中心的自动根据 serviceId 创建路由的功能。

locator: #路由访问方式:http://Gateway_HOST:Gateway_PORT/大写的serviceId/**,其中微服务应用名默认大写访问。

enabled: true

routes:

- id: 163 #网关路由到网易官网

uri: http://163.com/

predicates:

- Path=/163/**

# - id: ORDER-SERVICE #网关路由到订单服务order-service

# uri: lb://ORDER-SERVICE

# predicates:

# - Path=/ORDER-SERVICE/**

# - id: USER-SERVICE #网关路由到用户服务user-service

# uri: lb://USER-SERVICE

# predicates:

# - Pach=/USER-SERVICE/**

server:

port: 5001

logging:

level:

org.springframework.cloud.gateway: trace

org.springframework.http.server.reactive: debug

org.springframework.web.reactive: debug

reactor.ipc.netty: debug

eureka:

client:

service-url:

defaultZone: http://localhost:5000/eureka/

feign:

hystrix:

enabled: true

gateway-service的启动类,Application

package com.sunvalley.demo;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.gateway.route.RouteLocator;

import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;

import org.springframework.context.annotation.Bean;

@SpringBootApplication

public class GatewayServiceApplication {

/**

* spring cloud gateway 配置方式之一,启动主程序配置,还有一种是配置文件配置

* @param builder

* @return

*/

@Bean

public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {

return builder.routes()

.route(r -> r.path("/qq/**")

.and()

.uri("http://qq.com/"))

.build();

}

public static void main(String[] args) {

SpringApplication.run(GatewayServiceApplication.class, args);

}

}

通过上面我们可以看到,gateway网关路由配置有两种方式:

1.通过@Bean自定义RouteLocator,在启动主类Application中配置

2.在配置文件yml中配置

这两种方式都可以实现网关路由是等价的,但是通常项目开发中会使用配置文件yml方式。

运行测试:

访问 http://localhost:8080/qq,路由转发到 http://qq.com

访问http://localhost:8080/163, 路由转发到 http://163.com


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:做接口测试的工具有哪些(接口测试常用工具有哪些)
下一篇:做接口测试的工具(做接口测试的工具是什么)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~