eureka专题
一 .什么是eureka?
英文翻译过来就是 “找到了,发现了”,所以定义为(服务发现框架),其中Eureka包含两个组件:Eureka Server和Eureka Client。
Eureka Server :服务发现中心,注册中心.
Eureka Client :服务提供者.
二 .eureka应用场景
一般应用于微服务架构之间服务的调用.使用之简单,调用其他服务就像调用本地服务一样的轻松.所以很受青睐,
三 .开始我们的表演吧
3.1 .首先我们搭建springboot项目,引入一下依赖,cloud版本为Finchley.SR1,springboot版本为2.0.4.
因为我搭建的是聚合项目,所以父项目打包方式为pom
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<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.2 搭建我们的服务端
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
yml配置文件如下
spring:
application:
name: eureka-server
security:
user:
name: ccc
password: 123
server:
port: 8888
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/
#实例是否在eureka服务器上注册自己的信息以提供其他服务发现,默认为true
register-with-eureka: false
#此客户端是否获取eureka服务器注册表上的注册信息,默认为true
fetch-registry: false
server:
#开启自我保护模式
enable-self-preservation: false
#清理无效节点,默认60*1000毫秒,即60秒
eviction-interval-timer-in-ms: 50000
因为我们开启了服务端认证,只有拥有用户名和密码的服务才能注册,因此增加一个配置,不然服务无法注册.
/**
* @Description 在地址里加上用户名密码,然后运行还是报错,
* 是因为新版本的security默认开启csrf了,关掉就好了
* @ClassName WebSecurityConfig
* @Date 2019/7/6 19:16
* @VERSION 1.0
**/
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable(); //关闭csrf
http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); //开启认证
}
}
看一下我们的启动类
@SpringBootApplication
@EnableEurekaServer
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}
完美,服务端已经搭建完成.此时运行一下吧,访问地址如下:
登陆之后
当然现在是没有任何实例的,下面就开始搭建客户端,一个注册到eureka作为服务提供方,一个作为本地服务.
3.3 搭建我们的第一个客户端(注册使用)
老规矩,引入一下依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
yml配置文件:
server:
port: 9999
eureka:
instance:
ip-address: 127.0.0.1 #服务名称的修改
prefer-ip-address: true #访问信息提示 IP 信息
#可以不用配置,都有默认值
# #eureka客户端需要多长时间发送心跳给eureka服务器,表明他仍然或者,默认30秒
# lease-renewal-interval-in-seconds: 5
# #eureka服务器在接受到实力的最后一次发出的心跳后,需要等待多久才可以将此实力删除 默认90秒
# lease-expiration-duration-in-seconds: 10
client:
#表示eureka client间隔多久去拉取服务器注册信息,默认为30秒
registry-fetch-interval-seconds: 30
service-url:
defaultZone: http://ccc:123@localhost:8888/eureka/
#添加对该服务的描述信息
info:
app.name: client
company.name: 127.0.0.1
build.artifactId: eureka-client
build.version: 0.0.1-SNAPSHOT
#eureka 日志级别
logging:
level:
com.netflix: INFO
spring:
application:
name: ylt-test
启动类如下
@SpringBootApplication
@EnableDiscoveryClient //加上这个注解才可以被eureka服务发现并注册
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}
下面我们就启动它
YLT-TEST,就是我们注册是服务.已经成功注册了.
3.4 搭建我们的第二个客户端,调用第一个服务使用
引入以下依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
yml配置如下:
server:
port: 10000
eureka:
client:
#实例是否在eureka服务器上注册自己的信息以提供其他服务发现,默认为true
register-with-eureka: false
service-url:
defaultZone: http://ccc:123@localhost:8888/eureka/ # 指定注册中心的url
spring:
application:
name: local-client
启动类看一下,因为我们多加了一个注解,
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients //服务调用方开启服务调用 不然发现不了服务
public class LocalApplication {
public static void main(String[] args) {
SpringApplication.run(LocalApplication.class, args);
}
}
完美,启动它吧
在看一下,服务端图形化界面:
符合预期,调用方没有被注册到eureka.
下面我们就开始测试调用.
3.5 测试调用服务
编写服务提供方接口,如下,普通的rest api
@RestController
public class TestController {
@GetMapping("/hello")
public String getHello() {
return "Hello World,成功调用了注册中心的服务...";
}
}
编写服务调用方接口,如下
@RestController
public class TestController {
@Resource
private IService iService;
@RequestMapping("/hi")
public String getHi() {
return iService.getHi();
}
}
接口
@FeignClient(name = "ylt-test")//注册到eureka上的服务名称
public interface IService {
@GetMapping("/hello")//被调用服务的接口地址
String getHi(); //传参类型和返回值一定要与被调用方一致
}
完事了,调用我们本地的接口测试一下,
果然,服务提供方提供了服务,是不是很简单.本节完.