springcloud---2
每一个都是独立的springboot工程。通过自己的ip和端口访问。
Eureka是服务发现组件,Eureka里面有一个服务注册表,存的是服务消费者和服务生产者的ip和端口。Eureka集群里面每个Eureka server也是一个Eureka client,因为他们之间要相互注册。
Eureka server:服务发现组件
Eureka client:服务消费者,服务提供者。他们要去操作服务发现组件的注册表。
package com.itmuch.cloud; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication
@EnableEurekaServer //是一个EurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
security:
basic:
enabled: true
user:
name: user
password: password123
server:
port: 8761
eureka:
client: #单机里面不把自己当成一eureka client
register-with-eureka: false #这里是单机eureka,不需要注册到别的eureka上面去
fetch-registry: false #单机,不需要吧数据合并到别的eureka上面去 service-url:
defaultZone: http://user:password123@localhost:8761/eureka #跟Eureka通信的地址,后面的eureka不能少。
#想把eureka发布到哪个url
#之所以eureka server也要配置这个是因为每个eureka server里面也有一个eureka client.
#因为eureka集群里面每个eureka server也是一个eureka client他们之间是需要相互通信同步注册表的。
<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> <parent>
<groupId>com.itmuch.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent> <artifactId>microservice-discovery-eureka</artifactId>
<packaging>jar</packaging> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <!-- spring-cloud-starter-eureka-server表示是一个eureka server
spring-cloud-starter-eureka表示是一个eureka client -->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!-- 添加依赖才可以使用用户名和密码 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies> </project>
这时一个Eureka server,跑起来:localhost:8761/
Last 1000since sinup:最近的1000个注册在上面的微服务。Instances currently registered with Eureka:注册在上面的微服务。General Info:eureka的信息,Instance Info:这个实例的信息,eureka的状态。
把其他服务注册到eureka上面去。
服务提供者(user微服务):
package com.itmuch.cloud; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication
//将应用注册到eureka server上面去
@EnableEurekaClient //EurekaClient。@EnableDiscoveryClient这个注解说他是一个服务发现的client不一定使用的是eureka,可以使用类似于eureka的组件zk,consule
public class MicroserviceSimpleProviderUserApplication { public static void main(String[] args) {
SpringApplication.run(MicroserviceSimpleProviderUserApplication.class, args);
}
}
package com.itmuch.cloud.controller; import java.util.ArrayList;
import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController; import com.google.common.collect.Lists;
import com.itmuch.cloud.entity.User;
import com.itmuch.cloud.repository.UserRepository;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient; @RestController
public class UserController { @Autowired
private UserRepository userRepository; @Autowired
private EurekaClient eurekaClient; @Autowired
private DiscoveryClient discoveryClient; @GetMapping("/simple/{id}")
public User findById(@PathVariable Long id) {
return this.userRepository.findOne(id);
} //http://localhost:7900/eureka-instance
@GetMapping("/eureka-instance")
public String serviceUrl() {
InstanceInfo instance = this.eurekaClient.getNextServerFromEureka("MICROSERVICE-PROVIDER-USER", false);
return instance.getHomePageUrl();//http://192.168.88.1:7900/ : 使用名称MICROSERVICE-PROVIDER-USER就可以知道ip和端口。
} @GetMapping("/instance-info")
public ServiceInstance showInfo() {
ServiceInstance localServiceInstance = this.discoveryClient.getLocalServiceInstance();
//{"host":"192.168.88.1","port":7900,"metadata":{"zone":"ABC","lilizhou":"BBC"},"uri":"http://192.168.88.1:7900","secure":false,"serviceId":"microservice-provider-user"}
return localServiceInstance;
} @PostMapping("/user")
public User postUser(@RequestBody User user) {
return user;
} // 该请求不会成功
@GetMapping("/get-user")
public User getUser(User user) {
return user;
} @GetMapping("list-all")
public List<User> listAll() {
ArrayList<User> list = Lists.newArrayList();
User user = new User(1L, "zhangsan");
User user2 = new User(2L, "zhangsan");
User user3 = new User(3L, "zhangsan");
list.add(user);
list.add(user2);
list.add(user3);
return list; }
}
package com.itmuch.cloud.repository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; import com.itmuch.cloud.entity.User; @Repository
public interface UserRepository extends JpaRepository<User, Long> { }
server:
port: 7900
spring:
jpa:
generate-ddl: false
show-sql: true
hibernate:
ddl-auto: none
datasource:
platform: h2
schema: classpath:schema.sql
data: classpath:data.sql
application:
name: microservice-provider-user #这个微服务在eureka上面的名字,全部小写
logging:
level:
root: INFO
org.hibernate: INFO
org.hibernate.type.descriptor.sql.BasicBinder: TRACE
org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
com.itmuch: DEBUG
eureka:
client:
healthcheck:
enabled: true #健康检查,要添加actuator依赖
serviceUrl:
#eureka server的地址
defaultZone: http://user:password123@localhost:8761/eureka
instance:
prefer-ip-address: true #默认主机名访问的,配置这个就可以通过ip访问,
#将服务名字ip端口都显示上去
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
metadata-map:
zone: ABC # eureka可以理解的元数据
lilizhou: BBC # 不会影响客户端行为
lease-renewal-interval-in-seconds: 5
<?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> <artifactId>microservice-provider-user</artifactId>
<packaging>jar</packaging> <name>microservice-provider-user</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>com.itmuch.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency> <!-- 其他依赖正常加,spring-cloud-starter-eureka后面没有server表示是一个client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency> <!-- 监控和管理生产环境的模块
http://localhost:7900/env:可以查看环境
http://localhost:7900/health:当前应用的健康状态
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies> </project>
新增一个连接,这就是服务注册表的内容
服务消费者(movie微服务):
package com.itmuch.cloud; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableEurekaClient
public class MicroserviceSimpleConsumerMovieApplication { @Bean //方法名就是实例化后的对象的名字
public RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(MicroserviceSimpleConsumerMovieApplication.class, args);
}
}
package com.itmuch.cloud.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import com.itmuch.cloud.entity.User; @RestController
public class MovieController {
@Autowired
private RestTemplate restTemplate; //user: userServicePath: http://localhost:7900/simple/ (服务提供者user的地址)
//这里还是直接调用的是用户微服务的ip和地址
//要通过eureka来调用,多个用户微服务要有负载均衡
@Value("${user.userServicePath}")
private String userServicePath; @GetMapping("/movie/{id}")
public User findById(@PathVariable Long id) {
return this.restTemplate.getForObject(this.userServicePath + id, User.class);
}
}
spring:
application:
name: microservice-consumer-movie
server:
port: 7901
user:
userServicePath: http://localhost:7900/simple/
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://user:password123@localhost:8761/eureka
instance:
prefer-ip-address: true
<?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> <artifactId>microservice-consumer-movie</artifactId>
<packaging>jar</packaging> <parent>
<groupId>com.itmuch.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
springcloud---2的更多相关文章
- 【微框架】之一:从零开始,轻松搞定SpringCloud微框架系列--开山篇(spring boot 小demo)
Spring顶级框架有众多,那么接下的篇幅,我将重点讲解SpringCloud微框架的实现 Spring 顶级项目,包含众多,我们重点学习一下,SpringCloud项目以及SpringBoot项目 ...
- springcloud(第三篇)springcloud eureka 服务注册与发现 *****
http://blog.csdn.net/liaokailin/article/details/51314001 ******************************************* ...
- SpringCloud Sleuth 使用
1. 介绍 Spring-Cloud-Sleuth是Spring Cloud的组成部分之一,为SpringCloud应用实现了一种分布式追踪解决方案,其兼容了Zipkin, HTrace和log- ...
- SpringCloud+Consul 服务注册与服务发现
SpringCloud+Consul 服务注册与服务发现 1. 服务注册: 在Spring.factories有一段: # Discovery Client Configuration org.spr ...
- SpringCloud学习后获取的地址
关于SpringCloud + Docker 学习地址: (1) https://yq.aliyun.com/articles/57265 (2) https://yq.aliyun.com/team ...
- SpringCloud网关ZUUL集成consul
最近一直在搞基于springcloud的微服务开发,为了不限定微服务开发语言,服务发现决定采用consul不多说上代码 pom文件 <project xmlns="http://mav ...
- springcloud(一):大话Spring Cloud
研究了一段时间spring boot了准备向spirng cloud进发,公司架构和项目也全面拥抱了Spring Cloud.在使用了一段时间后发现Spring Cloud从技术架构上降低了对大型系统 ...
- springcloud(二):注册中心Eureka
Eureka是Netflix开源的一款提供服务注册和发现的产品,它提供了完整的Service Registry和Service Discovery实现.也是springcloud体系中最重要最核心的组 ...
- springcloud(四):熔断器Hystrix
说起springcloud熔断让我想起了去年股市中的熔断,多次痛的领悟,随意实施的熔断对整个系统的影响是灾难性的,好了接下来我们还是说正事. 熔断器 雪崩效应 在微服务架构中通常会有多个服务层调用,基 ...
- springcloud(六):配置中心(一)
随着线上项目变的日益庞大,每个项目都散落着各种配置文件,如果采用分布式的开发模式,需要的配置文件随着服务增加而不断增多.某一个基础服务信息变更,都会引起一系列的更新和重启,运维苦不堪言也容易出错.配置 ...
随机推荐
- TCP/IP 在 Windows 下的实现
Windows 实现TCP/IP 协议也是建立在上一篇博客的OSI 基础之上的. 用户态是由ws2_32.dll 和一些其他服务提供者的 dll 共同实现,当中ws2_32.dll 是一个框架.能够容 ...
- 系统管理模块_用户管理1_实现用户有关的功能_测试功能、解决事务的问题、对密码进行MD5摘要
系统管理模块__用户管理1__实现用户有关的功能 了解用户管理要做什么(增删改查初始化密码) 设计实体 分析功能有几个对应几个请求 增删改查有6个请求,初始化密码一个 实现增删改查一组功能的步骤流程 ...
- 微软发布新版 Skype Linux 客户端
导读 前两天,微软说要给Linux 用户带来一个令人兴奋的新闻,今天,这个新闻来了.它刚刚为 Linux 发布了一个新的 Skype 客户端.此次发布,微软为 Linux 带来的 Skype 客户端与 ...
- 【黑金原创教程】【TimeQuest】【第五章】网表质量与外部模型
声明:本文为黑金动力社区(http://www.heijin.org)原创教程,如需转载请注明出处,谢谢! 黑金动力社区2013年原创教程连载计划: http://www.cnblogs.com/al ...
- Fragment、FragmentTabHost以及RadioGroup框架
package com.dotfive.chuanbang.activity; import test.Fragment1; import test.Fragment1.OnBackListener; ...
- Angular2+ 基本知识汇总
Angular是Google推出的Web前端开发框架,从12年发布起就受到了强烈的关注,他首次提出了双向绑定的概念,让人耳目一新. Angular 2特性 就在2016年9月中旬,时隔4年,Googl ...
- Eclipse+ADT+Android SDK 搭建安卓开发环境(版权属于forever-z)
运行环境 windows 7或者10(64位); 为例eclipse-jee-neon-3-win32-x86_64: ADT-23.0.4 下载地址 安装JDK 这里可以参考关于安装JDK的教程,请 ...
- django-网页视屏播放
基本都基于第三方: -cc视频 -播放免费视频 -收费视频 -需要做认证,cc视频会给你发消息,你返回,携带数据 -在前端页面中添加响应的视屏框的代码 -功能实现,有相关接口文档,配置即可
- Ubentu下安装Docker
具体可以查看Docker官网,我是在服务器上面操作 1,sudo apt-get install -y apt-transport-https ca-certificates curl softwar ...
- 又一次认识java(四) — 组合、聚合与继承的爱恨情仇
有人学了继承,认为他是面向对象特点之中的一个,就在全部能用到继承的地方使用继承,而不考虑到底该不该使用,无疑.这是错误的.那么.到底该怎样使用继承呢? java中类与类之间的关系 大部分的刚開始学习的 ...