springcloud最佳實踐系列[4]--項目搭建

springcloud最佳實踐系列[4]--項目搭建

來自專欄 springcloud最佳實踐

一、概述

上一節《springcloud最佳實踐系列[3]--項目整體架構》中我們講解了項目的整體架構這個對接下來的課程很重要,包括項目的一些約定。接下來我們看service-price微服務的搭建。

二、servcie-product微服務

1) 項目結構

此部分就不詳細講解工程結構如下,在IDEA中創建一個maven項目結構如下:

項目結構約定:

api:即微服務rest介面層,和swagger文檔輸出。

service:為業務邏輯服務層

dao:為數據操作層

feign:為調用其他微服務介面層定義

2)servcie根目錄pom.xml文件

<?xml version="1.0" encoding="UTF-8"?><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"> <parent> <artifactId>panda</artifactId> <groupId>com.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>service</artifactId> <packaging>pom</packaging> <modules> <module>service-price</module> <module>service-product</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.BUILD-SNAPSHOT</spring-cloud.version> </properties> <!-- servcie 公共依賴信息 --> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!-- swagger2.0集成 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> <!-- base service --> <dependency> <groupId>com.base</groupId> <artifactId>base-service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</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> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories></project>

servcie-product工程的pom.xml文件,這個文件中兩個重點:

1)集成了上面的pom.xml文件

2)依賴了商品模塊mode-price模塊,文件如下:

<?xml version="1.0" encoding="UTF-8"?><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"> <groupId>com.springcloud</groupId> <artifactId>price</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <modelVersion>4.0.0</modelVersion> <name>service-price</name> <description>商品價格模塊</description> <!-- 繼承父工程 --> <parent> <groupId>com.springcloud</groupId> <artifactId>service</artifactId> <version>1.0-SNAPSHOT</version> </parent> <!-- 工程級別依賴 --> <dependencies> <!-- 商品price依賴 --> <dependency> <groupId>com.model</groupId> <artifactId>model-price</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies></project>

3)bootstrap.yml文件

spring: application: name: service-product profiles: active: dev cloud: config: profile: dev uri: http://127.0.0.1:8888/

cloud.config.uri: 為配置服務的地址

spirng.profles.active: 指定了項目中使用的為application-dev的配置文件

4)application-dev.yml文件

server: #項目的埠地址 port: 17001eureka: client: service-url: #eureka註冊地址 defaultZone: http://127.0.0.1:7777/eureka/ instance: #eureka顯示為IP:PORT 格式配置 #instance-id: ${spring.cloud.client.ipAddress}:${server.port} prefer-ip-address: true

5)啟動類

package com.springcloud.product;import com.base.baseservice.configuration.SwaggerConfiguration;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.openfeign.EnableFeignClients;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Import;@SpringBootApplication@EnableDiscoveryClient//@EnableEurekaClient@EnableFeignClientspublic class ProductApplication { public static void main(String[] args) { SpringApplication.run(ProductApplication.class, args); }}

@SpringBootApplication 啟動註解標記為spirngboot啟動

@EnableDiscoveryClient 開啟註冊發現和@EnableEurekaClient二選一即可。

/* * Copyright 2013-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.springframework.cloud.client.discovery;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import org.springframework.context.annotation.Import;/** * Annotation to enable a DiscoveryClient implementation. * @author Spencer Gibb */@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@Import(EnableDiscoveryClientImportSelector.class)public @interface EnableDiscoveryClient { /** * If true, the ServiceRegistry will automatically register the local server. */ boolean autoRegister() default true;}

@EnableEurekaClient 也可以使用此註解進行服務註冊,如果確定使用了eurake作為註冊服務可以使用這個註解,可以看到這個註解的包為: org.springframework.cloud.netflix.eureka屬於netfilix,eureka下面

/* * Copyright 2013-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.springframework.cloud.netflix.eureka;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * Convenience annotation for clients to enable Eureka discovery configuration * (specifically). Use this (optionally) in case you want discovery and know for sure that * it is Eureka you want. All it does is turn on discovery and let the autoconfiguration * find the eureka classes if they are available (i.e. you need Eureka on the classpath as * well). * * @author Dave Syer * @author Spencer Gibb */@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Inheritedpublic @interface EnableEurekaClient {}

@EnableFeignClients 開啟feign客戶端調用,至此項目主要配置完成。

6) 啟動項目

可以看到swagger的地址映射,和相關的啟動信息。查看eurake可以看到註冊信息。

7) 總結重點

  • eurake註冊的使用: 在啟動類開啟註解
  • pom 文件依賴關係
  • 工程結構

下一講我們將集成swagger2.8.0到項目中。


推薦閱讀:

如何評價華為新開源的ServiceComb微服務框架?

TAG:項目 | SpringBoot | SpringCloud |