首页 技术 正文
技术 2022年11月9日
0 收藏 435 点赞 4,878 浏览 4842 个字

Eureka集群主要有三个部分Eureka服务器,服务提供者,服务调用者

简单的来说就是服务提供者将服务注册到Eureka服务器,服务调用者对其服务进行查找调用。

Eureka服务程序的搭建可参考官方文档:http://cloud.spring.io/spring-cloud-static/Dalston.SR5/multi/multi_spring-cloud.html

Eureka架构图:

搭建一个简单的Eureka程序

使用IDEA分模块搭建三个工程Eureka服务器(server),服务提供者(police),服务调用者(person)

一.搭建服务器

1.引入maven依赖,使用官方文档中的依赖的结果还是启动不起来,缺少日志相关的依赖,另外自己添加了几个依赖后就OK了

 <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>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
<version>1.7.10</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency> <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>

因为Spring cloud集成了很多项目。所以引入spring-cloud-starter-eureka-server就相当于引入了spring-boot-starter-web等,就具有了web容器的功能了。

2.配置yml文件:设置服务器端口以及相关信息(在文档中都可以找到相关配置)

server:
port: 8761 #更改端口为8761
eureka:
client:
register-with-eureka: false #服务器不用注册到其他服务器
fetch-registry: false #服务器不用去服务器抓取注册信息

3.编写服务启动类,也就是main方法启动spring boot,注意使用@EnableEurekaServer注解

package com.nijunyang;import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication
@EnableEurekaServer
public class ServerApp {
public static void main(String[] args){
new SpringApplicationBuilder(ServerApp.class).web(true).run(args);
}
}

启动之后访问http://localhost:8761/就可以看到Eureka服务器控制台。

搭建一个简单的Eureka程序

二.服务提供者(警察局)

1.maven依赖将服务器的spring-cloud-starter-eureka-server依赖改为spring-cloud-starter-eureka即可

2.yml配置文件:需要将服务提供者注册到Eureka服务器上,服务器的端口设置的8761

server:
port: 8080
spring:
application:
name: first-police #服务提供者名字
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #注册到服务器

3.编写一个实体类police和PoliceController,有人报警则派出一个警察

package com.nijunyang;import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;@RestController
public class PoliceController {
@RequestMapping(value = "/call/{id}",method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public Police call(@PathVariable Integer id){
Police police = new Police();
police.setId(id);
police.setName("zhangsan");
return police;
}
}

4.该模块的启动类,main方法启动和服务器的一样  new SpringApplicationBuilder(XXX(启动类类名).class).web(true).run(args);

三.服务调用者(报警)

1.maveny依赖:在服务提供者的基础上再加入负载均衡(后续了解)相关的依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>

2.yml配置:同样需要注册到服务器,上面的服务提供者由于没有设置端口所以默认是8080,现在将调用者端口设置8081,否则启动会出错

server:
port: 8081 #更改端口为8081
spring:
application:
name: first-person
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #注册到服务器

3.编写PersonController,需要新加@Configuration注解,以及配置RestTemplate,RestTemplate本来是spring-web下面的类用来调用REST服务。本身不具备调用分布式服务的能力,但是被@LoadBalanced修饰后就具有访问分布式服务的能力了(具体涉及负载均衡,后续深入了解)。因为是注册到Eureka服务器的,所以我在内部请求的时候只需要转到相应的服务提供者就可以了:http://first-police/xxx

package com.nijunyang;import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;@Controller
@Configuration
public class PersonController {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
@GetMapping(value = "/call/{id}")
@ResponseBody
public String call(@PathVariable Integer id){
RestTemplate restTemplate = getRestTemplate();
return restTemplate.getForObject("http://first-police/call/" + id ,String.class);
}
}

4.不多说,启动类编写

依次启动服务,服务提提供者和服务调用者。访问Eureka控制台就可以看到注册进去的first-police和first-person搭建一个简单的Eureka程序

访问http://localhost:8081/call/id,页面就会返回某个police的josn信息

搭建一个简单的Eureka程序

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918