Eureka服务发现包含两大组件:
一.服务端发现组件(eureka server):也被称为服务注册中心,主要提供服务注册功能。
二.客户端发现组件(eureka client):主要用于处理服务的注册和发现。
为什么选择Eureka?
1.他是Netflix公司开源产品,经历生产环境考验,以及3年时间不短迭代,非常稳定
2.他是springcloud首选推荐的服务注册与发现组件,可以和其他组件无缝对接
3.他和其他组件,如负载均衡组件Ribbon,熔断器组件Hystrix,熔断器监控组件Hystrix Dashboard,熔断器聚合监控组件 Turbine组件以及网关组件Zuul相互配合,很容易实现服务注册与发现,负载均衡,熔断和智能路由等功能。
Eureka服务过程:
首先需要一个服务注册中心Eureka Server,服务提供者Eureka Client向服务注册中心Eureka Server注册,将自己的信息(如服务名和服务的ip地址等)通过Rest API的形式提交给服务注册中心Eureka Server。同样,服务消费者也可以向服务注册中心Eureka server注册,同时,服务消费者获得一份服务注册列表信息,该列表包含所有 向服务注册中心Eureka server注册服务的信息。获取服务注册列表之后服务消费者就知道服务提供者的ip,就可以通过http调度来消费服务提供者的服务。
一,服务端搭建
1. 创建一个基础SpringBoot工程(3种创建方式1.手动创建maven工程并修改相应依赖。2.start.spring.io创建。3.IntelliJ IDEA的spring initializr创建),经常命名为:Spring-server,本文命名为(microservice-springcloud),并 在pom.xml中引入必要的依赖内容:
<dependencies>
<!--增加eureka-server的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.通过@EnableEurekaServer注解启动一个服务注册中心给其他应用进行对话
package com.itheima.microservicespringcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class MicroserviceSpringcloudApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceSpringcloudApplication.class, args);
}
}
3.修改application.peopertises,本文创建application.yml,即修改application.yml:
server:
port: 8761
eureka:
instance: #定义Eureka实例
hostname: localhost #Eureka实例所在的主机名
#eureka默认情况下,把自己当做客户端来注册自己,所以我们要禁用它
client:
register-with-eureka: false #表示是否将自己注册到Eureka Server上,默认为true
fetch-registry: false
4.启动服务,浏览器访问配置的路径:localhost:8761
显示还没有注册任何服务。
二:搭建服务提供者(即客户端)
1. 创建一个基础SpringBoot工程(3种创建方式1.手动创建maven工程并修改相应依赖。2.start.spring.io创建。3.IntelliJ IDEA的spring initializr创建),经常命名为:Spring-client,本文命名为(microservice-client),并 在pom.xml中引入必要的依赖内容:
</dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.创建一个Controller处理接口,注入DiscoveryClient对象:
package com.itheima.microserviceclient.controller;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
private final Logger logger = Logger.getLogger(getClass());
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping(value = "/text", method = RequestMethod.GET)
public String text(){
String services = "Services: " + discoveryClient.getServices();
System.out.println(services);
return services;
//return "HELLO WORD";
}
}
3.在启动项中加入@EnableDiscoveryClient注解,激活eureka中的EnableDiscoveryClient实现
package com.itheima.microserviceclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class MicroserviceClientApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceClientApplication.class, args);
}
}
4.3.修改application.peopertises,本文创建application.yml,即修改application.yml:
server:
port: 8762
eureka:
instance: #定义Eureka实例
hostname: localhost #Eureka实例所在的主机名
prefer-ip-address: true
client:
service-url:
defaultZone:
http://localhost:8761/eureka/
spring:
application:
name: microservice-client #名字
5.启动服务,浏览器访问配置的路径:localhost:8761
可以发现服务已经注册上了。
访问client:http://localhost:8762/text
说明注册成功。
三:搭建服务消费者
https://blog.csdn.net/qq_38289534/article/details/82148192
四:搭建订单服务工程
1.创建一个基础SpringBoot工程(3种创建方式1.手动创建maven工程并修改相应依赖。2.start.spring.io创建。3.IntelliJ IDEA的spring initializr创建),经常命名为:Spring-client,本文命名为(microservice-order),并 在pom.xml中引入必要的依赖内容:
</dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.修改修改application.peopertises,本文创建application.yml,即修改application.yml:
server:
port: 7900
eureka:
instance: #定义Eureka实例
hostname: localhost #Eureka实例所在的主机名
prefer-ip-address: true
client:
service-url:
defaultZone:
http://localhost:8761/eureka/
spring:
application:
name: microservice-order #名字
3.创建实体类order
package com.itheima.microserviceorder.po;
public class Order {
private String id;
private Double price;
private String receiverName;
private String receiverAddress;
private String receiverPhone;
public Order(String id, Double price, String receiverName, String receiverAddress, String receiverPhone) {
super();
this.id = id;
this.price = price;
this.receiverName = receiverName;
this.receiverAddress = receiverAddress;
this.receiverPhone = receiverPhone;
}
public Order() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getReceiverName() {
return receiverName;
}
public void setReceiverName(String receiverName) {
this.receiverName = receiverName;
}
public String getReceiverAddress() {
return receiverAddress;
}
public void setReceiverAddress(String receiverAddress) {
this.receiverAddress = receiverAddress;
}
public String getReceiverPhone() {
return receiverPhone;
}
public void setReceiverPhone(String receiverPhone) {
this.receiverPhone = receiverPhone;
}
@Override
public String toString() {
return "Order [id="+id+",price="+price+",receiverName="+receiverName+",receiverAddredss="+receiverAddress+",receiverPhone="+receiverPhone+"]";
}
}
4.创建orderController
package com.itheima.microserviceorder.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.itheima.microserviceorder.po.Order;
@RestController
public class OrderController {
//通过id查询订单
@GetMapping("/order/{id}")
public String findOrderById(@PathVariable String id) {
Order order = new Order();
order.setId("123");
order.setPrice(23.5);
order.setReceiverName("xiaoqiang");
order.setReceiverAddress("beijin");
order.setReceiverPhone("110110110");
return order.toString();
}
}
5.引导类加入@EnableEurekaClient
6.在client工程引导类加入:
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
7.在client工程创建userController:
package com.itheima.microserviceclient.controller;
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.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class UserController {
@Autowired
private RestTemplate restTemplate;
//根据用户查找订单
@GetMapping("/findOrderByUser/{id}")
public String findOrderByUser(@PathVariable String id) {
//假设用户只有一个订单,订单id为123
int oid = 123;
return this.restTemplate.getForObject("http://localhost:7900/order/1", String.class);
}
}
8.启动服务:
已经注册好,然后可以通过http://localhost:7900/order/1 查询数据
也可以通过client服务访问: