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(六):配置中心(一)
随着线上项目变的日益庞大,每个项目都散落着各种配置文件,如果采用分布式的开发模式,需要的配置文件随着服务增加而不断增多.某一个基础服务信息变更,都会引起一系列的更新和重启,运维苦不堪言也容易出错.配置 ...
随机推荐
- nexus配置第三方库文件
进入nexus管理界面 默认用户名密码:admin/admin123 在左侧Views/Repositories中选择Repositories,然后在右侧的面板中选择3rd party,在下方arti ...
- 160601、Websocet服务端实现
今天是六一儿童节,祝愿小朋友们节日快乐!大朋友们事事顺心! Websocet服务端实现 WebSocketConfig.Java ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
- 防止Form中嵌入WebBrowser出错导致程序崩溃
siow(1253366) 10:11:13两种方法你用的自带的webbrowser还是embeded那个毛小毛(3335076) 10:12:15或者有什么办法拦截到是webbrowser,如 ...
- R语言中基于聚类的离群点挖掘
思路:首先,通过K-means算法将数据点划分为成若K个簇:然后计算每一个数据对象到最近簇的中心距离,来与离群点设置的阈值进行比较,以此来判别该数据对象是否是离群点. 1.读取数据 data<- ...
- HDU5667—Sequence(对数转化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5667 题目意思:f1=1,i=1 f2=2 ,i=2 fi=a^b*f[i-1]^c*f[i-2] i ...
- [iOS微博项目 - 4.0] - 自定义微博cell
github: https://github.com/hellovoidworld/HVWWeibo A.自定义微博cell基本结构 1.需求 创建自定义cell的雏形 cell包含:内容.工具条 内 ...
- libtorch 哪些函数比较常用?
libtorch 加载 pytorch 模块进行预测示例 void mat2tensor(const char * path, torch::Tensor &output) { //读取图片 ...
- CF#301 D:Bad Luck Island (概率dp)
D:Bad Luck Island 一个岛上有r个石头,s个剪子,p个布,他们之间随机挑出两个相遇,如果不是相同物种,就会有一个消失,分别求出最后这座岛上只剩下一个物种的概率. 我们用dp[i][j] ...
- add() 方法用于向 <select> 添加一个 <option> 元素。
//add() 方法用于向 <select> 添加一个 <option> 元素. //new Option() 创建一个option标签 school.add(new Opti ...
- Python高级特性(2):Closures、Decorators和functools(转)
原文:Python高级特性(2):Closures.Decorators和functools 装饰器(Decorators) 装饰器是这样一种设计模式:如果一个类希望添加其他类的一些功能,而不希望通过 ...