创建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) .目录

 

posted on 2019-12-17 15:50  朝秦暮楚·  阅读(513)  评论(0编辑  收藏  举报