利用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 微服务框架搭建
微服务是现在比较流行的技术,对于程序猿而言,了解并搭建一个基本的微服务框架是很有必要滴. 微服务包含的内容非常多,一般小伙伴们可以根据自己的需求不断添加各种组件.框架. 一般情况下,基本的微服务框架包 ...
随机推荐
- unity, Animator.ResetTrigger
解: 正确的写法应该是:Animator.SetTrigger("unfoldTrigger")Animator.ResetTrigger("unfoldTrigger& ...
- Linux学习笔记--SSH免password登录
须要实现的效果: 有两台server:"192.168.201.236" 和 "192.168.201.237" 须要实现:在server"192.1 ...
- eclipse下SVN subclipse插件
本文目的 让未使用过版本控制器软件或者未使用过subversion软件的人员尽快上手. subversion的使用技巧很多,这里只总结了最小使用集,即主要的基本功能,能够用来应付日常工作. 因此不涉及 ...
- quartz + spring 配置示例
<!-- 配置job定时任务类 --> <bean id="triggerCalculateLecturerProfitJob" class="com. ...
- UVa 10697 - Firemen barracks
题目:已知三点.求到三点距离同样的点. 分析:计算几何.分三类情况讨论: 1.三点共线,不成立. 2.多点重叠,有多组解. 3.是三角形,输出中点. 说明:注意绝对值小于0.05的按0计算:负数的四舍 ...
- (原创)用c++11打造好用的variant
variant类似于union,它能代表定义的多种类型,允许将不同类型的值赋给它.它的具体类型是在初始化赋值时确定.boost中的variant的基本用法: typedef variant<in ...
- 每日英语:Got 5 Minutes? 'Flash Fiction' Catches On
Chinese author Lao Ma has a simple approach to his short stories: In the face of life, everything is ...
- 每日英语:Can Robots Better Spot Terrorists at Airports?
Next to have their jobs automated: airport-security screeners? Aviation and government authorities a ...
- (转)Go和HTTPS
转自:http://studygolang.com/articles/2946 Go和HTTPS 2015-04-30 bigwhite 阅读 5688 次 4 人喜欢 3 条评论 收 ...
- 使用ViewPager和Fragment实现滑动导航
ViewPage是android-support-v4.jar包提供的用于页面滑动的库,android-support-v4.jar是google推荐使用的一个类库,在项目中使用之前,你必须其添加到项 ...