文章目录
服务的注册与治理(eureka)
1.创建注册中心
1.1 创建工程
新建一个maven父项目 一个model工程作为服务注册中心,即Eureka Server,另一个作为Eureka provider。
下面以server创建为例子
–>右键工程–>创建model–>选择 spring initialir(如果没有这个选项的参考idea中安装spring initializr)如图·
然后一直下一步 选择cloud discovery->eureka server–>一直下一步
\
完成后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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jange</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server</name>
<description>eureka-server</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<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>
1.2启动类配置
只需要一个注解@EnableEurekaServer,这个注解需要在springboot工程的启动application类上加@EnableEurekaServer 这个注解行了如图
1.3 appication.yml配置
eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳(因此可以在内存中完成),在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。eureka server的配置文件appication.yml:
server:
port: 9000
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#正式环境注销
server:
enable-self-preservation: false #是关闭自我保护模式,默认为true。
eviction-interval-timer-in-ms: 4000 #清理无效节点的时间间隔,默认
通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server.
1.4启动测试
eureka server 是有界面的,启动工程,打开浏览器访问:
http://localhost:9000 ,界面如下:
2.创建一个服务提供者 (service-provide1)
2.1创建工程
创建过程同server类似注意选择eureka discovery,创建完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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.janhe</groupId>
<artifactId>priveder</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>priveder</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<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>
2.2核心类配置
在启动类加上@EurekaClient注解 //表明自己是服务的提供者
2.3 application.yaml配置
server:
port: 8010
spring:
application:
name: service-provider
eureka:
client:
serviceUrl:
defaultZone: http://localhost:9000/eureka/ #注册中心
healthcheck:
enabled: true # 开启健康检查(需要spring-boot-starter-actuator依赖)
instance:
lease-expiration-duration-in-seconds: 30 # 续约到期时间(默认90秒)
lease-renewal-interval-in-seconds: 10 # 续约更新
2.3 注册服务
package com.janhe.priveder.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @Auther: Jan
* @Date: 2019-4-1 09:28
* @Description:
*/
@RestController
public class HelloController {
@Value("${server.port}")
String port;
@RequestMapping("/hello")
public String home(@RequestParam String name) {
return "hi "+name+",port:" +port;
}
}
2.4 测试
启动provider项目
最后访问http://localhost:9000 ,界面如下:
如图服务已经注册到注册中心了