Skip to content

如何创建一个服务

halower edited this page Mar 14, 2018 · 2 revisions
  1. 新建一个Maven 项目,如果使用Intellij idea开发的话需要安装Lombok plugin,同时设置 Setting -> Compiler -> Annotation Processors -> Enable annotation processing勾选
  2. 在POM.xml 中添加必要的依赖
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
        </dependency>
         <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.16</version>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  1. 在src/main/resources 目录下添加application.yml 配置文件
spring:
  application:
    name: xxxx # 服务识别名称
server:
  port: xxxx
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:xxxx/eureka/
  instance:
    hostname: xxxx
  1. 编写程序入口
@SpringBootApplication
@EnableEurekaClient
@EnableSwagger2
public class Application {
   public static void main(String[] args) {
       SpringApplication.run(Application.class, args);
   }

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("api扫描包"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档注释")
                .description("接口文档注释说明")
                .termsOfServiceUrl("帮助连接")
                .contact(new Contact("作者", "", "邮箱"))
                .version("1.0")
                .build();
    }
}
Clone this wiki locally