Spring Cloud配置(一)服务注册

一、概述:

Spring Cloud是
Spring boot实现的微服务架构开发工具。它为微服务架构中涉及的配置管理、服务治理、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等操作提供了一种简单的开发方式。

二、组件说明:
Eureka 服务治理组件,包含服务注册中心,服务注册与发现机制的实现。(服务治理,服务注册/发现)
Hystrix 容错管理逐渐,实现断路器模式,帮助服务以来中出现的延迟和为故障提供强大的容错能力。(熔断、断路器,容错)
Ribbon 客户端负载 均和的服务调用组件(客户端负载)
Feigin 给予Ribbon和Hystrix的声明式服务调用组件 (声明式服务调用)
Zuul 网关组件,提供智能路由,访问过滤功能
Archaius 外部化配置组件

三、运行流程:
Spring Cloud组件运行:
1、所有请求都统一通过 API 网关(Zuul)来访问内部服务。
2、网关接收到请求后,从注册中心(Eureka)获取可用服务。
3、由 Ribbon 进行均衡负载后,分发到后端的具体实例。
4、微服务之间通过 Feign 进行通信处理业务。
5、Spring Cloud配置服务注册于接收:

如下图:

 

四、具体示例:
创建相关工程:
1)spring_cloud    
2)eureka-server
3)service-producer-A

 

1)创建父工程:spring_cloud    

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.0</modelVersion>

	<groupId>com.sam</groupId>
	<artifactId>spring-cloud</artifactId>
	<packaging>pom</packaging>
	<version>0.0.1</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-cloud.version>Camden.SR7</spring-cloud.version>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.1.RELEASE</version>
		<relativePath/>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>

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

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

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	
</project>

 

2)创建注册中心服务:eureka-server

在以上spring_cloud工程中添加Maven Module模块,名称:eureka-server

pom.xml

<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">
	<parent>
		<artifactId>spring-cloud</artifactId>
		<groupId>com.sam</groupId>
		<version>0.0.1</version>
	</parent>
	<modelVersion>4.0.0</modelVersion>

	<artifactId>eureka-server</artifactId>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
		</dependency>
	</dependencies>

</project>

创建启动类:EurekaServerApplication

package com.sam.eureka.server;

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

/**
 * @ClassName: EurekaServerApplication 
 * @Description: 注册中心服务
 * @author sam 
 * @date 2018年8月10日 下午3:49:33
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
	
	public static void main(String[] args) {
		SpringApplication.run(EurekaServerApplication.class, args);
	}

}

创建配置文件(以后所有配置文件都在main/src/resources目录下创建):application.yml

server:
  port: 8010 #服务端口

eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false #是否将eureka自身作为应用注册到eureka注册中心
    fetch-registry: false #为true时,可以启动,但报异常:Cannot execute request on any known server
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

 

3)创建生产者服务:service-producer-A

在以上spring_cloud工程中添加Maven Module模块,名称:service-producer-A

pom.xml

<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">
	<parent>
		<artifactId>spring-cloud</artifactId>
		<groupId>com.sam</groupId>
		<version>0.0.1</version>
	</parent>
	<modelVersion>4.0.0</modelVersion>
	<artifactId>service-producer-A</artifactId>
</project>

创建启动类及相关接口:ServiceApplicationA

package com.sam.service.producer;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: ServiceApplicationA 
 * @Description: 消费者服务A
 * @author sam 
 * @date 2018年8月10日 下午3:57:56
 */
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ServiceApplicationA {

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

    @Value("${spring.application.name}")
    private String name;
    
    @Value("${server.port}")
    private String port;

    @RequestMapping("/hi")
    public String hi(@RequestParam String id){
        return "hi, " + id + ", " + name + ":" + port;
    }

}

创建配置文件:application.yml

spring:
  application:
    name: service-producer

server:
  port: 8811

eureka:
  client:
    serviceUrl:
          defaultZone: http://localhost:8010/eureka/ #eureka服务注册地址

其中,application.name为服务名称,

好了,至此服务注册配置完成!可以依次启动eureka-server服务、service-producer-A服务,

正常启动后访问eureka注册中心配置的8010端口 

http://localhost:8010 

可以查看服务注册情况,如下图:

可以看到service-producer-a已经成功注册到服务中心!

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值