Spring 初探(1) boot,swagger,eureka,hystrix

MiniCTO 2017-10-19

1.创建SpringBoot简单响应请求服务

1.在项目中pom.xml添加

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

2.创建boot启动程序,关键字@SpringBootApplication,其实现main方法,做为进程入口SpringApplication.run(DemoApplication.class,args);

3.创建controller,关键字@RestController,注册为contraller,@RequestMapping(value=&quot;/demo&quot;)为路径映射。

2.集成swagger

1.在项目pom.xml中添加:

<!--swagger集成-->

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger2</artifactId>

<version>2.7.0</version>

</dependency>

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger-ui</artifactId>

<version>2.7.0</version>

</dependency>

2.添加配置类

@Component

@Configuration

@EnableSwagger2

publicclassSwagger2Config{

@Bean

publicDocketcreateRestApi(){

returnnewDocket(DocumentationType.SWAGGER_2)

.select()

.apis(RequestHandlerSelectors.any())

.paths(PathSelectors.any())

.build();

}

}

3.集成eureka项目

1.服务端从网上下载eureka.war,放在tomcat下,启动tomcat后,可通过http:ip:port/eureka进行访问

2.客户端在项目中加入pom.xml中加入

org.springframework.cloudspring-cloud-starter-eureka1.3.1.RELEASE

3.在启动boot中添加@EnableEurekaClient启动eureka客户注册

4.属性文件中需要添加以下内容

#应用名称,若无此项,界面应用上则显示unknown

spring.application.name=demo

eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka/v2/

eureka.instance.status-page-url=http://localhost:${server.port}/swagger-ui.html

4.集成Hystrix项目

1.项目pom.xml添加

org.springframework.cloudspring-cloud-starter-hystrix1.3.1.RELEASE

2.在boot启动器中添加@EnableHystrix注解,以启动hystrix

3.在controller中方法中添加@HystrixCommand(fallbackMethod=&quot;方法名&quot;)其中方法名中方法需要和原方法参数一致

5.集成HystrixDashboard

1.项目pom.xml添加

org.springframework.bootspring-boot-starter-actuatororg.springframework.cloudspring-cloud-starter-hystrix-dashboard

2.boot启动类中添加@EnableHystrixDashboard

3.启动查看EnableHystrixDashboard地址:http://ip:端口/hystrix,其在stream处添加http://ip:端口/hystrix.steam,点击monitor即可查看。

demo,git地址:https://github.com/leaf-it/demo.git

相关推荐

kuihan0 / 0评论 2016-07-30