MicroService implementation techniques:
With springBoot to create a single service, with SpringCloud to manage these micro-services.
## SpringCloud five animal
# 1. Register / service discovery --Netflix Eureka
management server and ip address of
# 2. Kefuduan load balancing --Netflix Ribbon \ Feign
distribution service request
# 3. Breaker --Netflix Hystrix
for faulty service for processing
# 4. Services Gateway --Netflix Zuul
unified micro-entry services.
# 5. Distributed Configuration --Spring Cloud Config
configuration file to do the same for micro-management services
SpringCloud entry step-outs
1. Registration Center
address used to manage micro services
1.1 When the micro registration service can solve registry (IP, port, service name)
1.2 registration service from each micro-micro services Address List
1.3 Use heartbeat mechanism micro service contract: every N seconds to send a request to the registry, told the registry,
I'm still alive, if the micro-services linked with heart failure contract,
Registry micro address list service, the other service will be updated address list
2. Users of micro management services
3. Order Management microService
1. Registration Center Configuration
1.1 to create a common parent module maven project,
Configuration Utility jar package management in pom.xml
<properties> <!--编码--> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <!-- 1.管理SpringBoot的jar包 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> </parent> <!-- 2.管理spring-cloud的jar包 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- 测试 --> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
1.2 Creating Registry - Project name: eureka-server-1000
Create a spring-boot program in the parent module, also maven project,
But to select quickstart
Add 1.3 pom.xml dependency in eureka-server-1000 module
<parent> <artifactId>spring-cloud-parent</artifactId> <groupId>spring-cloud-parent</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>eureka-server-1000</artifactId> <name>eureka-server-1000</name> < The Dependencies > <-! Register / service discovery dependence with it, in order to obtain the ip, port, all micro-service service name with it, other services in order to obtain all the micro-micro-services ip, port, service name list -> < dependency > < groupId > org.springframework.cloud </ groupId > < artifactId > the Spring-Cloud-Starter-Netflix-Eureka-Server </ artifactId > </ dependency > <-! springboot-Web environmental dependence -> < dependency > < the groupId > ORG.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
1.4 resources to build a configuration folder,
Application.yml build a configuration file in the folder
Add yml file
Server:
Port: 1000 # port number
Eureka:
instance:
hostname: localhost # Host
Client:
registerWithEureka: false # prohibit the registration center to register themselves
fetchRegistry: false # prohibit the registration center to pull the registered address list
serviceUrl: # micro-service registered with the registry address
defaultZone: http: // localhost: 1000 / eureka /
Add 1.5 build a configuration classes in java
/ ** * @EnableEurekaServer * registry start off by default * / @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main (String [] args) { SpringApplication.run (EurekaServerApplication. Class , args); } }
Start main ways to see the page
Enter on the page http: // localhost: 1000 / following page configuration was successful
2 .. User Management Micro Services
Module name: order-server-2000
And create a registration module configured the same but not the same
2.1 add dependencies in pom.xml
<parent> <artifactId>spring-cloud-parent</artifactId> <groupId>spring-cloud-parent</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>order-server-2000</artifactId> <name>order-server-2000</name> <dependencies> <!-- 注册的微服务控制器 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- springboot的web环境配置 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
2.2在resources配置文件夹中新建个application.yml
在yml中添加配置
eureka:
client:
serviceUrl: #指定注册中心的地址
defaultZone: http://localhost:1000/eureka/
instance: #是否显示ip地址
prefer-ip-address: true
server: #端口号
port: 2000
spring:
application: #服务名
name: orders-server
cloud:
discovery:
client:
simple:
local:
service-id: orders-server:2000 #显示的id
2.3在java文件夹中建个
OrdersServerApplication.java
添加如下代码
@SpringBootApplication @RestController public class OrdersServerApplication { @RequestMapping("/") public String home() { return "Hello world"; } public static void main(String[] args) { SpringApplication.run(OrdersServerApplication.class); } }
启动main方法可以看见页面
在网页上输入http://localhost:2000/页面显示
Hello world
说明配置成功
再重启动注册模块的main方法
在网页上输入http://localhost:1000/页面显示如下就说明微服务配置成功
3.订单管理微服务
项目名:user-server-3000
和创建注册模块一样但配置不一样
3.1在pom.xml中添加依赖
<parent> <artifactId>spring-cloud-parent</artifactId> <groupId>spring-cloud-parent</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>user-server-3000</artifactId> <name>user-server-3000</name> <dependencies> <!-- 注册的微服务控制器 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- springboot的web环境配置 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
3.2在resources配置文件夹中新建个application.yml
在yml中添加配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:1000/eureka/
instance:
prefer-ip-address: true
server:
port: 3000
spring:
application:
name: user-server
cloud:
discovery:
client:
simple:
local:
service-id: user-server:3000
3.3在java文件夹中建个
OrdersServerApplication.java
添加如下代码
/** * @EnableEurekaClient * 启动微服务; * 默认开启 */ @SpringBootApplication @RestController @EnableEurekaClient public class UserServerApplication { @RequestMapping("/") public String home() { return "Hello world2"; } public static void main(String[] args) { SpringApplication.run(UserServerApplication.class); } }
启动main方法可以看见页面
在网页上输入http://localhost:3000/页面显示
Hello world2
说明配置成功
再重启动注册模块的main方法
在网页上输入http://localhost:1000/页面显示如下就说明微服务配置成功