SpringCloud-Eureka 学习
一,概述
(我们约定 E表示springcloud_eureka项目,P代表springcloud_eureka_provider项目,C代表springcloud-consumer项目)
SpringCloud Eureka本质是一个服务注册和发现的工具包,P和C是注册在E上的instances,在E来到人世间之前,P,C的耦合性很强的,现在呢,自从有了E之后,他们只需要注册到E上,C只需要依赖E就可以了.大大降低了他们之间的耦合性.
二,实战
1,注册中心项目
- 项目名称:springcloud_eureka
- 端口:8761
- pom文件中的依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
- application.properties
server.port=8761
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
- 程序入口
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class SpringcloudEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudEurekaApplication.class, args);
}
}
- 访问地址
http://localhost:8761/
2,服务提供者项目
- 项目名称:springcloud_eureka_provider
- 端口:8762
- pom文件中的依赖
<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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- application.properties
#应用(服务)名称
spring.application.name=springcloud_eureka_provider
eureka.instance.hostname=localhost
server.port=8762
eureka.client.register-with-eureka=true
#注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
- 程序入口
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
//激活eureka中的DiscoveryClient实现
@EnableDiscoveryClient
@EnableEurekaClient
@SpringBootApplication
public class SpringcloudEurekaProviderApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudEurekaProviderApplication.class, args);
}
}
- 相关代码
import com.yomob.springcloud.provider.entity.User;
import com.yomob.springcloud.provider.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/{id}")
private User findById(@PathVariable Long id) {
User user = userService.findUserById(id);
return user;
}
}
- 当再次访问该地址的时候会有如下的变化:(最开始的时候Application下面没有任何的instances)
http://localhost:8761/
3,消费者项目
- 项目名称:springcloud_consumer
- 端口:8763
- pom文件中的依赖
<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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
</dependencies>
- application.properties
#应用(服务)名称
spring.application.name=springcloud-consumer
eureka.instance.hostname=localhost
server.port=8763
eureka.client.register-with-eureka=true
#注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
- 程序入口
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class SpringcloudConsumerApplication {
//因为在spring cloud中,服务之间的调用是通过rest来实现的,等下要使用restTemplate来调用服务提供者提供的服务,提供一个RestTemplate的Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(SpringcloudConsumerApplication.class, args);
}
}
- 相关代码
package com.yomob.springcloud.controller;
import com.yomob.springcloud.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
import java.util.List;
/**
* @author Wengang Wang
* @since May 21, 2018
*/
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
private static final String SERVICE_NAME = "springcloud_eureka_provider";
@GetMapping("/{id}")
public User findById(@PathVariable Long id) {
return findByIdService(id);
}
public String client() {
String request_url = "";
for (String serviceTemp : discoveryClient.getServices()) {
if (SERVICE_NAME.equalsIgnoreCase(serviceTemp)) {
List<ServiceInstance> serviceInstanceList = discoveryClient.getInstances(serviceTemp);
for (ServiceInstance serviceInstance : serviceInstanceList) {
URI uri = serviceInstance.getUri();
request_url = uri.toString() + "/user/";
}
}
}
return request_url;
}
public User findByIdService(Long id) {
String request_url = client() + id;
return restTemplate.getForObject(request_url, User.class);
}
}
- 当再次访问该地址的时候会有如下的变化:(当启动提供服务项目之后Application下面只有SPRINGCLOUD_EUREKA_PROVIDER 这一个instances),目前有两个instances
http://localhost:8761/