Eureka
何为 Eureka?
简单来说Eureka 就是一个服务管理中心。
思考:在现实的开发逻辑中,我们把所有的逻辑通通都写到一个工程下,那么我们的项目会成什么样子?成千上万个Java文件都在一个目录下,各种资源文件堆叠在一起,这就像把不同年级的学生都统一到一个超大的教室上课;这就对我们的项目管理造成了很大的困难,甚至是阻碍,那么能不能把逻辑拆分以下,projectA 负责一部逻辑,ProjectB 负责一部分逻辑、projectC ……,然后让各个逻辑之间能够互相调用、通信!但管理有事相互独立的;就像一个企业,有人事部、财务部、研发部、销售部和董事会……
那么,Eureka 就是各个部门相互通信的基站!它负责各个project间的联络与通信!
image代码实现步骤:
- 新建Eureka服务中心
- 新建App1
- 新建App2
目的:使 App1、App2 能相互通信!
1. 创建Eureka服务中心
01 idea 创建一个maven Java项目 EurekaService!导入如下依赖:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
02 创建resources/application.properties 配置文件,并添加如下配置:
#启动端口改为 8761
server.port=8761
# 关闭默认注册到eureka服务中心(该项目本身就是服务中心,无需自己注册自己)
eureka.client.register-with-eureka=false
# 关闭自动抓取服务端,该工程本身就是服务端
eureka.client.fetch-registry=false
03 创建启动类 person.jack.eureka/EurekaService
package person.jack.eureka;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer /**声明该启动是一个Eureka服务端*/
public class EurekaService {
public static void main(String[] args) {
new SpringApplicationBuilder(EurekaService.class).web(true).run(args);
}
}
# 运行main() 方法,启动Eureka服务端
……
浏览器访问:http://localhost:8761
# 进入Eureka 首页
……
# 停止服务
2. 创建Eureka 客户端工程AppOne
01 创建一个maven java 工程AppOne,导入如下依赖:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
02 创建启动类,person.jack/Appone:
package person.jack;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class AppOne {
public static void main(String[] args) {
new SpringApplicationBuilder(AppOne.class).web(true).run(args);
}
}
03 创建person.jack.controller/HelloController:
package person.jack.controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@ResponseBody
@RequestMapping(value = "/sayMesg/{word}")
public String sayMesg(@PathVariable String word){
return "你传了一个:" + word ;
}
}
04 添加配置文件application.properties:
# 应用名称
spring.application.name=AppOne
# eureka服务端地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
05 启动Eureka 服务端EurekaService、启动客户端AppOne
# 观察服务端是否正常启动
……
# 观察AppOne 是否能正常启动
……
06 启动成功后,访问服务端:http://localhost:8761/
Instances currently registered with Eureka
Application | AMIs | Availability Zones | Status |
---|---|---|---|
APPONE | n/a (1) | (1) | UP (1) - LAPTOP-TJDLNJHK:AppOne |
AppOne 成功注册到Eureka服务中心
创建第二个客户端AppTwo
- 新建第二个客户端AppTwo,相关依赖同Appone相同:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
- 创建application.properties:
# 应用名称
spring.application.name=AppTwo
# eureka服务端地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
- pom.xml 新加入依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
- 创建启动类 person.jack/AppTwo
package person.jack;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class AppTwo {
public static void main(String[] args) {
new SpringApplicationBuilder(AppTwo.class).web(true).run(args);
}
}
- 创建配置工具类 person.jack.config/MyConfig,将RestTemplate 配置到IOC容器:
package person.jack.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class MyConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
- 创建 person.jack.controller/AppTwoHello
package person.jack.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class AppTwoHello {
@Autowired
private RestTemplate restTemplate;
@ResponseBody
@RequestMapping("/getAppOneMesg/{mesg}")
public String getAppOneMesg(@PathVariable String mesg){
String strMesg=restTemplate.getForObject("http://APPONE/sayMesg/" + mesg, String.class);
return "访问App2,从App1 收到信息:“"+strMesg+"“";
}
}
- AppTwo 代码编写完成!依次启动EurekaService、AppOne、AppTwo
# 注意观察服务是否成功启动
调用测试
- 访问Eureka 服务中心: http://localhost:8761/
页面打印
Instances currently registered with Eureka
Application | AMIs | Availability Zones | Status |
---|---|---|---|
APPONE | n/a (1) | (1) | UP (1) - LAPTOP-TJDLNJHK:AppOne |
APPTWO | n/a (1) | (1) | UP (1) - LAPTOP-TJDLNJHK:AppTwo:8081 |
现在,Eureka服务中心已经有了两个应用了!
- 访问 http://localhost:8081/getAppOneMesg/hello ,测试是否能连通AppOne
# 页面打印
访问App2,从App1 收到信息:“你传了一个:hello“
# 访问成功!AppTwo 成功连通、并调用AppOne
测试成功!第一个Euraka 环境搭建完成!
网友评论