zoukankan      html  css  js  c++  java
  • eureka 创建服务消费者

    1. 创建eureka-consumer子项目

    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">
        <parent>
            <artifactId>spring-cloud-demo</artifactId>
            <groupId>com.imooc</groupId>
            <version>1.0.0-SNAPSHOT</version>
            <relativePath>../../pom.xml</relativePath>
        </parent>
        <modelVersion>4.0.0</modelVersion>
        <packaging>jar</packaging>
        <artifactId>eureka-consumer</artifactId>
        <name>eureka-consumer</name>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
        </dependencies>
    
    </project>
    

    3.创建启动类和Controller

    3.1 创建启动类

    package com.imooc.springcloud;
    
    import org.springframework.boot.WebApplicationType;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    
    @SpringBootApplication
    @EnableDiscoveryClient
    public class EurekaConsumerApplication {
    
        @Bean
        public RestTemplate register(){
            return new RestTemplate();
        }
    
        public static void main(String[] args) {
            new SpringApplicationBuilder(EurekaConsumerApplication.class)
                    .web(WebApplicationType.SERVLET)
                    .run(args);
        }
    }
    

    3.2 创建Controller

    package com.imooc.springcloud;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cloud.client.ServiceInstance;
    import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    @Slf4j
    @RestController
    public class Controller {
    
        @Autowired
        private LoadBalancerClient client;
    
        @Autowired
        private RestTemplate restTemplate;
    
        @GetMapping("/hello")
        public String hello(){
            ServiceInstance instance = client.choose("eureka-client");
    
            if(instance == null){
                return "No available instances";
            }
    
            String target = String.format("http://%s:%s/sayHi",
                    instance.getHost(),
                    instance.getPort());
    
            log.info("uri is {}", target);
    
            return restTemplate.getForObject(target, String.class);
        }
    
        @PostMapping("/hello")
        public Friend helloPost(){
            ServiceInstance instance = client.choose("eureka-client");
    
            if(instance == null){
                return null;
            }
    
            String target = String.format("http://%s:%s/sayHi",
                    instance.getHost(),
                    instance.getPort());
    
            log.info("uri is {}", target);
    
            Friend friend = new Friend();
            friend.setName("Eureka Consumer");
    
            return restTemplate.postForObject(target,friend, Friend.class);
        }
    
    }
    
    

    3.3 Friend 类

    package com.imooc.springcloud;
    
    import lombok.Data;
    
    @Data
    public class Friend {
    
        private String name;
    
        private String port;
    }
    
    

    3.4 配置文件

    spring.application.name=eureka-consumer
    
    server.port=31000
    
    eureka.instance.prefer-ip-address=true
    eureka.client.serviceUrl.defaultZone=http://localhost:20000/eureka/
    

    4.项目eureka-consumer发起调用

  • 相关阅读:
    恐怖的Hibernate和JavaFX Table CallBack!
    Java 设计模式 – Observer 观察者模式
    Jenkins 配置 SpringBoot 自动构建部署
    Android 虹软人脸识别SDK-人脸对比
    Okhttp3 网络请求框架与 Gson
    商贸型企业 Java 收货 + 入库 + 生成付款单
    webupload项目中使用
    JavaFX程序初次运行创建数据库并执行建表SQL
    Java实现ArrayList
    Factory Method模式
  • 原文地址:https://www.cnblogs.com/hardy-wang/p/13936876.html
Copyright © 2011-2022 走看看