基于Spring Boot构建Spring Cloud微服务架构

一、Spring Cloud简介:

Spring Cloud是对Netflix的多个开源组件进一步的封装而成,同时又实现了和云端平台,和SpringBoot开发框架很好的集成。
Spring Cloud为开发者提供了在分布式系统(配置管理,服务发现,熔断,路由,微代理,控制总线,一次性token,全居琐,leader选举,分布式session,集群状态)中快速构建的工具,使用SpringCloud的开发者可以快速的启动服务或构建应用、同时能够快速和云平台资源进行对接。

二、创建服务端

参考Spring Inuti搭建Spring Boot项目 搭建基础Boot项目

在pom.xml中添加依赖:

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
  <dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>Brixton.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
	</dependency>
</dependencies>

在Application启动文件中添加注解:@EnableEurekaServer

@EnableEurekaServer
@SpringBootApplication
public class SpringCloudApplication {

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

在配置文件application.properties中添加配置:

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

点击输入图片说明启动程序 启动工程后,访问:http://localhost:1228/ (注意要有最后的反斜杠) 输入图片说明

三、创建提供服务的客户端

参考Spring Inuti搭建Spring Boot项目 搭建基础Boot项目

在pom.xml中添加依赖:

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
  <dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>Brixton.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
	</dependency>
</dependencies>

实现接口提供API支持

/**
 * Created by 从小就坏 on 2016/12/28.
 */

@RestController
public class HelenController {
    private final Logger logger = Logger.getLogger(getClass());

    @Autowired
    private DiscoveryClient client;

    @RequestMapping(value = "/add" ,method = RequestMethod.GET)
    public String add() {
        ServiceInstance instance = client.getLocalServiceInstance();
        logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
        return "HELEN";
    }
}

在Application启动文件中添加注解:@EnableEurekaServer

@EnableEurekaServer
@SpringBootApplication
public class SpringCloudApplication {

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

在配置文件application.properties中添加配置:

spring.application.name=compute-service
server.port=1229
eureka.client.serviceUrl.defaultZone=http://localhost:1228/eureka/

启动2个工程你会发现你的服务注册好了

转载于:https://my.oschina.net/u/1189098/blog/813930

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值