利用SpringCloud搭建一个最简单的微服务框架

http://blog.csdn.net/caicongyang/article/details/52974406

1.微服务

微服务主要包含服务注册,服务发现,服务路由,服务配置,服务熔断,服务降级等一系列的服务,而Spring Cloud为我们提供了个一整套的服务;

利用SpringCloud搭建一个最简单的微服务框架

本例子为你提供了最简单的一个服务发现例子,包含服务注册发现spingCloudEurekaServer、服务配置中心spingCloudConfServer、以及一个app应用springCloudApp

2.服务注册与发现

spingCloudEurekaServer

pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.caicongyang</groupId>
  4. <artifactId>spingCloudEurekaServer</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. <parent>
  7. <groupId>org.springframework.cloud</groupId>
  8. <artifactId>spring-cloud-starter-parent</artifactId>
  9. <version>Angel.SR6</version>
  10. </parent>
  11. <dependencies>
  12. <dependency>
  13. <groupId>org.springframework.cloud</groupId>
  14. <artifactId>spring-cloud-starter-eureka-server</artifactId>
  15. </dependency>
  16. </dependencies>
  17. <build>
  18. <plugins>
  19. <plugin>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-maven-plugin</artifactId>
  22. </plugin>
  23. </plugins>
  24. </build>
  25. </project>

Application.java

  1. package com.caicongyang.eureka;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. /**
  6. * Spring could EurekaServer程序主入口
  7. *
  8. * @author Administrator
  9. *
  10. */
  11. @SpringBootApplication
  12. @EnableEurekaServer
  13. public class Application {
  14. public static void main(String[] args) {
  15. SpringApplication.run(Application.class, args);
  16. }
  17. }

application.yml  (可用properties替代)

  1. server:
  2. port: 9999
  3. eureka:
  4. instance:
  5. hostname: 127.0.0.1
  6. client:
  7. registerWithEureka: false
  8. fetchRegistry: false
  9. serviceUrl:
  10. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

3.服务配置(全局配置中心)

pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.caicongyang</groupId>
  5. <artifactId>spingCloudConfServer</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <parent>
  8. <groupId>org.springframework.cloud</groupId>
  9. <artifactId>spring-cloud-starter-parent</artifactId>
  10. <version>Angel.SR6</version>
  11. </parent>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.cloud</groupId>
  15. <artifactId>spring-cloud-config-server</artifactId>
  16. </dependency>
  17. <!-- sping cloud 注册服务 -->
  18. <dependency>
  19. <groupId>org.springframework.cloud</groupId>
  20. <artifactId>spring-cloud-starter-eureka</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-test</artifactId>
  25. <scope>test</scope>
  26. </dependency>
  27. </dependencies>
  28. <build>
  29. <plugins>
  30. <plugin>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-maven-plugin</artifactId>
  33. </plugin>
  34. </plugins>
  35. <defaultGoal>compile</defaultGoal>
  36. </build>
  37. </project>

application.java

  1. package com.caiconyang.conf;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.config.server.EnableConfigServer;
  5. /**
  6. * Spring could conf程序主入口
  7. * @author Administrator
  8. *
  9. */
  10. @SpringBootApplication
  11. @EnableConfigServer
  12. public class Application {
  13. public static void main(String[] args) {
  14. SpringApplication.run(Application.class,args);
  15. }
  16. }

application.properties

  1. server.port=8888
  2. ## App配置文件所在git地址
  3. spring.cloud.config.server.git.uri=https://git.oschina.net/caicongyang/springCloudConfigRepo.git
  4. spring.cloud.config.server.git.searchPaths=repo
  5. spring.application.name=spingCloudConfServer

4.App

pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.caicongyang</groupId>
  4. <artifactId>springCloudApp</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. <parent>
  7. <groupId>org.springframework.cloud</groupId>
  8. <artifactId>spring-cloud-starter-parent</artifactId>
  9. <version>Angel.SR6</version>
  10. </parent>
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. <java.version>1.7</java.version>
  14. <java.encoding>UTF-8</java.encoding>
  15. <springfox.swagger.version>2.2.2</springfox.swagger.version>
  16. </properties>
  17. <dependencies>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-web</artifactId>
  21. </dependency>
  22. <!-- sping cloud 监控  http://localhost:8080/health -->
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-actuator</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.cloud</groupId>
  29. <artifactId>spring-cloud-starter-config</artifactId>
  30. </dependency>
  31. <!-- sping cloud 注册服务 -->
  32. <dependency>
  33. <groupId>org.springframework.cloud</groupId>
  34. <artifactId>spring-cloud-starter-eureka</artifactId>
  35. </dependency>
  36. <!-- sping cloud 路由 -->
  37. <dependency>
  38. <groupId>org.springframework.cloud</groupId>
  39. <artifactId>spring-cloud-starter-hystrix</artifactId>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-starter-test</artifactId>
  44. <scope>test</scope>
  45. </dependency>
  46. <dependency>
  47. <groupId>io.springfox</groupId>
  48. <artifactId>springfox-swagger2</artifactId>
  49. <version>${springfox.swagger.version}</version>
  50. </dependency>
  51. <dependency>
  52. <groupId>io.springfox</groupId>
  53. <artifactId>springfox-swagger-ui</artifactId>
  54. <version>${springfox.swagger.version}</version>
  55. </dependency>
  56. </dependencies>
  57. <build>
  58. <finalName>spingcould</finalName>
  59. <plugins>
  60. <plugin>
  61. <groupId>org.apache.maven.plugins</groupId>
  62. <artifactId>maven-compiler-plugin</artifactId>
  63. <configuration>
  64. <source>${java.version}</source>
  65. <target>${java.version}</target>
  66. <encoding>${java.encoding}</encoding>
  67. <showWarnings>true</showWarnings>
  68. </configuration>
  69. </plugin>
  70. </plugins>
  71. </build>
  72. </project>

Application.java

  1. package com.caicongyang.springCloudApp.main;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.context.annotation.ComponentScan;
  6. import org.springframework.context.annotation.Configuration;
  7. /**
  8. * Spring could web程序主入口
  9. * @author Administrator
  10. *
  11. */
  12. @Configuration//配置控制
  13. @EnableAutoConfiguration//启用自动配置
  14. @ComponentScan(value={"com.caicongyang.springCloudApp"})//组件扫描
  15. @EnableDiscoveryClient
  16. public class Application {
  17. public static void main(String[] args) {
  18. //第一个简单的应用,
  19. SpringApplication.run(Application.class,args);
  20. }
  21. }

SwaggerConfig.java

  1. package com.caicongyang.springCloudApp.conf;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import springfox.documentation.service.ApiInfo;
  6. import springfox.documentation.spi.DocumentationType;
  7. import springfox.documentation.spring.web.plugins.Docket;
  8. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  9. /**
  10. *
  11. * @author caicongyang1
  12. * @version id: SwaggerConfig, v 0.1 16/4/22 下午4:12 caicongyang1 Exp $$
  13. */
  14. @Configuration
  15. @EnableSwagger2
  16. public class SwaggerConfig {
  17. @Value("${swagger.ui.enable}") //该配置项在配置中心管理
  18. private boolean environmentSpecificBooleanFlag;
  19. @Bean
  20. public Docket docketFactory() {
  21. return new Docket(DocumentationType.SWAGGER_2).apiInfo(
  22. new ApiInfo("接口文档", "SpingCloud web接口列表", "1.0", "", "", "", "")).enable(environmentSpecificBooleanFlag);
  23. }
  24. }

application.properties

  1. server.port=8080
  2. spring.cloud.config.uri=http://127.0.0.1:8888
  3. spring.cloud.config.name=springCloudApp
  4. spring.cloud.config.profile=${config.profile:dev}
  5. #service discovery url
  6. eureka.client.serviceUrl.defaultZone=http://localhost:9999/eureka/
  7. #service name
  8. spring.application.name=springCloudApp

5.测试与验证

顺序启动服务注册发现spingCloudEurekaServer、服务配置中心spingCloudConfServer、以及一个app应用springCloudApp

测试与验证

1.访问http://localhost:9999/eureka/  app是否已经注册上来

2.访问 http://localhost:8080/swagger-ui.html 是否正常访问,如果正常访问说明争取读取到config配置中心的swagger.ui.enable配置项

6.源码:

以上所有源码:

https://git.oschina.net/caicongyang/springcloud.git

上一篇:linux下数学运算器:expr命令(shell中完成数学运算)


下一篇:MyEclipse 上使用sping+hibernate+mysql