spring cloud 学习笔记(1)
SpringCloud + Eureka / Nacos
项目目录:
父pom:
<?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>spring.cloud.learn</groupId>
<artifactId>sc-learn</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>api</module>
<module>consumer-eureka</module>
<module>consumer-nacos</module>
<module>provider-eureka</module>
<module>provider-nacos</module>
<module>eureka-server</module>
</modules>
<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>Finchley.RELEASE</spring-cloud.version>
<spring-boot.version>2.0.4.RELEASE</spring-boot.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
基于Eureka的服务注册和发现 provider-eureka
目录
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>
<parent>
<groupId>spring.cloud.learn</groupId>
<artifactId>sc-learn</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>sc.learn.provider.eureka</groupId>
<artifactId>provider-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>provider-eureka</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<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>
<dependency>
<groupId>spring.cloud.learn.api</groupId>
<artifactId>api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>spring.cloud.learn.api</groupId>
<artifactId>api</artifactId>
<version>2.1.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 7070
spring:
application:
name: provider-eureka
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
ProviderEurekaApplication
@SpringBootApplication
@EnableEurekaClient
public class ProviderEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderEurekaApplication.class, args);
}
}
EchoServiceController
@RestController
@RequestMapping("/pe")
public class EchoServiceController {
@Autowired
private EchoService echoService;
@GetMapping("/echo")
public String echo(@RequestParam String name) {
return echoService.echo(name);
}
}
结果
基于Eureka的服务注册和发现 consumer-eureka
目录
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>
<parent>
<groupId>spring.cloud.learn</groupId>
<artifactId>sc-learn</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>sc.learn.consumer.eureka</groupId>
<artifactId>consumer-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consumer-eureka</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>spring.cloud.learn.api</groupId>
<artifactId>api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>spring.cloud.learn.api</groupId>
<artifactId>api</artifactId>
<version>2.1.4.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 7000
spring:
application:
name: consumer-eureka
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
ConsumerEurekaApplication
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableDiscoveryClient
public class ConsumerEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerEurekaApplication.class, args);
}
}
InvokeEchoServiceController
@RestController
@RequestMapping("/ce")
public class InvokeEchoServiceController {
@Autowired
private HelloController helloController;
@GetMapping("/echo")
public String echo(@RequestParam String name) {
return helloController.hello(name);
}
}
HelloController
@FeignClient(value = "provider-eureka")
public interface HelloController {
@GetMapping("/pe/echo")
String hello(@RequestParam(value = "name") String name);
}
结果
基于Nacos的服务注册和发现和配置中心 provider-nacos
目录
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>
<parent>
<groupId>spring.cloud.learn</groupId>
<artifactId>sc-learn</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>sc.learn.provider.nacos</groupId>
<artifactId>provider-nacos</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>provider-nacos</name>
<description>Demo project for Spring Boot</description>
<properties>
<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-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
</project>
bootstrap.properties
spring.application.name=provider-nacos
server.port=9090
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
application.properties 空着,配置中心只能写在bootstrap.properties中,不然报错
ProviderNacosApplication
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderNacosApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderNacosApplication.class, args);
}
}
EchoController
@RestController
@RequestMapping("/pn")
@RefreshScope
public class EchoController {
@Value("${sys.user.name:null}")
private String user;
@GetMapping(value = "/hello")
public String hello(@RequestParam String name) {
return user + "Hello : " + name;
}
@GetMapping(value = "/echo")
public String echo(@RequestParam String name) {
return user + "Echo : " + name;
}
}
结果
基于Nacos的服务注册和发现和配置中心 consumer-nacos
目录
bootstrap.properties
server.port=9000
spring.application.name=consumer-nacos
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
ConsumerNacosApplication
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerNacosApplication {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerNacosApplication.class, args);
}
}
EchoControllerCn
@RestController
@RequestMapping("/cn")
@RefreshScope
public class EchoControllerCn {
private final RestTemplate restTemplate;
@Value("${sys.user.name:null}")
private String user;
@Autowired
public EchoControllerCn(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@GetMapping(value = "/hello")
public String echo(@RequestParam String name) {
return restTemplate.getForObject("http://nacos-provider/pn/hello?name=" + name, String.class);
}
@Autowired
private HelloController2 helloController2;
@Autowired
private HelloController helloController1;
@GetMapping(value = "/hello1")
public String echo1(String name) {
return user + helloController1.hello(name);
}
@GetMapping(value = "/hello2")
public String echo2(String name) {
return user + helloController2.hello(name);
}
}
HelloController
@FeignClient(value = "provider-nacos")
public interface HelloController {
@GetMapping("/pn/hello")
String hello(@RequestParam(value = "name") String name);
}
结果
Nacos 服务器
nacos多环境配置
参考:https://www.cnblogs.com/didispace/p/10340685.html#undefined
修改配置文件
nacos.config-dev.properties:
sys.user.name=user-dev
nacos.config-test.properties:
sys.user.name=user-test
指定配置文件前缀
spring.cloud.nacos.config.prefix=nacos.config
添加启动参数
-Dspring.profiles.active=test
结果:
Nacos配置
spring cloud 学习笔记(1)的更多相关文章
- Spring Cloud学习笔记--Spring Boot初次搭建
1. Spring Boot简介 初次接触Spring的时候,我感觉这是一个很难接触的框架,因为其庞杂的配置文件,我最不喜欢的就是xml文件,这种文件的可读性很不好.所以很久以来我的Spring学习都 ...
- Spring Cloud 学习笔记 (一)-- Eureka 服务器
开局一张图,截取了本人学习资料中的一张图,很好地展示了Eureka的架构. Eureka服务器 管理服务的作用.细分为服务注册,服务发现. 所有的客户端在Eureka服务器上注册服务,再从Eureka ...
- Spring Cloud 学习笔记(二)——Netflix
4 Spring Cloud Netflix Spring Cloud 通过自动配置和绑定到Spring环境和其他Spring编程模型惯例,为Spring Boot应用程序提供Netflix OSS集 ...
- Spring Cloud 学习笔记(一)——入门、特征、配置
[TOC] 0 放在前面 0.1 参考文档 http://cloud.spring.io/spring-cloud-static/Brixton.SR7/ https://springcloud.cc ...
- Spring Cloud学习笔记-006
服务容错保护:Spring Cloud Hystrix 在微服务架构中,我们将系统拆分成了很多服务单元,各单元的应用间通过服务注册与订阅的方式互相依赖.由于每个单元都在不同的进程中运行,依赖通过远程调 ...
- Spring Cloud学习笔记-005
服务消费者 之前已经搭建好了微服务中的核心组件——服务注册中心(包括单节点模式和高可用模式).也有了服务提供者,接下来搭建一个服务消费者,它主要完成两个目标,发现服务以及消费服务.其中,服务发现的任务 ...
- Spring Cloud学习笔记-002
搭建Spring Cloud注册中心:Eureka 服务注册:在服务治理框架中,通常都会构建一个注册中心,每个服务单元向注册中心登记自己提供的服务,将主机与端口号.版本号.通信协议等一些附加信息告诉注 ...
- Spring Cloud学习笔记-007
声明式服务调用:Spring Cloud Feign Feign基于Netflix Feign实现,整合了Spring Cloud Ribbon和Spring Cloud Hystrix,除了提供这两 ...
- Spring Cloud学习笔记-008
继承特性 通过上节的示例实践,当使用Spring MVC的注解来绑定服务接口时,几乎完全可以从服务提供方的Controller中依靠复制操作,构建出相应的服务客户端绑定接口.既然存在这么多复制操作,自 ...
- Spring Cloud学习笔记-009
API网关服务:Spring Cloud Zuul API网关是一个更为智能的应用服务器,它的定义类似于面向对象设计模式中的Façade模式,它的存在就像是整个微服务架构系统的门面一样,所有的外部客户 ...
随机推荐
- QPen
Help on class QPen in module PyQt5.QtGui: class QPen(sip.simplewrapper) | QPen() | QPen(Qt.PenStyl ...
- scala的多种集合的使用(2)之集合常用方法
一.常用的集合方法 1.可遍历集合的常用方法 下表列出了Traverable在所有集合常用的方法.接下来的符号: c代表一个集合 f代表一个函数 p代表一个谓词 n代表一个数字 op代表一个简单的操作 ...
- UnityInspector显示扩展
比如经常在三方插件中看到如下在Inspector中的样式 这种对特别是要做编辑序列化数据脚本操作很友好,但是这个是如何实现呢?比如我们要创建一个保存序列化的npc基本数据,名字(Name),性别(Se ...
- Java Selenium中的几种等待方式
Selenium自动化性能测试过程中,经常会出现取不到界面元素,主要原因是界面元素的加载与我们访问页面的时机不一致.可能是界面要素过多或者网络较慢,界面一直加载中:为了解决这种问题,selenium提 ...
- array数组(n,1)和(n,)互相转换
data.shape #(172,1) result = [arr[0] for arr in data] result.shape #(172,) (172,1)表示是一个(172,1)shape大 ...
- bugku 逆向 take the maze
看到如果判断正确之后 会生成一个png文件 直接用idc脚本生成: auto v,begin,end,dexbyte; v = fopen("flag.png", "wb ...
- Vue-详解设置路由导航的两种方法
https://www.cnblogs.com/superlizhao/p/8527317.html
- 学习java接口知识
学习java接口知识 //一个类最多只能有一个直接的父类.但是有多个间接的父类. java是单继承. class ye_01{ String name; } class fu_01 extends y ...
- [powershell]获取FCID&Port
Get-InitiatorID Get-InitiatorPort
- 20165223《网络对抗技术》Exp5 MSF基础应用
目录 -- MSF基础应用 实验说明 实验任务内容 基础问题回答 实验内容 主动攻击 ms17_10_eternalblue(成功) ms17_10_psexec(成功) ms08_067_netap ...