spring cloud 实战项目

Spring Cloud简介

Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等操作提供了一种简单的开发方式。

Spring Cloud包含了多个子项目(针对分布式系统中涉及的多个不同开源产品),比如:Spring Cloud Config、Spring Cloud Netflix、Spring Cloud CloudFoundry、Spring Cloud AWS、Spring Cloud Security、Spring Cloud Commons、Spring Cloud Zookeeper、Spring Cloud CLI等项目。

1.搭建eureka服务

 

       1.1 创建springboot项目

 http://start.spring.io/ 自定义spring boot在线maven构建工具很方便

1.2 修改pom文件,添加spring cloud 依赖如下

 
  1. <dependencies>

  2. <dependency>

  3. <groupId>org.springframework.boot</groupId>

  4. <artifactId>spring-boot-starter-test</artifactId>

  5. <scope>test</scope>

  6. </dependency>

  7.  
  8. <dependency>

  9. <groupId>org.springframework.cloud</groupId>

  10. <artifactId>spring-cloud-starter-eureka-server</artifactId>

  11. </dependency>

  12. </dependencies>

  13.  
  14. <dependencyManagement>

  15. <dependencies>

  16. <dependency>

  17. <groupId>org.springframework.cloud</groupId>

  18. <artifactId>spring-cloud-dependencies</artifactId>

  19. <version>Dalston.RELEASE</version>

  20. <type>pom</type>

  21. <scope>import</scope>

  22. </dependency>

  23. </dependencies>

  24. </dependencyManagement>

 代码中加红的部分是对应的版本

如果 spring boot的版本是 1.4之前 对应的版本是Brixton.RELEASE,1.4之后 对应的Dalston.RELEASE版本如果对应不起来会报错

1.3 在启动类上加上注解 如下

 
  1. @EnableEurekaServer

  2. @SpringBootApplication

  3. public class SpringCloudApplication {

  4.  
  5. public static void main(String[] args) {

  6. SpringApplication.run(SpringCloudApplication.class, args);

  7. }

  8. }

1.4 配置文件application.properties

 
  1. #端口号

  2. server.port=1111

  3. # eureka.client.registerWithEureka :表示是否将自己注册到Eureka Server,默认为true。

  4. # 由于当前这个应用就是Eureka Server,故而设为false

  5. eureka.client.register-with-eureka=false

  6. # eureka.client.fetchRegistry :表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,

  7. # 不需要同步其他的Eureka Server节点的数据,故而设为false。

  8. eureka.client.fetch-registry=false

  9. # eureka.client.serviceUrl.defaultZone :设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是

  10. eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

配置好了以后启动  访问 http://localhost:1111/ 就出现了spring cloud 管理中心
 

2.搭建服务端

    2.1 创建springboot项目同上

    2.2 修改pom文件,添加spring cloud 依赖如下

 
  1.         <parent>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-parent</artifactId>

  4. <version>1.5.9.RELEASE</version>

  5. <relativePath/> <!-- lookup parent from repository -->

  6. </parent>

  7.  
  8. <properties>

  9. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  10. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

  11. <java.version>1.8</java.version>

  12. </properties>

  13.  
  14. <dependencies>

  15. <dependency>

  16. <groupId>org.springframework.boot</groupId>

  17. <artifactId>spring-boot-starter</artifactId>

  18. </dependency>

  19. <dependency>

  20. <groupId>org.springframework.cloud</groupId>

  21. <artifactId>spring-cloud-starter-config</artifactId>

  22. </dependency>

  23. <!-- sping cloud 注册服务 -->

  24. <dependency>

  25. <groupId>org.springframework.cloud</groupId>

  26. <artifactId>spring-cloud-starter-eureka</artifactId>

  27. </dependency>

  28. <dependency>

  29. <groupId>org.springframework.boot</groupId>

  30. <artifactId>spring-boot-starter-test</artifactId>

  31. <scope>test</scope>

  32. </dependency>

  33. <dependency>

  34. <groupId>org.springframework.boot</groupId>

  35. <artifactId>spring-boot-starter-web</artifactId>

  36. </dependency>

  37. </dependencies>

  38.  
  39. <dependencyManagement>

  40. <dependencies>

  41. <dependency>

  42. <groupId>org.springframework.cloud</groupId>

  43. <artifactId>spring-cloud-dependencies</artifactId>

  44. <version>Dalston.RELEASE</version>

  45. <type>pom</type>

  46. <scope>import</scope>

  47. </dependency>

  48. </dependencies>

  49. </dependencyManagement>

  50.  
  51. <build>

  52. <plugins>

  53. <plugin>

  54. <groupId>org.springframework.boot</groupId>

  55. <artifactId>spring-boot-maven-plugin</artifactId>

  56. </plugin>

  57. </plugins>

  58. </build>

2.3 在启动类上加上注解 如下

 
  1. @SpringBootApplication

  2. @EnableEurekaClient

  3. @EnableFeignClients

  4. public class SpringServiceApplication {

  5.  
  6. public static void main(String[] args) {

  7. SpringApplication.run(SpringServiceApplication.class, args);

  8. }

  9. }

2.4 配置文件application.properties

 
  1. #服务名称

  2. spring.application.name=compute-service1

  3. #端口号

  4. server.port=2222

  5. #注册中心

  6. eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

  7. spring.cloud.config.discovery.enabled=true

  8. #注册中心的服务id

  9. spring.cloud.config.discovery.serviceId=compute-server

    2.5启动项目后如下图    

红框内就是启动的项目,显示的是服务的id和端口号


 

3 spring cloud 路由网关服务

    3.1 创建 spring boot项目 同上

    3.2 修改pom文件,添加spring cloud 依赖如下

 
  1.     <groupId>com.xue</groupId>

  2. <artifactId>sring-zuul</artifactId>

  3. <version>0.0.1-SNAPSHOT</version>

  4. <packaging>jar</packaging>

  5.  
  6. <name>sring-zuul</name>

  7. <description>Demo project for Spring Boot</description>

  8.  
  9. <parent>

  10. <groupId>org.springframework.boot</groupId>

  11. <artifactId>spring-boot-starter-parent</artifactId>

  12. <version>1.5.9.RELEASE</version>

  13. <relativePath/> <!-- lookup parent from repository -->

  14. </parent>

  15.  
  16. <properties>

  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  18. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

  19. <java.version>1.8</java.version>

  20. </properties>

  21.  
  22. <dependencies>

  23. <dependency>

  24. <groupId>org.springframework.cloud</groupId>

  25. <artifactId>spring-cloud-starter-eureka</artifactId>

  26. </dependency>

  27. <dependency>

  28. <groupId>org.springframework.cloud</groupId>

  29. <artifactId>spring-cloud-starter-zuul</artifactId>

  30. </dependency>

  31. <dependency>

  32. <groupId>org.springframework.boot</groupId>

  33. <artifactId>spring-boot-starter-web</artifactId>

  34. </dependency>

  35.  
  36. <dependency>

  37. <groupId>org.springframework.boot</groupId>

  38. <artifactId>spring-boot-starter-test</artifactId>

  39. <scope>test</scope>

  40. </dependency>

  41. </dependencies>

  42. <dependencyManagement>

  43. <dependencies>

  44. <dependency>

  45. <groupId>org.springframework.cloud</groupId>

  46. <artifactId>spring-cloud-dependencies</artifactId>

  47. <version>Dalston.RELEASE</version>

  48. <type>pom</type>

  49. <scope>import</scope>

  50. </dependency>

  51. </dependencies>

  52. </dependencyManagement>

  53. <build>

  54. <plugins>

  55. <plugin>

  56. <groupId>org.springframework.boot</groupId>

  57. <artifactId>spring-boot-maven-plugin</artifactId>

  58. </plugin>

  59. </plugins>

  60. </build>

   

3.3 在启动类上加上注解 如下

        

 
  1. @EnableZuulProxy

  2. @EnableEurekaClient

  3. @SpringBootApplication

  4. public class SringZuulApplication {

  5.  
  6. public static void main(String[] args) {

  7. SpringApplication.run(SringZuulApplication.class, args);

  8. }

  9. }

   

3.4 配置文件application.properties

     

 
  1. eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

  2. server.port=3333

  3. spring.application.name=service-zuul

  4. #表示只要访问以/api-a/开头的多层目录都可以路由到 id为compute-service1的服务上

  5. zuul.routes.compute-service1=/api-a/**

#表示只要访问以/api-a/开头的多层目录都可以路由到 id为compute-service1的服务上
zuul.routes.compute-service1=/api-a/**
上面的一行等同于下面的两行
zuul.routes.api-a.path=/api-a/**
zuul.routes.api-a.serviceId=compute-service1

通配符 含义 举例 解释
? 匹配任意单个字符 /feign-consumer/? 匹配/feign-consumer/a,/feign-consumer/b,/feign-consumer/c等
* 匹配任意数量的字符 /feign-consumer/* 匹配/feign-consumer/aaa,feign-consumer/bbb,/feign-consumer/ccc等,无法匹配/feign-consumer/a/b/c
** 匹配任意数量的字符 /feign-consumer/* 匹配/feign-consumer/aaa,feign-consumer/bbb,/feign-consumer/ccc等,也可以匹配/feign-consumer/a/b/c

    3.4 启动项目 测试

   3.5服务过滤

zuul不仅只是路由,并且还能过滤,做一些安全验证。继续改造工程;

 
  1. @Component

  2. public class MyFilter extends ZuulFilter{

  3.  
  4. private static Logger log = LoggerFactory.getLogger(MyFilter.class);

  5. @Override

  6. public String filterType() {

  7. return "pre";

  8. }

  9.  
  10. @Override

  11. public int filterOrder() {

  12. return 0;

  13. }

  14.  
  15. @Override

  16. public boolean shouldFilter() {

  17. return true;

  18. }

  19.  
  20. @Override

  21. public Object run() {

  22. RequestContext ctx = RequestContext.getCurrentContext();

  23. HttpServletRequest request = ctx.getRequest();

  24. log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));

  25. Object accessToken = request.getParameter("token");

  26. if(accessToken == null) {

  27. log.warn("token is empty");

  28. ctx.setSendZuulResponse(false);

  29. ctx.setResponseStatusCode(401);

  30. try {

  31. ctx.getResponse().getWriter().write("token is empty");

  32. }catch (Exception e){}

  33.  
  34. return null;

  35. }

  36. log.info("ok");

  37. return null;

  38. }

  39. }

  • filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下: 
    • pre:路由之前
    • routing:路由之时
    • post: 路由之后
    • error:发送错误调用
    • filterOrder:过滤的顺序
    • shouldFilter:这里可以写逻辑判断,是否要过滤,本文true,永远过滤。
    • run:过滤器的具体逻辑。可用很复杂,包括查sql,nosql去判断该请求到底有没有权限访问。

猜你喜欢

转载自blog.csdn.net/cns15090972366/article/details/82623231