Eureka简介
Eureka是一种基于REST(Representational State Thransfer表现层状态转移)的服务,主要用于AWS的定位服务,以便实现中间层服务器的负载均衡和故障转移。
单个注册中心整合
pom配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
application.yml
spring:
application:
name: eureka-server
server:
port: 7000 # Server HTTP port.
eureka:
instance:
hostname: localhost # eureka实例的hostname
client:
register-with-eureka: false # 指示当前实例是否应在eureka服务器上注册其信息以供发现
fetch-registry: false # 指示当前实例是否从eureka服务器获取注册表信息
service-url: # 映射map。映射关于eureka服务器的url列表,map的value值可以是单个url,也可以是以,分割符分割的多个url,修改之后生效时间将在下一个循环中,由eurekaServiceUrlPollIntervalSeconds指定
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
注解@EnableEurekaServer
@EnableEurekaServer // Annotation to activate Eureka Server related configuration. 激活eureka相关配置的注解
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
打包部署运行
mvn clean package -Dmaven.test.skip=true
java -jar xxx.jar
访问 localhost:7000
注册两个注册中心
修改application.yaml配置文件
## 新增两个配置文件
## application-peer1.yml
spring:
application:
name: eureka-server
server:
port: 7001 # Server HTTP port.
eureka:
instance:
hostname: localhost # eureka实例的hostname
client:
register-with-eureka: true # 指示当前实例是否应在eureka服务器上注册其信息以供发现
fetch-registry: true # 指示当前实例是否从eureka服务器获取注册表信息
service-url: # 映射map。映射关于eureka服务器的url列表,map的value值可以是单个url,也可以是以,分割符分割的多个url,修改之后生效时间将在下一个循环中,由eurekaServiceUrlPollIntervalSeconds指定
defaultZone: http://127.0.0.1:7002/eureka/
## application-peer2.yml
spring:
application:
name: eureka-server
server:
port: 7002 # Server HTTP port.
eureka:
instance:
hostname: localhost # eureka实例的hostname
client:
register-with-eureka: true # 指示当前实例是否应在eureka服务器上注册其信息以供发现
fetch-registry: true # 指示当前实例是否从eureka服务器获取注册表信息
service-url: # 映射map。映射关于eureka服务器的url列表,map的value值可以是单个url,也可以是以,分割符分割的多个url,修改之后生效时间将在下一个循环中,由eurekaServiceUrlPollIntervalSeconds指定
defaultZone: http://127.0.0.1:7001/eureka/
打包部署
// 打包
mvn clean package -Dmaven.test.skip=true
// 运行
java -jar xxx.jar --spring.profiles.active=peer1
java -jar xxx.jpg --spring.profiles.active=peer2
// 访问
localhost:7001
localhost:7002
服务提供和调用
生成者
pom
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.67</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
application.yml
spring:
application:
name: eureka-producer
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7000/eureka/
server:
port: 8000
具体方法
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(@RequestBody JSONObject requestJSON) {
String name = requestJSON.getString("name");
return "hello, " + name + " " + new Date();
}
}
打包部署测试
mvn clean package -Dmaven.test.skip=true
java -jar xxx.jar
curl -H'Content-Type: application/json' -d'{"name":"huskyui"}' localhost:8000/hello
hello, huskyui Fri Mar 20 16:06:40 CST 2020
消费者
pom
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.67</version>
</dependency>
<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>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
application.yml
spring:
application:
name: eureka-consumer
eureka:
client:
service-url:
defaultZone: http://localhost:7000/eureka/
server:
port: 9000
添加@EnableFeignClients
@EnableFeignClients
@SpringBootApplication
public class CloudEurekaConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(CloudEurekaConsumerApplication.class, args);
}
}
具体代码
@FeignClient(name = "eureka-producer")
public interface HelloRemote {
@RequestMapping("/hello")
String hello(@RequestBody JSONObject requestJSON);
}
@RestController
public class HelloController {
@Autowired
private HelloRemote helloRemote;
@RequestMapping("/hello/{name}")
public String index(@PathVariable("name")String name){
JSONObject requestJSon = new JSONObject();
requestJSon.put("name",name);
return helloRemote.hello(requestJSon);
}
}
打包部署测试
mvn clean package -Dmaven.test.skip=true
java -jar xxx.jar
curl localhost:9000/hello/huskyui
hello, huskyui Fri Mar 20 16:11:59 CST 2020
负载均衡
启动两个不同端口的生产者,启动一个消费者会实现相关效果