搭建Eureka Server
1-创建工程 eureka_server子模块
2-导入坐标
eureka_server的pom.xml
<?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">
<parent>
<artifactId>spring_cloud_demo</artifactId>
<groupId>cn.test</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka_server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
3-配置application.yml
server:
port: 9000 #端口
#配置eureka server
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false #是否将自己注册到注册中心
fetch-registry: false #是否从eureka中获取注册信息
service-url: #配置暴露给Eureka Client的请求地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
4-配置启动类
eureka server中新建包cn.itcast.eureka,新建启动类EurekaServerApplication
package cn.itcast.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //激活eureka server
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
启动
将服务提供者注册到注册中心eureka server上
1-引入EurekaClient的坐标
修改product_service子工程的pom.xml
添加依赖
<!--引入EurekaClient-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2-修改application.yml,添加Eureka Server的信息
修改product_service子工程的resources中的application.yml,添加
#配置Eureka
eureka:
client:
service-url:
defaultZone: http://localhost:9000/eureka/
instance:
prefer-ip-address: true #使用ip地址注册
3-修改启动类,添加服务发现的支持(可选)
修改product_service子工程的启动类ProductApplication
package cn.itcast.product;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
@SpringBootApplication
@EntityScan("cn.itcast.product.entity") //用来扫描和发现指定包及其子包中的Entity定义
//激活EurekaClient @EnableEurekaClient == @EnableDiscoveryClient == 不写也行
public class ProductApplication {
public static void main(String[] args) {
SpringApplication.run(ProductApplication.class,args);
}
}
启动
验证:
服务消费者通过注册中心获取服务列表并调用
Eureka中的元数据:服务的主机名,ip,等信息.可以通过eureka server进行获取,用于服务之间的调用.
1-引入EurekaClient的坐标
修改order_service子工程的pom.xml,同上
2-修改application.yml,添加Eureka Server的信息
修改order_service子工程的resources中的application.yml,添加...同上
3-修改启动类,添加服务发现的支持(可选)
修改order_service子工程的启动类OrderApplication\
4-修改order_service的子模块的controller类OrderController
调用DiscoveryClient方法
package cn.itcast.order.controller;
import cn.itcast.order.entity.Product;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@RequestMapping("/order")
public class OrderController {
//注入restTemplate
@Autowired
private RestTemplate restTemplate;
@Autowired
//import org.springframework.cloud.client.discovery.DiscoveryClient;
/**
* 注入DiscoveryClient:springcloud提供的获取元数据的工具类,调用方法获取服务的元数据信息
*/
private DiscoveryClient discoveryClient;
/**
*
* @param id 商品id
* @return
* 通过订单系统,调用商品服务根据id查询商品信息
* 1-需要配置商品服务
* 2-需要调用商品服务
*/
// @RequestMapping(value = "/buy/{id}",method = RequestMethod.GET)
// public Product findById(@PathVariable Long id){
// Product product = null;
// //如何调用远程服务?
// product = restTemplate.getForObject("http://127.0.0.1:9001/product/1",Product.class);
// return product;
// }
@RequestMapping(value = "/buy/{id}",method = RequestMethod.GET)
public Product findById(@PathVariable Long id){
//已调用服务名称获取所有的元数据
List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
//获取唯一的一个元数据
ServiceInstance instance = instances.get(0);
Product product = null;
//根据元数据的主机地址和端口号拼接请求微服务的URL
product = restTemplate.getForObject("http://"+instance.getHost()+":"+instance.getPort()+"/product/1",Product.class);
return product;
}
}
5-启动OrderApplication
此时,不满足高可用性
来源:CSDN
作者:level_Tiller
链接:https://blog.csdn.net/level_Tiller/article/details/103487222