一.项目需求
拆分微服务
1.account提供账户服务(用户管理员的登录退出)
2.menu提供菜品服务:添加,删除,修改,查询菜品
3.order提供订单服务:添加,删除,处理,查询订单
4.user提供用户服务:添加用户,查询用户,删除用户
分离出服务消费者调用四个服务提供者,服务消费者包含客户端的前端页面和后台接口,还有后台管理系统的前端页面和后台系统。用户管理员直接访问资源都保存在服务消费者中,服务消费者根据具体的需求来调用四个服务提供者的业务逻辑,通过feign实现负载均衡。
四个服务提供者和一个服务消费者都需要在注册中心进行注册,同时可以使用配置中心来对配置文件进行统一集中管理。
创建项目
创建父工程pom.xml添加相关依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.7.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--JDK 9 缺失 jar--> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
二.创建子模块
然后创建中心相关依赖
注册中心pom.xml
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> <version>2.0.2.RELEASE</version> </dependency> </dependencies>
创建application.yml
server: port: 8761 eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ register-with-eureka: false fetch-registry: false
接下来是启动类
package com.redhat; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class,args); } } 测试
创建配置中心
配置依赖pom.xml
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> <version>2.0.2.RELEASE</version> </dependency> </dependencies>
创建配置中心application.yml
server: port: 8762 spring: application: name: configserver profiles: active: native cloud: config: server: native: search-locations: classpath:/shared
创建文件夹
创建文件(order服务者配置文件)
创建启动类
package com.redhat; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class,args); } } 创建新的服务提供者order
1.pom.yml相关依赖
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> <version>2.0.2.RELEASE</version> </dependency> </dependencies>
bootstrap.yml
spring: application: name: order profiles: active: dev cloud: config: uri: http://localhost:8762 fail-fast: true
创建Handler
OrderHandler类
package com.redhat.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/order") public class OrderHandler { @Value("${server.port}") private String port; @GetMapping("/index") public String index(){ return "order的端口"+this.port; } } 启动类OrderApplication
package com.redhat; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class,args); } } 把配置中心注册中心服务提供者三个服务启动起来测试
至此环境搭建完成