服务治理Spring Cloud Eureka

40次阅读

共计 4646 个字符,预计需要花费 12 分钟才能阅读完成。

1.Spring Cloud Eureka 的作用
(1) 建立 Eureka 服务端 (也称为注册中心)
我使用的 springboot 的版本是 2.0.5.RELEASE 版本
① 添加依赖
在书中引入的是 spring-cloud-eureka-server, 但是当我引入这个依赖的时候,找不到 @EnableEurekeServer
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<!– https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies –>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
② 配置 application-peer1.properties 和 application-peer2.properties 文件
以下是 application-peer1.properties 的配置:
server.port=8001
spring.application.name=eureka-server

#eureka
eureka.instance.hostname=127.0.0.1
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://127.0.0.1:8002/eureka/
eureka.instance.prefer-ip-address=true
以下是 application-peer2.properties 的配置
server.port=8002
spring.application.name=eureka-server

#eureka
eureka.instance.hostname=127.0.0.1
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://127.0.0.1:8001/eureka/
eureka.instance.prefer-ip-address=true
③ 建立启动文件, 添加 @EnableEurekaServer 注解
@ComponentScan(basePackages = “gdut.ff”)
@EnableAutoConfiguration
@EnableEurekaServer
public class Main {
public static void main(String args[]) {
SpringApplication.run(Main.class, args);
}
}

④ 启动,指定使用的配置文件

以上就建立了一个注册中心集群。
⑤ 演示效果

(2) 建立 Eureka 客户端 (也称为服务提供方)
① 添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<!– https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies –>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
② 配置 application.properties
server.port=9001
spring.application.name=hello-service
eureka.client.service-url.defaultZone=http://127.0.0.1:8001/eureka/,http://127.0.0.1:8002/eureka/
eureka.instance.prefer-ip-address=true
③ 提供服务
@RestController
@RequestMapping(“/hello”)
public class HelloController {

private final Logger logger = LoggerFactory.getLogger(HelloController.class);

@Autowired
private DiscoveryClient discoveryClient;

@RequestMapping(value = “/index”,method = RequestMethod.GET)
public String index() {
String serviceId = “hello-service”;
List<ServiceInstance> instance = discoveryClient.getInstances(serviceId);
if(null != instance) {
for(int i = 0;i < instance.size();i++) {
System.out.println(instance.get(i).getHost() + “:” + instance.get(i).getPort());
}
}
return “Hello,world”;
}
}

④ 建立启动文件, 添加 EnableDiscoveryClient 注解
@ComponentScan(basePackages = “gdut.ff”)
@EnableAutoConfiguration
@EnableDiscoveryClient
public class Main {
public static void main(String args[]) {
SpringApplication.run(Main.class, args);
}
}
⑤ 启动,指定使用的端口
启动 Main 文件时,指定 –server.port=xxx
访问 http://ip:port/hello/index
(3) 建立 ribbon-consumer(也称为服务调用方)
① 添加依赖
<dependencies>
<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-ribbon</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<!– https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies –>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
② 配置 application.properties
server.port=7001
spring.application.name=ribbon-consumer
eureka.client.service-url.defaultZone=http://127.0.0.1:8001/eureka/,http://127.0.0.1:8002/eureka/
eureka.instance.prefer-ip-address=true
③ 获取服务
@RestController
public class ConsumerController {

@Autowired
RestTemplate restTemplate;

@RequestMapping(value = “/ribbon-consumer”, method= RequestMethod.GET)
public String helloConsumer() {
return restTemplate.getForEntity(“http://hello-service/hello/index”,String.class).getBody();
}
}
④ 建立启动文件, 添加 EnableDiscoveryClient 注解
@ComponentScan(basePackages = “gdut.ff”)
@EnableAutoConfiguration
@EnableDiscoveryClient
public class Main {

@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}

public static void main(String args[]) {
SpringApplication.run(Main.class, args);
}
}
⑤ 启动
访问 http://ip:port/ribbon-consumery
示例代码

正文完
 0