spring cloud 系列学习

1 注册中心(eureka)

1.1 注册中心

1.1.1 pom文件

1 <dependency>
2     <groupId>org.springframework.cloud</groupId>
3     <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
4 </dependency>

 1.1.2 启动类,添加@EnableEurekaServer

1 @SpringBootApplication
2 @EnableEurekaServer
3 public class EurekaServerApplication {
4 
5     public static void main(String[] args) {
6         SpringApplication.run(EurekaServerApplication.class, args);
7     }
8 }

 1.1.3 配置文件(application.yml)

 1 server:
 2   port: 9761
 3 
 4 eureka:
 5   instance:
 6     hostname: localhost
 7   client:
 8     registerWithEureka: false
 9     fetchRegistry: false
10     serviceUrl:
11       defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

1.2 服务

1.2.1 pom.xml

1 <dependency>
2     <groupId>org.springframework.cloud</groupId>
3     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
4 </dependency>

1.2.2 启动类,添加@EnableEurekaClient

1 @EnableEurekaClient
2 @SpringBootApplication
3 public class OrderServiceApplication {
4 
5     public static void main(String[] args) {
6         SpringApplication.run(OrderServiceApplication.class, args);
7     }
8 }

 1.2.3 配置文件(application.yml)

1 eureka:
2   client:
3     serviceUrl:
4       defaultZone: http://localhost:9761/eureka/
5 spring:
6   application:
7     name: order-service

2 配置中心

2.1 配置中心

2.1.1 pom.xml

1 <dependency>
2     <groupId>org.springframework.cloud</groupId>
3     <artifactId>spring-cloud-config-server</artifactId>
4 </dependency>

2.1.2 启动类,添加@EnableConfigServer

1 @EnableConfigServer
2 @SpringBootApplication
3 public class ConfigServerApplication {
4 
5     public static void main(String[] args) {
6         SpringApplication.run(ConfigServerApplication.class, args);
7     }
8 }

2.1.3 application.yml

1 spring:
2   cloud:
3     config:
4       server:
5         git:
6           uri: https://github.com/...... # git地址 
7           username: username # git用户名
8           password: password # git密码
9           search-paths: path # 配置文件位置

2.2 客户端

2.2.1 pom.xml

1 <dependency>
2     <groupId>org.springframework.cloud</groupId>
3     <artifactId>spring-cloud-starter-config</artifactId>
4 </dependency>

2.2.2 配置文件(bootstrap.yml)

1 spring:
2   cloud:
3     config:
4       discovery:
5         uri: http://localhost:8769 #服务地址
6       profile: dev
7       label: master

http请求地址和资源文件映射如下:,可参考

   /{application}/{profile}[/{label}]

   /{application}-{profile}.yml

   /{label}/{application}-{profile}.yml

   /{application}-{profile}.properties

   /{label}/{application}-{profile}.properties

3 网关

3.1 网关

3.1.1 pom.xml

1 <dependency>
2     <groupId>org.springframework.cloud</groupId>
3     <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
4 </dependency>

3.1.2 启动类添加@EnableZuulProxy

1 @EnableZuulProxy
2 @SpringBootApplication
3 public class GatewayServiceApplication {
4     public static void main(String[] args) {
5         SpringApplication.run(GatewayServiceApplication.class, args);
6     }
7 }

3.1.3 配置文件(applicationyml)

 1  zuul:
 2    host:
 3      connect-timeout-millis: 80000
 4      socket-timeout-millis: 80000
 5    ribbon:
 6      eager-load:
 7        enabled: true
 8    routes:
 9      user-service:
10        path: /user/** # http请求拦截路径
11        serviceId: user-service # 在eureka注册的服务名
12        sensitiveHeaders:
13 
14 eureka:
15   client:
16     serviceUrl:
17       defaultZone: http://localhost:9761/eureka/
18 
19 spring:
20   application:
21     name: gateway-server

4 swagger

4.1 服务端

4.1.1 pom.xml

1 <dependency>
2     <groupId>io.springfox</groupId>
3     <artifactId>springfox-swagger2</artifactId>
4 </dependency>
5 <dependency>
6     <groupId>io.springfox</groupId>
7     <artifactId>springfox-swagger-ui</artifactId>
8 </dependency>

4.1.2 启动类添加@EnableSwagger2

1 @EnableSwagger2
2 @SpringBootApplication
3 public class GatewayServiceApplication {
4     public static void main(String[] args) {
5         SpringApplication.run(GatewayServiceApplication.class, args);
6     }
7 }

4.1.3 添加类

 1 @Component
 2 @Primary
 3 public class DocumentationConfig implements SwaggerResourcesProvider {
 4 
 5     @Override
 6     public List<SwaggerResource> get() {
 7         List<SwaggerResource> resources = new ArrayList<>();
 8         resources.add(swaggerResource("用户系统", "/user/v2/api-docs", "2.0"));
 9         resources.add(swaggerResource("订单系统", "/order/v2/api-docs", "2.0"));
10         return resources;
11     }
12 
13     private SwaggerResource swaggerResource(String name, String location, String version) {
14         SwaggerResource swaggerResource = new SwaggerResource();
15         swaggerResource.setName(name);
16         swaggerResource.setLocation(location);
17         swaggerResource.setSwaggerVersion(version);
18         return swaggerResource;
19     }
20 }

4.2 客户端

4.2.1 pom.xml

1 <dependency>
2     <groupId>io.springfox</groupId>
3     <artifactId>springfox-swagger2</artifactId>
4 </dependency>
5 <dependency>
6     <groupId>io.springfox</groupId>
7     <artifactId>springfox-swagger-ui</artifactId>
8 </dependency>

4.2.2 在相应类上添加注解

- @Api()用于类; 
表示标识这个类是swagger的资源 
- @ApiOperation()用于方法; 
表示一个http请求的操作 
- @ApiParam()用于方法,参数,字段说明; 
表示对参数的添加元数据(说明或是否必填等) 
- @ApiModel()用于类 
表示对类进行说明,用于参数用实体类接收 
- @ApiModelProperty()用于方法,字段 
表示对model属性的说明或者数据操作更改 
- @ApiIgnore()用于类,方法,方法参数 
表示这个方法或者类被忽略 
- @ApiImplicitParam() 用于方法 
表示单独的请求参数 
- @ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam

 

posted @ 2018-09-12 16:35  songchuanrang  阅读(159)  评论(0)    收藏  举报