抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

本Demo演示了Eureka服务端的搭建,以及消费者如何通过Eureka服务注册中心调用其他供应者提供的接口。
源码:https://github.com/seepine/spring-cloud-demo

模块介绍

spring-cloud-demo-01-eureka模块分为三个子模块,分别为Eureka-Server(Eureka服务端)、Eureka-Provider(供应者)和Eureka-Consumer(消费者)。

一、Eureka服务注册与发现

1.Eureka-Server

1.1 添加依赖

1
2
3
4
5
6
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>

1.2 在启动类上添加注解@EnableEurekaServer

1
2
3
4
5
6
7
@EnableEurekaServer
@SpringBootApplication
public class SpringCloudDemo01EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudDemo01EurekaServerApplication.class, args);
}
}

1.3 添加配置文件

1
2
3
4
5
6
7
8
9
10
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false #该实例是否向EurekaServer注册自己,默认true
fetch-registry: false #该实例是否向Eureka服务器获取所有的注册信息表,默认true
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #配置 Eureka-Server 地址

2.Eureka-Provider

1.1 添加依赖

1
2
3
4
5
6
7
8
9
10
<dependencies>
<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>
</dependencies>

1.2 添加配置文件

1
2
3
4
5
6
7
8
9
10
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false #该实例是否向EurekaServer注册自己,默认true
fetch-registry: false #该实例是否向Eureka服务器获取所有的注册信息表,默认true
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #配置 Eureka-Server 地址

启动Demo

  • 1.启动Eureka-Server
  • 2.启动Eureka-Provider
  • 3.启动Eureka-Consumer

结果检验

打开http://localhost:8761/,将会看到Eureka的界面以及Eureka-Provider和Eureka-Consumer服务

二、生产者与消费者(RestTemplate)

1.Eureka-Provider

1.1 添加接口

1
2
3
4
5
6
7
8
@RestController
public class ApiController {
@RequestMapping("/hello/{name}")
public String hello(@PathVariable String name) {
System.out.println("I am provider , name :" + name);
return "hello " + name;
}
}

1.2 测试接口

2.Eureka-Consumer

1.1 添加接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@RestController
@AllArgsConstructor
public class ApiController {
private DiscoveryClient discoveryClient;
private RestTemplate restTemplate;

@RequestMapping("/hello/{name}")
public String hello(@PathVariable String name) {
System.out.println("I am consumer , name :" + name);
List<ServiceInstance> instances = discoveryClient.getInstances("provider");
if (!instances.isEmpty()) {
ServiceInstance serviceInstance = instances.get(0);
return restTemplate.getForObject(serviceInstance.getUri().toString() + "/hello/" + name, String.class);
}
return "not find provider";
}
}

1.2 测试接口

同时Eureka-Provider控制台打印出,说明能够通过Eureka-consumer的接口,调用Eureka-Provider的接口

1
I am provider , name :seepine

三、生产者与消费者(Feign)

1.Eureka-Consumer

1.1 添加Feign依赖

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

1.2 启动类添加注解(@EnableFeignClients)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
public class SpringCloudDemo01EurekaConsumerApplication {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}

public static void main(String[] args) {
SpringApplication.run(SpringCloudDemo01EurekaConsumerApplication.class, args);
}

}

1.3 添加FeignService

1
2
3
4
5
6
7
8
package com.seepine.demo.eureka.consumer.feign;

//填写要调用方,即生产者spring.application.name
@FeignClient("provider")
public interface RemoteApiService {
@GetMapping("/hello/{name}")
String sayHello(@PathVariable("name") String name);
}

1.4 添加接口

1
2
3
4
5
6
7
8
9
10
@RestController
@AllArgsConstructor
public class ApiController {
private RemoteApiService remoteApiService;

@RequestMapping("/helloByFeign/{name}")
public String helloByFeign(@PathVariable String name) {
return remoteApiService.sayHello(name);
}
}

1.5 测试接口

同时Eureka-Provider控制台打印出,说明能够通过Eureka-consumer的接口,调用Eureka-Provider的接口

1
I am provider , name :seepine-feign

四、Eureka-Server增加用户验证

1.添加依赖

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

2.添加配置

1
2
3
4
5
spring:
security:
user:
name: seepine
password: 123456

3.打开http://127.0.0.1:8761/验证

4.修改所有Eureka连接url

1
http://seepine:123456@${eureka.instance.hostname}:${server.port}/eureka/

评论