spring cloud 实践-1 (服务治理)

spring cloud erueka:主要负责完成微服务框架中服务治理功能。

本章的主要功能有:

  • 构建服务注册中心;
  • 服务注册和服务发现;
  • Eureka的基础框架;
  • eureka的服务治理机制;
  • eureka的配置;
服务治理:

服务治理是微服务最为核心和基础的模块,用来实现微服务的自动注册和发现;

  1. 服务注册:
  2. 服务发现:

spring cloud eureka,使用netfilix eureka来实现服务注册和发现。他既包含了服务端组件,也包含了客户端组件。

eureka服务端:服务注册中心;

eureka客户端:处理服务的注册和发现。

搭建服务注册中心:

依赖包:

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


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


	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.SR1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

启动:

@EnableEurekaServer
@SpringBootApplication
public class FishEurekaApplication {
	public static void main(String[] args) {
		new SpringApplicationBuilder(FishEurekaApplication.class).web(true).run(args);
	}
}

配置:

扫描二维码关注公众号,回复: 2210796 查看本文章
spring.application.name=eureka-server
server.port=1111
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
logging.file=${spring.application.name}.log
注册服务提供者:

依赖:

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.4.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</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-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.SR1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

配置:

spring.application.name=eureka-client
server.port=2001
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

代码:

@EnableDiscoveryClient
@SpringBootApplication
public class FishClientApplication {
	public static void main(String[] args) {
		new SpringApplicationBuilder(FishClientApplication.class).web(true).run(args);
	}
}
高可用注册中心:

eureka服务治理中,所有的节点及时服务提供方,也是服务消费方。eureka高可用注册中心,其实就是将自己作为服务向其他服务注册自己。形成一组互相注册的服务注册中心。

服务发现与消费:

服务消费者主要有两个目标:发现服务和消费服务;

服务的发现由eureka客户端提供,服务消费由由ribbon实现。

ribbon:ribbon基于http和TCP的客户端负载均衡器,它通过客户端配置的ribbonserverlist服务端列表轮询访问达到负载均衡的目的。ribbon和eureka整合使用,重写轮询机制。

依赖:

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

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

代码:

@EnableDiscoveryClient
@SpringBootApplication
public class FishRibbonApplication {
	@LoadBalanced
	@Bean
	RestTemplate restTemplate() {
		return new RestTemplate();
	}

	public static void main(String[] args) {
		SpringApplication.run(FishRibbonApplication.class, args);
	}
}
@RestController
public class ConsumerController {
	@Autowired
	private RestTemplate restTemplate;

	@RequestMapping("/ribbon-consumer")
	public String helloConsumer() {
		return restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody();
	}
	
	
}
spring.application.name=ribbon-consumer
server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/


猜你喜欢

转载自blog.csdn.net/qq_15140841/article/details/80994502