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

1.微服务

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

本例子为你提供了最简单的一个服务发现例子,包含服务注册发现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

利用SpringCloud搭建一个最简单的微服务框架的更多相关文章

  1. 小D课堂 - 新版本微服务springcloud+Docker教程_2_03常见的微服务框架

    笔记 3.常见的微服务框架     简介:讲解常用的微服务框架 consumer: 调用方         provider: 被调用方         一个接口一般都会充当两个角色(不是同时充当) ...

  2. 为什么Dapr是比SpringCloud和Istio更优雅的微服务框架?

    Dapr 是微软主导的云原生开源项目,2019年10月首次发布,到正式发布 V1.0 版本的不到一年的时间内,github star 数达到了 1.2万(现在已经超过1.7万星),超过同期的 kube ...

  3. 【微服务】使用spring cloud搭建微服务框架,整理学习资料

    写在前面 使用spring cloud搭建微服务框架,是我最近最主要的工作之一,一开始我使用bubbo加zookeeper制作了一个基于dubbo的微服务框架,然后被架构师否了,架构师曰:此物过时.随 ...

  4. SpringCloud升级之路2020.0.x版-2.微服务框架需要考虑的问题

    本系列为之前系列的整理重启版,随着项目的发展以及项目中的使用,之前系列里面很多东西发生了变化,并且还有一些东西之前系列并没有提到,所以重启这个系列重新整理下,欢迎各位留言交流,谢谢!~ 上图中演示了一 ...

  5. 微服务框架surging学习之路——序列化

    1.对微服务的理解 之前看到在群里的朋友门都在讨论微服务,看到他们的讨论,我也有了一些自己的理解,所谓微服务就是系统里的每个服务都 可以自由组合.自由组合这个就很厉害了,这样一来,每个服务与服务之间基 ...

  6. 微服务框架surging学习之路——序列化 (转载https://www.cnblogs.com/alangur/p/10407727.html)

    微服务框架surging学习之路——序列化   1.对微服务的理解 之前看到在群里的朋友门都在讨论微服务,看到他们的讨论,我也有了一些自己的理解,所谓微服务就是系统里的每个服务都 可以自由组合.自由组 ...

  7. SpringCloud微服务实战——搭建企业级开发框架(四十四):【微服务监控告警实现方式一】使用Actuator + Spring Boot Admin实现简单的微服务监控告警系统

      业务系统正常运行的稳定性十分重要,作为SpringBoot的四大核心之一,Actuator让你时刻探知SpringBoot服务运行状态信息,是保障系统正常运行必不可少的组件.   spring-b ...

  8. 利用 nodeJS 搭建一个简单的Web服务器(转)

    下面的代码演示如何利用 nodeJS 搭建一个简单的Web服务器: 1. 文件 WebServer.js: //-------------------------------------------- ...

  9. 简单Spring Cloud 微服务框架搭建

    微服务是现在比较流行的技术,对于程序猿而言,了解并搭建一个基本的微服务框架是很有必要滴. 微服务包含的内容非常多,一般小伙伴们可以根据自己的需求不断添加各种组件.框架. 一般情况下,基本的微服务框架包 ...

随机推荐

  1. SQL SERVER 数据库被标记为“可疑”的解决办法

    问题背景: 日常对Sql Server 2005关系数据库进行操作时,有时对数据库(如:Sharepoint网站配置数据库名Sharepoint_Config)进行些不正常操作如数据库在读写时而无故停 ...

  2. eclipse CDT写c++使用文件作为输入源(输入重定向)

    在main函数第一句添加下面. freopen("inputfile","r",stdin); 创建一个inputfile,放project根文件夹下. 注意添 ...

  3. atitit.html编辑器的设计要点与框架选型 attilax总结

    atitit.html编辑器的设计要点与框架选型 attilax总结 1. html编辑器的设计要求1 1.1. 障碍訪问 1 1.2. 强大Ajax上传 1 1.3. Word完美支持 2 1.4. ...

  4. vivado自定IP例化的问题,怎么生成VHDL的例化

    在tools 下面选中project settings.然后选targat language为VHDL .这样就会生成一个以VHDL语言为模板的ip. 转载:https://zhidao.baidu. ...

  5. 评价linux协议栈tcp实现中的prequeue

    https://blog.csdn.net/dog250/article/details/5464513 https://wiki.aalto.fi/download/attachments/7078 ...

  6. sqlserver 时间字符串转化为时间格式

    ),),),),,)) select substring('D:\\files,3,len('D:\\files)-2) --去掉前两位路径D:

  7. 【转】【WebStorm】利用WebStorm来管理你的Github

    用webstorm上传代码时,首先要先下载git,网址一搜就可以搜到,然后开始配置webstorm,打开webstorm,在file-settings中直接搜索github,然后输入自己github的 ...

  8. 【Android】json格式详解

    JSON有两种结构 1. “名称/值”对的集合(A collection of name/value pairs).    不同的语言中,它被理解为对象(object),记录(record),结构(s ...

  9. IntelliJ IDEA 14.1.4导入项目启动报错:Error during artifact deployment.[组件部署期间出错]

    1.问题描述:Error during artifact deployment.[组件部署期间出错] 2.删除Artifacts 3.刷新 4.重新生成Artifacts 5.重新选择 再重新启动项目 ...

  10. 【转】31个实用的find命令

    find . -name "*.sql" -exec md5sum {} \; 一.主要内容 ====================================== . 用文 ...