zoukankan      html  css  js  c++  java
  • SpringCloud入门案例

    创建Eureka注册中心

    (1) .Pom文件

    <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.4.RELEASE</version>
    </parent>

    <!--eureka依赖-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>

    <dependencyManagement>
      <dependencies>
        <!--springCloud依赖-->
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-dependencies</artifactId>
          <version>Greenwich.RELEASE</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>

    (2) .创建配置文件

    spring:
      application:
        name: eureka-server

    server:
      port: 8888

    eureka:
      client:
        register-with-eureka: false
        fetch-registry: false
        service-url:
          defaultZone: http://localhost:8888/eureka

    (3) .创建启动类

    package com.qzy;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

    @SpringBootApplication
    @EnableEurekaServer
    public class StartEurekaService {
        public static void main(String[] args) {
            SpringApplication.run(StartEurekaService.class,args);
        }
    }

    (4) .在网页测试

     

    (5) .目录

     

    创建生产者

    (1) .Pom文件

    <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.4.RELEASE</version>
    </parent>

    <!--eureka依赖-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>

    <dependencyManagement>
      <dependencies>
        <!--springCloud依赖-->
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-dependencies</artifactId>
          <version>Greenwich.RELEASE</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>

    (2) .创建Service接口方法

    package com.qzy.service;

    public interface IDosomeService {
        public String doSome();
    }

    (3) .创建Serviceimpl实现接口方法

    package com.qzy.service;

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    public class IDoSomeServiceImpl implements  IDosomeService {
        @RequestMapping("/doSome")
        @Override
        public String doSome() {
            System.out.println("服务提供者");
            return "eureka";
        }
    }

    (4) .创建配置文件

    spring:
      application:
        name: eureka-provider

    server:
      port: 8886

    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8888/eureka

    (5) .启动类

    package com.qzy.service;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

    @SpringBootApplication
    @EnableEurekaClient
    public class StratProvide {
        public static void main(String[] args) {
            SpringApplication.run(StratProvide.class,args);
        }
    }

    (6).在网页测试

     

    (7).目录

     

    创建消费者

    (1).Pom文件

    <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.4.RELEASE</version>
    </parent>

    <!--eureka依赖-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>

    <dependencyManagement>
      <dependencies>
        <!--springCloud依赖-->
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-dependencies</artifactId>
          <version>Greenwich.RELEASE</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>

    (2).创建Controller

    package com.qzy.controller;

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;

    import javax.annotation.Resource;

    @RestController
    public class ConsumerController {
        @Resource
        private RestTemplate restTemplate;
        @RequestMapping("/doSome")
        public String doSome(){
            System.out.println("ConsumerController");
            return restTemplate.getForObject("http://eureka-provider/doSome",String.class);
        }
    }

    (3).创建配置文件

    spring:
      application:
        name: eureka_consumer

    server:
      port: 8891

    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8888/eureka

    (6) .启动类

    package com.qzy;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    @SpringBootApplication
    @EnableEurekaClient

    public class StartConsumer {
        public static void main(String[] args) {
            SpringApplication.run(StartConsumer.class,args);
        }
        @Bean
        @LoadBalanced
        RestTemplate restTemplate(){
            return  new RestTemplate();
        }

    }

    (7) .目录

     

  • 相关阅读:
    第三方库添加记录
    xcode之语法高亮效果消失解决办法
    将excel记录导入ms sql
    eWebEditor在IE8,IE7下所有按钮无效之解决办法
    关于对数据库中重复记录的操作
    javascript如何取得RadioButtonList的值
    水晶报表分页并自动插入空白行
    如何防止SQL注入
    VC++视频教程下载地址
    如何显示最近过生日的记录
  • 原文地址:https://www.cnblogs.com/qinzhenyu/p/12054955.html
Copyright © 2011-2022 走看看