1. Eureka 介绍
Eureka的服务发现机制包含了三个角色:服务注册中心、服务提供者和服务消费者。三者的关系如下图:
理解起来其实很简单:
1. 我们可以吧服务注册中心比作安居客中介,服务提供中比作房东的房子,消费者就是我们。
2. 当我们需要租房子时就需要通过安居客平台去查看房源,房东想要把房子租出去则需要在安居客上上架房子。
3. 等我们选中房子后可以跟房东联系就是调用了。
4.注意点就是服务提供者也可以是服务的消费者,不就比如房东想去买一套别墅时也需要通过中介找房子。
其他概念不多说,网上资源一大把,现在直接上代码
2. 代码
先看下本项目的目录架构吧
首先创建这四个maven工程 其中microservice-springcloud 为父工程,其他三个为它的子工程,下面来详细讲述每个maven的相关配置
2.1 搭建maven父工程
我先贴出父工程的POM的相关代码
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.pixiu</groupId>
<artifactId>microservice-springcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>../microservice-eureka-server</module>
<module>../microservice-eureka-user</module>
<module>../microservice-eureka-order</module>
</modules>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<project.reporting.outputEncoding>
UTF-8
</project.reporting.outputEncoding>
<spring-cloud.version>Dalston.SR3</spring-cloud.version>
</properties>
<dependencyManagement>
<!--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>
</project>
创建好父工程后,把的pom中的代码复制过去就可以了
在这个环节需要 特别注意 的有以下几点,笔者我也踩过不少坑在这里就 分享 给大家
1. 第一点springboot的版本,本项目采用的是1.5.6.RELEASE的版本
2. 注意springcloud的版本,本项目用的是Dalston.SR3,这里也特别提醒下springcloud与springboot有相应的匹配表,具体可以百度,资料一大堆,这里就不赘述了。
3. 还要一个地方需要注意下,避免出现幽灵般的BUG<artifactId>microservice-springcloud</artifactId> <version>0.0.1-SNAPSHOT</version>
,建立工程这里最好跟我一致。
最后就一句话,想要不报错,直接copy我的额配置文件就好了
2.2 搭建服务端工程
1.创建注册中心server的maven工程
2.编写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>microservice-springcloud</artifactId>
<groupId>org.pixiu</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>microservice-eureka-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
3.在resource下创建application.yaml,相关代码如下:
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone:
http://${eureka.instance.hostname}:${server.port}/eureka/
server:
enable-self-preservation: false #关闭保护机制,可以将不可用的实例正确删除
4.在项目的引导类上加上注解@EnableEurekaServer,代码如下
package com.pixiu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //注解用于声名标注类是一个EurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class,args);
}
}
2.2 搭建客户端工程
1.创建客户端user的maven工程
2.添加pom依赖
注意删除子maven的pom中的relativePath,不然会出现很可怕的事情哦
<!--注意删除子maven的pom中的relativePath,不然会出现很可怕的事情哦-->
<dependencies>
<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>
3.在resource下创建application.yaml,相关代码如下:
server:
port: 8000 # 指定该Eureka的实例端口
eureka:
instance:
prefer-ip-address: true # 是否显示主机IP
instance-id: ${spring.cloud.client.ipAddress}:${server.port} # 格式“IP:端口号”
client:
service-url:
defaultZone: http://localhost:8761/eureka/ # 指定eureka的服务端地址
spring:
application:
name: microservice-eureka-user #指定应用的名称
4.在项目的引导类上加上注解@EnableEurekaClient,代码如下
package com.pixiu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@RestController
@EnableEurekaClient
public class UserEurekaApplication {
@RequestMapping("/hello")
public String home(){
return "hello world!!!";
}
public static void main(String[] args) {
SpringApplication.run(UserEurekaApplication.class,args);
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
5.分别启用服务端和客户端工程,在浏览器中访问http://localhsot:8761,展示效果如下图