天天看点

SpringCloud 入门1(服务注册和服务发现)

开发工具 Spring Tool Suite™ (STS):3.9.5 (或者 使用eclipse自己安装sts插件)

1. 注册中心

  在微服务架构中,由于服务粒度一般很小,服务众多,因此有效管理服务就显得非常重要,所以,第一步我们先来写一个简单的注册中心,这里采用Spring Cloud Netflix的Eureka ,eureka是一个服务注册和发现模块。

新建maven工程,在sts中,file->new->Spring Starter Project

SpringCloud 入门1(服务注册和服务发现)

 点击 Next,如下选择Eureka Server 

SpringCloud 入门1(服务注册和服务发现)

将application.properties 修改为application.yml

SpringCloud 入门1(服务注册和服务发现)

填写配置文件application.yml

server:
  port: 8000

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      
spring:
  application:
    name: eurekaServer
           

在启动类上添加注解@EnableEurekaServer

package com.gray;

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

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

启动工程:访问:http://localhost:8000/

SpringCloud 入门1(服务注册和服务发现)

一个简单的注册中心就完成了

2. 微服务-编写服务提供者(eurekaClient)

  STS新建maven工程,在选择支持界面,选择Eureka Discorvery,同时选择web和Rest支持,由于我们要提供接口

SpringCloud 入门1(服务注册和服务发现)

建好后引入的jar包如下:

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-rest</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<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-actuator</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
           

配置文件:

eureka:
  client:
    healthcheck:
      enabled: true   #健康检查
    serviceUrl:
      defaultZone: http://localhost:8000/eureka/ #注册中心地址
server:
  port: 8001
spring:
  application:
    name: eureka-client
  cloud:
    config:
      discovery: 
        enabled: true
        serviceId: eureka-server     #指定服务id
           

主运行类添加@EnableEurekaClient

package com.gray;

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


@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {

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

编写一个controller提供一个测试接口

package com.gray.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {


	@Value("${server.port}")
	String port;
	
	@RequestMapping("/hello")
	public String home(@RequestParam String name) {
		return "hello "+name+",port:" +port;
	}
}
           

运行客户端项目

在注册中心界面,看到新注册的实例:

SpringCloud 入门1(服务注册和服务发现)

调用接口:http://localhost:8001/hello?name=gray  页面返回成功

SpringCloud 入门1(服务注册和服务发现)
目录:

SpringCloud 入门1(服务注册和服务发现)

SpringCloud 入门2(服务消费)

继续阅读