我的个人网站:等不见天亮等时光
环境介绍
-
相关组件版本
SpringBoot :2.3.2.RELEASE SpringCloud :Hoxton.SR5 SpringCloud-GateWay :2.2.3.RELEASE SpringCloud-Hystrix :2.2.3.RELEASE SpringCloud-Feign :2.2.3.RELEASE SpringCloud-Eureka :2.2.3.RELEASE SpringCloud-Hystrix :2.2.3.RELEASE
-
搭建服务模块及相关功能介绍
cloud-eureka > 微服务注册中心 cloud-gateway > 微服务网关,承载服务限流,熔断,鉴权 cloud-cus > 微服务消费模块,集成Feign进行调用,Hystrix服务熔断 cloud-pro > 微服务生产模块,rest服务提供,分库分表
3.服务模块搭建
- 新建空白project,删除src目录,parent的pom依赖如下
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.2.RELEASE</version> </parent> <!--SpringBoot基础依赖--> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.75</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- 代码库 --> <repositories> <repository> <id>maven-ali</id> <url>http://maven.aliyun.com/nexus/content/groups/public//</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> <checksumPolicy>fail</checksumPolicy> </snapshots> </repository> <repository> <id>springsource-repos</id> <name>SpringSource Repository</name> <url>http://repo.spring.io/release/</url> </repository> <repository> <id>central-repos</id> <name>Central Repository</name> <url>http://repo.maven.apache.org/maven2</url> </repository> </repositories>
- 新建服务模块cloud-eureka,服务依赖,配置文件与启动类如下,需要注意的是,需要使用@EnableEurekaServer在启动类上,标注当前服务为注册中心服务端,启动后访问http://localhost:9000,即可访问到eureka的控制台
<dependencies> <!--eureka依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
server: port: 9000 spring: application: name: cloud-eureka eureka: client: register-with-eureka: false fetch-registry: false service-url: default-zone: http://${eureka.instance.hostname}:${server.port}/eureka/ instance: hostname: localhost
- 新建服务提供模块,cloud-cus,依赖与配置文件配置如下;
<dependencies> <!--熔断器--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> <!--eureka--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!--feign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> </dependencies>
package com.sunyw.xyz; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.cloud.openfeign.EnableFeignClients; /** * 名称: XX定义 * 功能: <功能详细描述> * 方法: <方法简述-方法描述> * 版本: 1.0 * 作者: sunyw * 说明: 说明描述 * 时间: 2021/9/30 15:54 */ @SpringBootApplication //启动hystrix控制台 @EnableHystrixDashboard //注册中心连接 @EnableEurekaClient //启动feign调用 @EnableFeignClients //启动熔断器 @EnableCircuitBreaker public class CusApplication{ public static void main(String[] args) { SpringApplication.run(CusApplication.class,args); } }
#端口号 server: port: 9001 #服务名称 spring: application: name: cloud-cus #redis配置 redis: host: 127.0.0.1 port: 6379 password: 123456 #注册中心 eureka: client: service-url: defaultZone: http://127.0.0.1:7000/eureka/ instance: ip-address: true hostname: 127.0.0.1 lease-renewal-interval-in-seconds: 5 #fegin熔断器启用 feign: hystrix: enabled: true #负载均衡配置 ribbon: eureka: enabled: true
- 启动cus模块可在eureka控制台查看到当前服务信息
- 创建cloud-pro模块,配置文件信息,依赖如下所示
<dependencies> <!--熔断器--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> <!--eureka--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!--feign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies>
#端口号 server: port: 9002 #服务名称 spring: application: name: cloud-pro #redis配置 redis: host: 127.0.0.1 port: 6379 password: 123456 #注册中心 eureka: client: service-url: defaultZone: http://127.0.0.1:7000/eureka/ instance: ip-address: true hostname: 127.0.0.1 lease-renewal-interval-in-seconds: 5 #fegin熔断器启用 feign: hystrix: enabled: true #负载均衡配置 ribbon: eureka: enabled: true
package com.sunyw.xyz; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.cloud.openfeign.EnableFeignClients; /** * 名称: XX定义 * 功能: <功能详细描述> * 方法: <方法简述-方法描述> * 版本: 1.0 * 作者: sunyw * 说明: 说明描述 * 时间: 2021/9/30 15:54 */ @SpringBootApplication @EnableHystrixDashboard @EnableEurekaClient @EnableFeignClients @EnableCircuitBreaker public class ProApplication { public static void main(String[] args) { SpringApplication.run(ProApplication.class,args); } }
- 启动查看eureka情况
- 搭建cloud-gateway,配置文件,依赖如下,配置文件的属性信息会在配置文件中解释清楚
<dependencies> <!--网关--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <!--eureka--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!--hystrix--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <!--redis限流--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis-reactive</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
server: port: 9003 #注册中心 eureka: client: service-url: defaultZone: http://127.0.0.1:9000/eureka/ instance: ip-address: true hostname: 127.0.0.1 lease-renewal-interval-in-seconds: 5 spring: main: web-application-type: reactive application: name: cloud-gateway cloud: #网关路由配置 gateway: discovery: locator: lower-case-service-id: true routes: #cus模块 - id: cloud-cus uri: lb://cloud-cus predicates: - Path=/cloud-cus/** #pro模块 - id: cloud-pro uri: lb://cloud-pro predicates: - Path=/cloud-pro/** #默认过滤器 default-filters: - StripPrefix=1 - name: Hystrix args: name: fallback #调用失败熔断默认转发 fallbackUri: forward:/fallback
package com.sunyw.xyz; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * 名称: XX定义 * 功能: <功能详细描述> * 方法: <方法简述-方法描述> * 版本: 1.0 * 作者: sunyw * 说明: 说明描述 * 时间: 2021/9/30 16:18 */ @SpringBootApplication @EnableEurekaClient public class GateWayApplication { public static void main(String[] args) { SpringApplication.run(GateWayApplication.class,args); } }
- 启动网关后查看服务是否注册成功,
- 在cloud-pro模块编写服务提供,在cloud-cus编写服务消费通过feign调用,并配置熔断进行测试;
- 在cloud-pro下新建ServerController
package com.sunyw.xyz.web; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.UUID; /** * 名称: XX定义 * 功能: <功能详细描述> * 方法: <方法简述-方法描述> * 版本: 1.0 * 作者: sunyw * 说明: 说明描述 * 时间: 2021/9/30 16:21 */ @RestController public class ServerController { private Logger log=LoggerFactory.getLogger(ServerController.class); /** * pro服务 * * @param version 版本 * @return String */ @GetMapping("/pro") public String serverPro(@RequestParam(value="version") String version) { log.info("-------------------pro接收到服务请求 > > > > >:{}",version); return "请求Pro成功,序列号:[" + UUID.randomUUID().toString() + "],版本为:" + version; } }
- 在cloud-cus下新建CusController,FeignService(feign远程调用),FallBackServer(熔断响应类)
package com.sunyw.xyz.web; import com.sunyw.xyz.service.FeignService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * 名称: XX定义 * 功能: <功能详细描述> * 方法: <方法简述-方法描述> * 版本: 1.0 * 作者: sunyw * 说明: 说明描述 * 时间: 2021/9/30 16:27 */ @RestController public class CusController { private Logger log=LoggerFactory.getLogger(CusController.class); @Autowired private FeignService feignService; /** * cus服务 * * @param version 版本 * @return String */ @GetMapping("/cus") public String serverPro(@RequestParam(value="version") String version) { log.info("-------------------cus接收到服务请求 > > > > >:{}",version); return feignService.proInvoke(version); } }
package com.sunyw.xyz.service; import com.sunyw.xyz.fallback.FallBackService; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; /** * 名称: XX定义 * 功能: <功能详细描述> * 方法: <方法简述-方法描述> * 版本: 1.0 * 作者: sunyw * 说明: 说明描述 * 时间: 2021/9/30 16:27 */ @FeignClient(value="cloud-pro", fallback=FallBackService.class) public interface FeignService { @RequestMapping("/pro") String proInvoke(@RequestParam(value="version") String version); }
package com.sunyw.xyz.fallback; import com.alibaba.fastjson.JSON; import com.sunyw.xyz.service.FeignService; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.Map; /** * 名称: XX定义 * 功能: <功能详细描述> * 方法: <方法简述-方法描述> * 版本: 1.0 * 作者: sunyw * 说明: 说明描述 * 时间: 2021/9/30 16:28 */ @Component public class FallBackService implements FeignService { @Override public String proInvoke(String version) { Map<String,String> error=new HashMap<>(); error.put("code","500"); error.put("msg","服务暂时不可用"); return JSON.toJSONString(error); } }
- 重启服务,通过网关进行服务调用,SpringCloudGateWay同zuul相比,zuul启动后配置网关路由后无法直接调用微服务子模块,只能通过网关路由,gateway则可以直接调用,解决方案为服务模块直接进行鉴权,或将服务部署内网中,本次将使用服务之间鉴权来解决,在下文中将会体现
- 未完待续