利用SpringCloud搭建一个最简单的微服务框架
利用SpringCloud搭建一个最简单的微服务框架
1.微服务
微服务主要包含服务注册,服务发现,服务路由,服务配置,服务熔断,服务降级等一系列的服务,而Spring Cloud为我们提供了个一整套的服务;
本例子为你提供了最简单的一个服务发现例子,包含服务注册发现spingCloudEurekaServer、服务配置中心spingCloudConfServer、以及一个app应用springCloudApp
2.服务注册与发现
spingCloudEurekaServer
pom.xml
[html] view plain copy
4.0.0 com.caicongyang spingCloudEurekaServer 0.0.1-SNAPSHOT org.springframework.cloud spring-cloud-starter-parent Angel.SR6 org.springframework.cloud spring-cloud-starter-eureka-server org.springframework.boot spring-boot-maven-plugin
Application.java
[java] view plain copy
- 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替代)
[plain] view plain copy
- 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
[html] view plain copy
- <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">
4.0.0 com.caicongyang spingCloudConfServer 0.0.1-SNAPSHOT org.springframework.cloud spring-cloud-starter-parent Angel.SR6 org.springframework.cloud spring-cloud-config-server org.springframework.cloud spring-cloud-starter-eureka org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin compile
application.java
[java] view plain copy
- 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
[plain] view plain copy
- 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
[html] view plain copy
4.0.0 com.caicongyang springCloudApp 0.0.1-SNAPSHOT org.springframework.cloud spring-cloud-starter-parent Angel.SR6 UTF-8 1.7 UTF-8 2.2.2 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-starter-hystrix org.springframework.boot spring-boot-starter-test test io.springfox springfox-swagger2 ${springfox.swagger.version} io.springfox springfox-swagger-ui ${springfox.swagger.version} spingcould org.apache.maven.plugins maven-compiler-plugin ${java.version} ${java.version} ${java.encoding} true
Application.java
[java] view plain copy
- 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
[java] view plain copy
- 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
[plain] view plain copy
- 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
还没有评论,来说两句吧...