4.1 微服务注册中心
4.1.1注册中心的主要作用
4.1.2 常见的注册中心
4.2 Eureka概述
4.2.1Eureka组成
4.2.2 Eureka的交互流程与原理
Eureka使用流程
4.3使用Eureka步骤
4.3.1搭建eureka server
1 创建工程
2 导入pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring_cloud_demo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>product_service</module>
<module>order_service</module>
<module>eureka_server</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<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-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
1.3 配置application.properties
server.port=9000
eureka.instance.hostname=localhost
#是否将自己注册到注册中心
eureka.client.register-with-eureka=false
#是否从eureka中获取注册信息
eureka.client.fetch-registry=false
#配置暴露给Eureka Client请求的地址
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
1.4 配置启动类
package xx.study.sc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class);
}
}
4.3.2创建Eureka生产者
1 引入EurekaClient依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
2修改application.properties 添加EurekaServer信息
server.port=9001
#服务名称
spring.application.name=service-product
#注册中心访问地址
eureka.client.service-url.defaultZone=http://localhost:9000/eureka/
#使用ip地址注册
eureka.instance.prefer-ip-address=true
3修改启动类 添加服务发现的支持(可选)
package xx.study.sc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
//激活EurekaClient 最新版本可以不写
@EnableEurekaClient
public class ProductApplication {
public static void main(String[] args) {
SpringApplication.run(ProductApplication.class);
}
}
4产品
package xx.study.sc.controller;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/product")
public class ProductController {
@RequestMapping(value = "/{name}",method = RequestMethod.GET)
public String say(@PathVariable String name){
String returnVal="欢迎"+name+"学习Spring Cloud!!!";
return returnVal;
}
@RequestMapping(value = "/buy",method = RequestMethod.GET)
public String buy(@RequestParam String name){
String returnVal=name+Math.random();
return returnVal;
}
}
4.3.3创建Eureka消费者
Eureka中的元数据:服务的主机名,ip等信息,可以通过EurekaServer进行获取,用于服务之间的调用。
1 引入EurekaClient依赖(同生产者)
2修改application.properties 添加EurekaServer信息(同生产者)
3实现消费
package xx.study.sc.controller;
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.*;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@RequestMapping("/order")
public class OrderController {
//注入
@Autowired
private RestTemplate restTemplate;
/**
*注入DiscoverClient
* Springcloud提供获取元数据的工具类
*/
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping(value = "/buyApple",method = RequestMethod.GET)
public String buyApple(@RequestParam String name){
//调用服务名称的所有元数据
List<ServiceInstance> instances=discoveryClient.getInstances("service-product");
/*for (ServiceInstance instance : instances) {
System.out.println(instance);
}*/
//获取唯一的元数据
ServiceInstance instance=instances.get(0);
name=restTemplate.getForObject("http://"+instance.getHost()+":"+instance.getPort()+"/product/buy?name= "+name,String.class);
String returnVal="从注册中心收到"+name+"!!!";
return returnVal;
}
}