SpringCloud + Eureka / Nacos

git:https://github.com/huanmsf/springCloudLearn.git

项目目录:

父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)的更多相关文章

  1. Spring Cloud学习笔记--Spring Boot初次搭建

    1. Spring Boot简介 初次接触Spring的时候,我感觉这是一个很难接触的框架,因为其庞杂的配置文件,我最不喜欢的就是xml文件,这种文件的可读性很不好.所以很久以来我的Spring学习都 ...

  2. Spring Cloud 学习笔记 (一)-- Eureka 服务器

    开局一张图,截取了本人学习资料中的一张图,很好地展示了Eureka的架构. Eureka服务器 管理服务的作用.细分为服务注册,服务发现. 所有的客户端在Eureka服务器上注册服务,再从Eureka ...

  3. Spring Cloud 学习笔记(二)——Netflix

    4 Spring Cloud Netflix Spring Cloud 通过自动配置和绑定到Spring环境和其他Spring编程模型惯例,为Spring Boot应用程序提供Netflix OSS集 ...

  4. Spring Cloud 学习笔记(一)——入门、特征、配置

    [TOC] 0 放在前面 0.1 参考文档 http://cloud.spring.io/spring-cloud-static/Brixton.SR7/ https://springcloud.cc ...

  5. Spring Cloud学习笔记-006

    服务容错保护:Spring Cloud Hystrix 在微服务架构中,我们将系统拆分成了很多服务单元,各单元的应用间通过服务注册与订阅的方式互相依赖.由于每个单元都在不同的进程中运行,依赖通过远程调 ...

  6. Spring Cloud学习笔记-005

    服务消费者 之前已经搭建好了微服务中的核心组件——服务注册中心(包括单节点模式和高可用模式).也有了服务提供者,接下来搭建一个服务消费者,它主要完成两个目标,发现服务以及消费服务.其中,服务发现的任务 ...

  7. Spring Cloud学习笔记-002

    搭建Spring Cloud注册中心:Eureka 服务注册:在服务治理框架中,通常都会构建一个注册中心,每个服务单元向注册中心登记自己提供的服务,将主机与端口号.版本号.通信协议等一些附加信息告诉注 ...

  8. Spring Cloud学习笔记-007

    声明式服务调用:Spring Cloud Feign Feign基于Netflix Feign实现,整合了Spring Cloud Ribbon和Spring Cloud Hystrix,除了提供这两 ...

  9. Spring Cloud学习笔记-008

    继承特性 通过上节的示例实践,当使用Spring MVC的注解来绑定服务接口时,几乎完全可以从服务提供方的Controller中依靠复制操作,构建出相应的服务客户端绑定接口.既然存在这么多复制操作,自 ...

  10. Spring Cloud学习笔记-009

    API网关服务:Spring Cloud Zuul API网关是一个更为智能的应用服务器,它的定义类似于面向对象设计模式中的Façade模式,它的存在就像是整个微服务架构系统的门面一样,所有的外部客户 ...

随机推荐

  1. 【zabbix教程系列】六、自动注册(Linux)

    一.agent安装脚本 #!/bin/bash #ltt #安装zabbix源 rpm -ivh http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zab ...

  2. 用标准3层神经网络实现MNIST识别

    一.MINIST数据集下载 1.https://pjreddie.com/projects/mnist-in-csv/      此网站提供了mnist_train.csv和mnist_test.cs ...

  3. 关系型数据库 VS 非关系型数据库

    一.关系型数据库? 1.概念 关系型数据库是指采用了关系模型来组织数据的数据库.简单来说,关系模式就是二维表格模型. 主要代表:SQL Server,Oracle,Mysql,PostgreSQL. ...

  4. 常见的概率分布类型(Probability Distribution)

    统计学中最常见的几种概率分布分别是正态分布(normal distribution),t分布(t distribution),F分布(F distribution)和卡方分布(χ2 distribut ...

  5. List根据对象的两个字段进行排序,并且有一个倒序

    用java8 的lambda 表达式 list.sort(Comparator.comparing(Live::getId) .thenComparing(Live::getAppId, Compar ...

  6. 牛客小白月赛13-J小A的数学题 (莫比乌斯反演)

    链接:https://ac.nowcoder.com/acm/contest/549/J来源:牛客网 题目描述 小A最近开始研究数论题了,这一次他随手写出来一个式子,∑ni=1∑mj=1gcd(i,j ...

  7. css进度条

    1.环形进度条 源码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...

  8. appache 在windows 中无法启动的测试

    使用phpstudy刚启动就自动停止,排除端口被占用后,总感觉找不到好的调试方法 网上找了一些资料,这个方法不错,所以就记录了下来.最好的办法是找appache的bin目录,打开运行,输入httpd. ...

  9. libiconv交叉编译提示arm-none-linux-gnueabi-gcc

    title: libiconv交叉编译提示arm-none-linux-gnueabi-gcc date: 2019/3/6 17:45:48 toc: true --- libiconv交叉编译提示 ...

  10. Apache Hadoop 2.9.2 的YARN High Available 模式部署

    Apache Hadoop 2.9.2 的YARN High Available 模式部署 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.环境准备 1>.官方文档(htt ...