标签:步骤 tar 注册表 elf 工具类 标准 load 一致性 enable
1.创建一个子模块:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
server:
port: 9000 #端口
#配置eureka server
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false #是否将自己注册到注册中心(默认为true)
fetch-registry: false #是否要从eureka获取注册信息
service-url: #配置暴露给eureka Client的请求地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
@SpringBootApplication //激活eurekaserver @EnableEurekaServer public class EurekaServiceApplication { public static void main(String[] args) { SpringApplication.run(EurekaServiceApplication.class,args); } }
启动服务并访问localhost:9000:
<!--引入eurekaClient--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
eureka:
client:
service-url:
defaultZone: http://localhost:9000/eureka/
instance:
prefer-ip-address: true #使用IP地址注册
操作步骤同上
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 { /** * @param id 商品id * @return 通过订单服务调用商品服务根据id查询商品信息 * 1.需要配置商品对象 * 2.需要调用商品服务 */ //注入RestTemplate对象 @Autowired RestTemplate restTemplate; @RequestMapping(value = "/buy/{id}", method = RequestMethod.GET) public Product findById(@PathVariable Long id) { // Product product = productService.findProductById(id); //如何调用商品服务 /** * 1.使用java中的urlconnection * 2.httpclient * 3.okhttp * 4.spring的RestTemplate */ Product product = restTemplate.getForObject("http://localhost:9001/product/findById/1", Product.class); return product; } /** * 注入DiscoveryClient * springCloud提供的获取元数据的工具类 * 调用方法获取元数据信息 */ @Autowired private DiscoveryClient discoveryClient; @RequestMapping(value = "/buybuy/{id}", method = RequestMethod.GET) public Product findById2(@PathVariable Long id) { // 调用discorveryClient方法 //获取到所有元数据 List<ServiceInstance> instances = discoveryClient.getInstances("service-product"); // for (ServiceInstance instance:instances){ // System.out.println(instance); // } // 获取唯一一个元数据 ServiceInstance instance = instances.get(0); // Product product = restTemplate.getForObject("http://localhost:9001/product/findById/1", Product.class); // 根据元数据中的主机地址和端口号拼接请求商品微服务(prodect_service)的url Product product = restTemplate.getForObject("http://" + instance.getHost() + ":" + instance.getPort() + "/product/findById/1", Product.class); return product; } }
翻译:
紧急!尤里卡可能是错误地声称实例已经启动,而实际上它们还没有启动。续费低于阈值,因此为了安全起见,实例不会过期。
标签:步骤 tar 注册表 elf 工具类 标准 load 一致性 enable
原文地址:https://www.cnblogs.com/luzhanshi/p/13568831.html