从今天开始我们讲解 springcloud 专题,前部分我们讲解各组件的使用,后部分讲解各组件的原理及源码。此篇我们分析eureka的使用流程。
首先我们知道eureka 是微服务的注册中心,分为服务端和客户端,服务端通常有一个或多个eureka 服务,客户端一般为多个不同或相同功能的微服务。下面我们用idea工具演示一下服务端和客户端的配置。
1、先创建一个springboot 项目 ,一种方式如下图:
右键总目录名
点击Module,然后依次操作,然后继续点击next
进入下一步,填好项目相关信息,然后点击next
进入Maven 配置环境
继续下一步,可以根据需求修改一些信息
2、,新建后在pom 文件中加一下几个重要依赖,包含一些依赖版本信息 如下:
<!-- springboot 版本信息!-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/>
</parent>
<!-- springcloud 版本信息 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<!-- eureka 服务端依赖配置,重点!-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- 用来管理jar包版本,如果后面的jar包没有申明版本,会以这里面的版本为主,此处并不会引入jar包 -->
<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>
3、创建springboot 启动类:
package com.navinfo.nandao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/** springboot启动注解*/
@SpringBootApplication
/** eureka 服务端启动注解 */
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
4、在resources 中创建配置文件 application.properties 简单配置内容如下:
#该eureka 服务端的端口号
server.port=8763
#该eureka 服务端的ip地址
eureka.instance.hostname=localhost
#是否注册eureka
eureka.client.register-with-eureka=false
#是否从eureka中拉取信息
eureka.client.fetch-registry=false
#暴露eureka 服务地址
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
#自我保护模式,当出现出现网络分区、eureka在短时间内丢失过多客户端时,会进入自我保护模式,
# 即一个服务长时间没有发送心跳,eureka也不会将其删除,默认为true
eureka.server.enable-self-preservation=true
#eureka server清理无效节点的时间间隔,默认60000毫秒,即60秒
eureka.server.eviction-interval-timer-in-ms=60
5、然后启动服务:
6、成功后访问 http://localhost:8763/ 显示如下:目前还没有客户端注册,所以eureka客户端列表为空
7、现在我们创建eureka 客户端,客户端创建端创建类似,几处不同的地方我这里详解一下:
pom 配置不同:
<!-- eureka 客户端jar包引入(不引人服务端jar) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 选择性配置,此配置为了一个微服务通过不同端口号 配置多个服务 -->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.navinfo.nandao.OrderApplication</mainClass>
</configuration>
</plugin>
</plugins>
启动类不同:
package com.navinfo.nandao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
/** eureka 客户端 注解配置,重点 */
@EnableEurekaClient
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
}
}
配置文件不同:
#微服务项目名称
spring.application.name=nandao-order
#微服务项目端口号
server.port=8084
#此客户端连接的eureka 服务端地址
eureka.client.service-url.defaultZone=http\://localhost\:8763/eureka/
8、大概三处不同,配置后 启动该微服务,启动后刷新 http://localhost:8763/ 显示:有一个微服务客户端注册来了
9、添加多个窗口,启动多个微服务比如8085、8086,启动后刷新 http://localhost:8763/ 显示
打包以及启动服务的步骤:
a、
b、
c、输入打包命令:mvn package -Dmaven.test.skip=true 回车,打好后的包在target目录
d、运行打好包,cd target/ 输入启动命令 java -jar 包名 --server.port=8085 回车,第二个微服务就启动了......................
10、eureka 注册中心作用 已经解析完,在创建一个微服务进行接口调用,同理,简单流程如下:
package com.navinfo.nandao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
public class NandaoWebApplication {
@Bean
@LoadBalanced //接口调用时起到负载均衡作用
RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(NandaoWebApplication.class,args);
}
}
conroller 控制层
package com.navinfo.nandao.controller;
import com.navinfo.nandao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/queryUser")
public String queryUser() {
return userService.queryContents();
}
}
server 接口实现层:
package com.navinfo.nandao.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.concurrent.atomic.AtomicInteger;
@Slf4j
@Service
@Scope(proxyMode = ScopedProxyMode.INTERFACES)
public class UserServiceImpl implements UserService {
private AtomicInteger s = new AtomicInteger();
private AtomicInteger f = new AtomicInteger();
public static String SERVIER_NAME = "nandao-order";
@Autowired
private RestTemplate restTemplate;
@Override
public String queryContents() {
s.incrementAndGet();
String results = restTemplate.getForObject("http://"
+ SERVIER_NAME + "/queryUser", String.class);
return results;
}
}
另外一个客户端代码如下:
package com.navinfo.nandao.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
public class UserController {
@RequestMapping("/queryUser")
public String queryUser() {
log.info("========micro-order===queryUser");
return "========micro-order===queryUser";
}
}
该微服务有多客户端 8084、8085、8086,其他接口过来时通过负载均衡算法随机选择一个服务。小伙伴可以自己模拟一下,印象会更深刻。下一篇我们 继续深入讲解eureka 使用和原理,敬请期待!