1.创建一个services项目,添加三个子模块client(客户端)、service(服务端)、registry(注册中心)

1.1 创建一个services项目

1.2 添加pom.xml依赖

<?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">
<modelVersion>4.0.</modelVersion>
<packaging>pom</packaging>
<modules>
<module>client</module>
<module>service</module>
<module>registry</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1..RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.micro</groupId>
<artifactId>services</artifactId>
<version>0.0.-SNAPSHOT</version>
<name>services</name>
<description>services</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>1.4..RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>

1.3 添加子模块client(客户端)

1.4 添加子模块service(服务端)

1.5 添加子模块registry(注册中心)

2.搭建registry(注册中心)

2.1 application.yml 配置

server:
port: eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false

2.2 编写启动程序

package com.services.registry;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication
@EnableEurekaServer
public class RegistryApplication { public static void main(String[] args) { SpringApplication.run(RegistryApplication.class);
}
}

3.搭建service(服务端)

3.1 application.yml 配置

server:
port:
spring:
application:
name: service
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8080/eureka/

3.2 编写启动程序

package com.services.service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication
@EnableDiscoveryClient
public class ServiceApplication { public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
} }

3.3 编写服务

package com.services.service;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloWorldController {
@RequestMapping("hello/{name}")
public String hello(@PathVariable String name) {
return name + " say hello";
} }

4.搭建client(客户端)

4.1 application.yml 配置

server:
port:
spring:
application:
name: client
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8080/eureka/
app:
service-url: http://localhost:8081/

4.2 编写启动程序

package com.services.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableDiscoveryClient
public class ClientApplication { public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
} @Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}

4.3 编写service

package com.services.client;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; @Service
public class CallHelloService { @Value("${app.service-url}")
private String appServiceUrl; @Autowired
private RestTemplate restTemplate; public String callHello(String name) {
// 是一个http client
ResponseEntity result = restTemplate.postForEntity(appServiceUrl + "hello/" + name, null, String.class);
return result.getBody().toString();
}
}

4.4 编写controller

package com.services.client;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class CallHelloController { @Autowired
private CallHelloService callHelloService; @GetMapping("hello")
public String hello(String name) {
String result = callHelloService.callHello(name);
return result;
} }

5.启动registry-service-client模块

5.1 registry

http://localhost:8080/

5.2 service

刷新网页 http://localhost:8080/

5.3 client

刷新网页 http://localhost:8080/

client 访问 http://localhost:8082/hello?name=tao

源码地址: https://github.com/80905949/SpringCloud.git

SpringBoot整合Eureka搭建微服务的更多相关文章

  1. 十分钟搭建微服务框架(SpringBoot +Dubbo+Docker+Jenkins源码)

    本文将以原理+实战的方式,首先对“微服务”相关的概念进行知识点扫盲,然后开始手把手教你搭建这一整套的微服务系统. 这套微服务框架能干啥? 这套系统搭建完之后,那可就厉害了: 微服务架构 你的整个应用程 ...

  2. Spring-Boot:Spring Cloud构建微服务架构

    概述: 从上一篇博客<Spring-boot:5分钟整合Dubbo构建分布式服务> 过度到Spring Cloud,我们将开始学习如何使用Spring Cloud 来搭建微服务.继续采用上 ...

  3. 【微服务】使用spring cloud搭建微服务框架,整理学习资料

    写在前面 使用spring cloud搭建微服务框架,是我最近最主要的工作之一,一开始我使用bubbo加zookeeper制作了一个基于dubbo的微服务框架,然后被架构师否了,架构师曰:此物过时.随 ...

  4. 【译文】用Spring Cloud和Docker搭建微服务平台

    by Kenny Bastani Sunday, July 12, 2015 转自:http://www.kennybastani.com/2015/07/spring-cloud-docker-mi ...

  5. spring cloud+dotnet core搭建微服务架构:Api授权认证(六)

    前言 这篇文章拖太久了,因为最近实在太忙了,加上这篇文章也非常长,所以花了不少时间,给大家说句抱歉.好,进入正题.目前的项目基本都是前后端分离了,前端分Web,Ios,Android...,后端也基本 ...

  6. spring cloud+.net core搭建微服务架构:Api授权认证(六)

    前言 这篇文章拖太久了,因为最近实在太忙了,加上这篇文章也非常长,所以花了不少时间,给大家说句抱歉.好,进入正题.目前的项目基本都是前后端分离了,前端分Web,Ios,Android...,后端也基本 ...

  7. Spring Cloud和Docker搭建微服务平台

    用Spring Cloud和Docker搭建微服务平台 This blog series will introduce you to some of the foundational concepts ...

  8. 手把手教你使用spring cloud+dotnet core搭建微服务架构:服务治理(-)

    背景 公司去年开始使用dotnet core开发项目.公司的总体架构采用的是微服务,那时候由于对微服务的理解并不是太深,加上各种组件的不成熟,只是把项目的各个功能通过业务层面拆分,然后通过nginx代 ...

  9. spring cloud+dotnet core搭建微服务架构:服务发现(二)

    前言 上篇文章实际上只讲了服务治理中的服务注册,服务与服务之间如何调用呢?传统的方式,服务A调用服务B,那么服务A访问的是服务B的负载均衡地址,通过负载均衡来指向到服务B的真实地址,上篇文章已经说了这 ...

随机推荐

  1. flask框架下的jinja2模板引擎(1)(模板渲染)

    #转载请留言联系 模板是什么? 在 flask 框架中,视图函数有两个作用:处理业务逻辑和返回响应内容.在大型应用中,把业务逻辑和表现内容放在一起,会增加代码的复杂度和维护成本.模板作用即是承担视图函 ...

  2. JavaScript中模块化工具require.js

    什么是require.js? RequireJS是一个非常小巧的JavaScript模块载入框架,是AMD规范最好的实现者之一.它还同时可以和其他的框架协同工作,使用RequireJS必将使您的前端代 ...

  3. 谈谈CSS性能

    CSS性能优化 1.衡量属性和布局的消耗代价: 2.探索W3C的性能优化新规范: 3.用测试数据判断优化策略. 慎重选择高消耗的样式 1.box-shadows; 2.border-radius; 3 ...

  4. 删除List中指定的元素

    删除List中指定的元素 CopyOnWriteArrayList是ArrayList的一个线程安全的变体实现,即可在多线程并发环境中使用 List<Map<String, Object& ...

  5. C#启动服务

    启动服务的方法有很多种,简单的cmd下dos命名,手动启动,还有C#代码启动. 我们要实现的功能: 判断是否安装 是否启动 启动服务 关闭服务 我封装了有关服务的代码,如下: using System ...

  6. 三、docker学习笔记——安装postgresql

    1.docker pull postgres 2.docker run --name postgres -e POSTGRES_PASSWORD=123456 -p 5432:5432 -d post ...

  7. php集群和分布式理解

    首先架构层次来说: php的集群是指很多台服务器处理同样的工作,指的是硬件上的一般,比如slb负载均衡主要作用是有多台服务器处理同样的工作, php分布式是指多台服务器处理不同的工作,指的是业务上的一 ...

  8. 删除datatable的行后,出现“不能通过已删除的行访问该行的信息”的错误,即DeletedRowInaccessibleException

    删除datatable的行后,出现“不能通过已删除的行访问该行的信息”的错误 =========================================================== 采 ...

  9. hearbeat

    heartbeat介绍: 作用: 通过heartbeat,可以将资源(IP及程序服务等资源)从一台已经故障的计算机快速转移到另一台正常运转的机器上继续提供服务,一般称之为高可用服务.在升级生产应用场景 ...

  10. ZT C,C++表达式求值顺序 裘老的解释。 [问题点数:300分]

    http://bbs.csdn.net/topics/370153775 [置顶] [推荐] C,C++表达式求值顺序 裘老的解释. [问题点数:300分] 最近这问题有从日经变时经的趋势,这里贴出裘 ...