利用SpringCloud搭建一个最简单的微服务框架
http://blog.csdn.net/caicongyang/article/details/52974406
1.微服务
微服务主要包含服务注册,服务发现,服务路由,服务配置,服务熔断,服务降级等一系列的服务,而Spring Cloud为我们提供了个一整套的服务;
本例子为你提供了最简单的一个服务发现例子,包含服务注册发现spingCloudEurekaServer、服务配置中心spingCloudConfServer、以及一个app应用springCloudApp
2.服务注册与发现
spingCloudEurekaServer
pom.xml
- <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">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.caicongyang</groupId>
- <artifactId>spingCloudEurekaServer</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <parent>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-parent</artifactId>
- <version>Angel.SR6</version>
- </parent>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-eureka-server</artifactId>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
Application.java
- package com.caicongyang.eureka;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
- /**
- * Spring could EurekaServer程序主入口
- *
- * @author Administrator
- *
- */
- @SpringBootApplication
- @EnableEurekaServer
- public class Application {
- public static void main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
- }
application.yml (可用properties替代)
- server:
- port: 9999
- eureka:
- instance:
- hostname: 127.0.0.1
- client:
- registerWithEureka: false
- fetchRegistry: false
- serviceUrl:
- defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3.服务配置(全局配置中心)
pom.xml
- <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">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.caicongyang</groupId>
- <artifactId>spingCloudConfServer</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <parent>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-parent</artifactId>
- <version>Angel.SR6</version>
- </parent>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-config-server</artifactId>
- </dependency>
- <!-- sping cloud 注册服务 -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-eureka</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- <defaultGoal>compile</defaultGoal>
- </build>
- </project>
application.java
- package com.caiconyang.conf;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.config.server.EnableConfigServer;
- /**
- * Spring could conf程序主入口
- * @author Administrator
- *
- */
- @SpringBootApplication
- @EnableConfigServer
- public class Application {
- public static void main(String[] args) {
- SpringApplication.run(Application.class,args);
- }
- }
application.properties
- server.port=8888
- ## App配置文件所在git地址
- spring.cloud.config.server.git.uri=https://git.oschina.net/caicongyang/springCloudConfigRepo.git
- spring.cloud.config.server.git.searchPaths=repo
- spring.application.name=spingCloudConfServer
4.App
pom.xml
- <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">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.caicongyang</groupId>
- <artifactId>springCloudApp</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <parent>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-parent</artifactId>
- <version>Angel.SR6</version>
- </parent>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <java.version>1.7</java.version>
- <java.encoding>UTF-8</java.encoding>
- <springfox.swagger.version>2.2.2</springfox.swagger.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <!-- sping cloud 监控 http://localhost:8080/health -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-config</artifactId>
- </dependency>
- <!-- sping cloud 注册服务 -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-eureka</artifactId>
- </dependency>
- <!-- sping cloud 路由 -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-hystrix</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger2</artifactId>
- <version>${springfox.swagger.version}</version>
- </dependency>
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger-ui</artifactId>
- <version>${springfox.swagger.version}</version>
- </dependency>
- </dependencies>
- <build>
- <finalName>spingcould</finalName>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>${java.version}</source>
- <target>${java.version}</target>
- <encoding>${java.encoding}</encoding>
- <showWarnings>true</showWarnings>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
Application.java
- package com.caicongyang.springCloudApp.main;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
- import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- /**
- * Spring could web程序主入口
- * @author Administrator
- *
- */
- @Configuration//配置控制
- @EnableAutoConfiguration//启用自动配置
- @ComponentScan(value={"com.caicongyang.springCloudApp"})//组件扫描
- @EnableDiscoveryClient
- public class Application {
- public static void main(String[] args) {
- //第一个简单的应用,
- SpringApplication.run(Application.class,args);
- }
- }
SwaggerConfig.java
- package com.caicongyang.springCloudApp.conf;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import springfox.documentation.service.ApiInfo;
- import springfox.documentation.spi.DocumentationType;
- import springfox.documentation.spring.web.plugins.Docket;
- import springfox.documentation.swagger2.annotations.EnableSwagger2;
- /**
- *
- * @author caicongyang1
- * @version id: SwaggerConfig, v 0.1 16/4/22 下午4:12 caicongyang1 Exp $$
- */
- @Configuration
- @EnableSwagger2
- public class SwaggerConfig {
- @Value("${swagger.ui.enable}") //该配置项在配置中心管理
- private boolean environmentSpecificBooleanFlag;
- @Bean
- public Docket docketFactory() {
- return new Docket(DocumentationType.SWAGGER_2).apiInfo(
- new ApiInfo("接口文档", "SpingCloud web接口列表", "1.0", "", "", "", "")).enable(environmentSpecificBooleanFlag);
- }
- }
application.properties
- server.port=8080
- spring.cloud.config.uri=http://127.0.0.1:8888
- spring.cloud.config.name=springCloudApp
- spring.cloud.config.profile=${config.profile:dev}
- #service discovery url
- eureka.client.serviceUrl.defaultZone=http://localhost:9999/eureka/
- #service name
- 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搭建一个最简单的微服务框架的更多相关文章
- 小D课堂 - 新版本微服务springcloud+Docker教程_2_03常见的微服务框架
笔记 3.常见的微服务框架 简介:讲解常用的微服务框架 consumer: 调用方 provider: 被调用方 一个接口一般都会充当两个角色(不是同时充当) ...
- 为什么Dapr是比SpringCloud和Istio更优雅的微服务框架?
Dapr 是微软主导的云原生开源项目,2019年10月首次发布,到正式发布 V1.0 版本的不到一年的时间内,github star 数达到了 1.2万(现在已经超过1.7万星),超过同期的 kube ...
- 【微服务】使用spring cloud搭建微服务框架,整理学习资料
写在前面 使用spring cloud搭建微服务框架,是我最近最主要的工作之一,一开始我使用bubbo加zookeeper制作了一个基于dubbo的微服务框架,然后被架构师否了,架构师曰:此物过时.随 ...
- SpringCloud升级之路2020.0.x版-2.微服务框架需要考虑的问题
本系列为之前系列的整理重启版,随着项目的发展以及项目中的使用,之前系列里面很多东西发生了变化,并且还有一些东西之前系列并没有提到,所以重启这个系列重新整理下,欢迎各位留言交流,谢谢!~ 上图中演示了一 ...
- 微服务框架surging学习之路——序列化
1.对微服务的理解 之前看到在群里的朋友门都在讨论微服务,看到他们的讨论,我也有了一些自己的理解,所谓微服务就是系统里的每个服务都 可以自由组合.自由组合这个就很厉害了,这样一来,每个服务与服务之间基 ...
- 微服务框架surging学习之路——序列化 (转载https://www.cnblogs.com/alangur/p/10407727.html)
微服务框架surging学习之路——序列化 1.对微服务的理解 之前看到在群里的朋友门都在讨论微服务,看到他们的讨论,我也有了一些自己的理解,所谓微服务就是系统里的每个服务都 可以自由组合.自由组 ...
- SpringCloud微服务实战——搭建企业级开发框架(四十四):【微服务监控告警实现方式一】使用Actuator + Spring Boot Admin实现简单的微服务监控告警系统
业务系统正常运行的稳定性十分重要,作为SpringBoot的四大核心之一,Actuator让你时刻探知SpringBoot服务运行状态信息,是保障系统正常运行必不可少的组件. spring-b ...
- 利用 nodeJS 搭建一个简单的Web服务器(转)
下面的代码演示如何利用 nodeJS 搭建一个简单的Web服务器: 1. 文件 WebServer.js: //-------------------------------------------- ...
- 简单Spring Cloud 微服务框架搭建
微服务是现在比较流行的技术,对于程序猿而言,了解并搭建一个基本的微服务框架是很有必要滴. 微服务包含的内容非常多,一般小伙伴们可以根据自己的需求不断添加各种组件.框架. 一般情况下,基本的微服务框架包 ...
随机推荐
- Android app启动activity并调用onCreate()方法时都默默地干了什么?
Android app启动activity并调用onCreate() 方法时都默默地干了什么? 在AndroidManifest.xml文件中的<intent-filter>元素中有这 ...
- InlineModelAdmin
参考博客:https://www.cnblogs.com/linxiyue/p/4074562.html
- Kubernetes的系统架构与设计理念
Kubernetes与云原生应用简介 随着Docker技术的发展和广泛流行,云原生应用和容器调度管理系统也成为IT领域大热的词汇.事实上,云原生应用的思想,在Docker技术火爆之前,已经由云计算技术 ...
- Android基础知识之拼写检查框架
原文:http://android.eoe.cn/topic/android_sdk 拼写检查器框架 Android平台提供了一个拼写检查器的框架,来方便你在你的应用中实现并使用拼写检查.这个框架是A ...
- jmeter经验----java 读取文件中文乱码问题
很少写java io的代码,今天整了一个发现 本地调试好好的,放到jmeter里就打印乱码.一番折腾,终于搞定~直接上代码: List<Order> orderList = new Arr ...
- mac安装GnuGP
1.首先查看gnugp最新的稳定版: localhost:~ jack$ brew search gnupg ==> Formulae gnupg gnupg-pkcs11-scd gnupg@ ...
- Python 父目录获取
# coding=utf-8import os currentPath = os.getcwd() # 当前目录parent_path = os.path.dirname(currentPath) # ...
- .net4.5使用async和await异步编程实例
关于异步编程的简单理解: 在.NET4.5中新增了异步编程的新特性async和await,使得异步编程更为简单.通过特性可以将这项复杂的工作交给编译器来完成了.之前传统的方式来实现异步编程较为复杂,这 ...
- spark读取hdfs数据本地性异常【转】
在分布式计算中,为了提高计算速度,数据本地性是其中重要的一环. 不过有时候它同样也会带来一些问题. 一.问题描述 在分布式计算中,大多数情况下要做到移动计算而非移动数据,所以数据本地性尤其重要,因此我 ...
- js判断网页是真静态还是伪静态的方法
现在很多网站都是使用以“.html“为扩展名的网页,这样做的好处是有利于搜索引擎收录和排名,或者是其他目的,如可以缩短网址使人容易记住.不过有意思的是并不是所有的以“.html”扩展名的网页都是静态的 ...