idea之springCloud微服务创建
一、创建注册中心
(1)创建名为Eureka的Maven工程
file→New→Project→Maven→Next→Next→Finish
(2)导入pom
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.springCloud.cn</groupId> <artifactId>Eureka</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>Eureka Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.5.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-eureka-server</artifactId> </dependency> </dependencies> <build> <finalName>Eureka</finalName> </build> </project>(3)配置application.yml
在src/main/resources文件夹下,新建文件application.yml
配置如下:
server: port: 8081 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/(4)创建application类
在src/main下创建cloud/service文件夹并创建EurekaApplication.java文件
内容为:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaApplicaion { public static void main(String[] args) { SpringApplication.run(EurekaApplicaion.class, args); } }
(5)具体结构如下
二、创建服务A
(1)创建名为service1的服务端A
过程略。。。
(2)导入pom
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.springCloud.cn</groupId> <artifactId>service1</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>service1 Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.5.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> <build> <finalName>service1</finalName> </build> </project>
(3)配置application.yml文件
在src/main/resources文件夹下,新建文件application.yml
配置如下:
spring: application: name: service eureka: client: serviceUrl: defaultZone: http://localhost:8081/eureka/ server: port: 8082
(4)创建application类
在src/main下创建cloud/service文件夹并创建ServiceAApplication.java文件
内容为:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableDiscoveryClient @RestController public class ServiceAApplication { @RequestMapping("/service1") public String service2(){ return "这是服务A"; } public static void main(String[] args) { SpringApplication.run(ServiceAApplication.class, args); } }
(5)结构如下:
三、创建服务B
(1)创建名为service2的服务端B
过程略。。。
(2)导入pom
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.springCloud.cn</groupId> <artifactId>service2</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>service2 Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.5.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> <build> <finalName>service2</finalName> </build> </project>(3)配置application.yml文件
在src/main/resources文件夹下,新建文件application.yml
配置如下:
spring: application: name: service2 eureka: client: serviceUrl: defaultZone: http://localhost:8081/eureka/ server: port: 8083(4)创建application类
在src/main下创建cloud/service文件夹并创建ServiceBApplication.java文件
内容为:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableDiscoveryClient @RestController public class ServiceBApplication { @RequestMapping("/service2") public String service2(){ return "这是服务B"; } public static void main(String[] args) { SpringApplication.run(ServiceBApplication.class, args); } }(5)结构如下:
四、配置网关
(1)创建名为gateway的服务端B
过程略。。。
(2)导入pom
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.springCloud.cn</groupId> <artifactId>gateway</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>gateway Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.5.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zuul --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> <version>1.4.2.RELEASE</version> </dependency> </dependencies> <build> <finalName>gateway</finalName> </build> </project>
(3)配置application.yml文件
(5)结构如下:
在src/main/resources文件夹下,新建文件application.yml
配置如下:
eureka: client: serviceUrl: defaultZone: http://localhost:8081/eureka/ spring: application: name: gateway server: port: 8084 zuul: routes: service: /service/** service2: /service2/**(4)创建application类
在src/main下创建cloud/service文件夹并创建GatewayApplication.java文件
内容为:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication @EnableZuulProxy @EnableEurekaClient public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } }
五、测试
浏览器依次输入:
http://localhost:8081 http://localhost:8082/service1 http://localhost:8083/service2 http://localhost:8084/service/service1 http://localhost:8084/service2/service2