首页 > 其他分享 >springcloud之使用eureka例子

springcloud之使用eureka例子

时间:2022-11-30 18:34:38浏览次数:36  
标签:product service springcloud springframework eureka 例子 import org


搭建Eureka Server

1-创建工程  eureka_server子模块

springcloud之使用eureka例子_元数据

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);
}
}

启动

springcloud之使用eureka例子_spring_02

springcloud之使用eureka例子_元数据_03

将服务提供者注册到注册中心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);
}
}

启动

验证:

springcloud之使用eureka例子_xml_04

服务消费者通过注册中心获取服务列表并调用

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

springcloud之使用eureka例子_spring_05

springcloud之使用eureka例子_xml_06

此时,不满足高可用性

springcloud之使用eureka例子_spring_07

标签:product,service,springcloud,springframework,eureka,例子,import,org
From: https://blog.51cto.com/u_12528551/5900118

相关文章

  • springcloud之使用eureka例子&实现高可用
    1-准备2(.../n)个EurekaServer,需要相互注册  1号server:9000  首先,将eureka_server子模块的resources中的application.yml修改如下server:port:9000#端口#配......
  • springcloud之eureka server启动原理
    /org/springframework/cloud/spring-cloud-netflix-eureka-server/2.1.0.RELEASE/spring-cloud-netflix-eureka-server-2.1.0.RELEASE.jar!/META-INF/spring.factories中 ......
  • springcloud之eureka client的启动流程
    eurekaclien向eurekaserver准备的web接口发送http请求,完成服务注册,获取...org/springframework/cloud/spring-cloud-netflix-eureka-client/2.1.0.RELEASE/spring-cloud......
  • springcloud之负载均衡策略
    Ribbon内置了多种负载均衡策略,内部负责负载均衡的顶级接口为com.netflix.loadbalanced.IRule  服务消费者,application.yml中添加#修改ribbon的负载均衡策略服务名-......
  • SpringCloud之Config分布式配置文件中心
    分布式系统面临的配置问题:微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息......
  • 枚举小例子记录
    1、创建枚举类:packagecom.atguigu.common.constant;publicclassProductConstant{publicenumAttrEnum{ATTR_TYPE_BASE(1,"基本属性"),ATTR_TYPE_SALE......
  • SpringCloud01
    SpringCloud011.认识微服务随着互联网行业的发展,对服务的要求也越来越高,服务架构也从单体架构逐渐演变为现在流行的微服务架构。这些架构之间有怎样的差别呢?1.0.学习目......
  • Mybatis更新小例子记录
    转自:https://blog.csdn.net/mikelv01/article/details/123920873//源码/***根据whereEntity条件,更新记录**@paramentity实体对象(set条件值,可以为null......
  • 搭建EurekaServer注册中心报错:Unable to start web server; nested exception is org.
    错误异常:Unabletostartwebserver;nestedexceptionisorg.springframework.boot.web.server.WebServerException:UnabletostartembeddedTomcat今天搭建springcl......
  • MybatisPlus查询小例子
    @OverridepublicPageUtilsqueryBaseAttrPage(Map<String,Object>params,LongcatelogId){QueryWrapper<AttrEntity>queryWrapper=newQueryWrapper<>();i......