Eureka 注册中心

IDea 中创建 3个模块 

Eureka 注册中心_第1张图片

1、创建 eureka 服务端模块 center

package com.eureka.center;

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

@SpringBootApplication
@EnableEurekaServer
public class CenterApplication {

	public static void main(String[] args) {
		SpringApplication.run(CenterApplication.class, args);
	}

}

application.yml 

spring:
  application:
    name: center

server:
  port: 8761

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://localhost:${server.port}/eureka/

 2、创建 eureka 客户端模块 customer

CustomerApplication 代码

package com.eureka.customer;

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

@SpringBootApplication
@EnableEurekaClient
public class CustomerApplication {

	public static void main(String[] args) {
		SpringApplication.run(CustomerApplication.class, args);
	}

}

创建 CustomerController

package com.eureka.customer.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CustomerController {
    @GetMapping("/get/{id}")
    public String get(@PathVariable String id){
        return "xieong"+id;
    }
}

application.yml 

spring:
  application:
    name: customer

server:
  port: 8763

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

3、创建 eureka 客户端模块 order

package com.eureka.order;

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 OrderApplication {

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

}

创建 OrderController 

package com.eureka.order.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@RestController
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/c/get/{id}")
    public String get(@PathVariable String id) {
        String result = restTemplate.getForObject("http://customer:8763/get/"+id, String.class);
        return result;
    }

}

application.yml 

spring:
  application:
    name: order

server:
  port: 8762

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

分别启动三个模块

Eureka 注册中心_第2张图片 

访问 customer服务

Eureka 注册中心_第3张图片 

order服务 调用 customer服务

Eureka 注册中心_第4张图片

 

你可能感兴趣的:(Java)