
全面的spring-cloud知识
全面的微服务技术图学习路线图单体架构与微服务架构微服务技术对比了解spring cloudspring cloud与spring boot的兼容版本服务拆分与远程调用Eureka
服务提供者服务消费者服务注册
配置eureka服务配置eureka服务提供者provider 服务发现负载均衡配置负载均衡的原理Ribbon策略(饥饿加载) Nacos注册中心
集群搭建
全面的微服务技术图 学习路线图 单体架构与微服务架构单体架构
分布式架构
分布式架构需要解决很多问题
微服务的特点
对微服务的认识
企业需求
服务拆分注意事项
注意:
服务调用存在的问题
*** 作步骤
1.导入依赖
注意:一定要在parent节点最后加上 ,不然子项目无法使用父项目的依赖。
4.0.0 pom eureka-server-7001 org.springframework.boot spring-boot-starter-parent2.1.3.RELEASE org.example spring-cloud-20211.0-SNAPSHOT 8 8 org.springframework.cloud spring-cloud-dependenciesGreenwich.SR1 pom import
2.书写启动类
主启动类一定要加 @EnableEurekaServer注解(开启eureka服务)
package com.hjx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer // 开启eureka服务
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
3.配置文件
注意:service-url下的
下面这两个一定要设置为false,否者eureka启动时会注册自己(因为自己还没有启动,所以会导致连接失败,导致报连接错误!!)
fetch-registry: false #获取注册信息 register-with-eureka: false # 注册到eureka服务中
server:
port: 7001
spring:
application:
name: eureka-server-7001 # eureka的服务名称
eureka:
instance:
hostname: 127.0.0.1
client:
service-url: # 注意:因为eureka自己也是一个服务,所以它会将它自己也注册
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka #eureka的地址的信息
fetch-registry: false
register-with-eureka: false
4.启动,测试
注意:
eureka控制台访问地址:http://localhost:7001(不要使用配置文件中配置的全地址访问,地址只要到端口就好了!!)
1.导入依赖
注意:一定要导入spring-boot-starter-web依赖,否者该服务启动后会导致服务停止(即服务注册失败,控制台无报错)
org.springframework.cloud spring-cloud-starter-eureka1.4.2.RELEASE org.springframework.boot spring-boot-starter-web
2.书写启动类
加上@EnableEurekaClient 注解
@EnableEurekaClient
@SpringBootApplication
public class EurekaProviderApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaProviderApplication.class,args);
}
}
3.配置文件
注意:service-url下的defaultZone没有提示(不要误以为依赖导错了!!)
server:
port: 7002
spring:
application:
name: provider-7002 #服务名称
eureka:
client:
register-with-eureka: true
service-url:
defaultZone: http://127.0.0.1:7001/eureka # 要注册到的地址
4.启动主启动类
5.进入eureka服务控制台,如下所示
1.导入依赖
org.springframework.cloud spring-cloud-starter-eureka1.4.2.RELEASE org.springframework.boot spring-boot-starter-web
2.主启动类
**package com.hjx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class,args);
}
}
**
3.编写配置
server:
port: 7003
spring:
application:
name: provider-7003 #服务名称
eureka:
client:
register-with-eureka: false # 不注册到eureka注册中心
fetch-registry: true # 获取注册信息(因为是client,就是为了获取服务的,所以为true)
service-url:
defaultZone: http://127.0.0.1:7001/eureka # 要注册到的地址
4.配置RestTemplate
将RestTemplate注入到spring 容器
@Configuration
public class MyConfig {
// 负载均衡
@LoadBalanced
@Bean
public RestTemplate Register(){
return new RestTemplate();
}
}
5.编写controller获取服务
@RestController
public class TestController {
@Autowired
private RestTemplate template;
@GetMapping("/get")
public String get(){
// 注意:这里的ip用服务名替代即可 getService:为服务提供方中controller中你的映射路径
String url="http://PROVIDER-7002/getService";
String msg=template.getForObject(url,String.class,"" );
return msg;
}
}
负载均衡配置
当多个服务的服务名相同时,客户端去请求该服务时,通过负载均衡策略就可以以一种策略(轮询)访问服务。(这里的@LoadBalanced注解即实现该负载均衡),具体代码在上面,这里只是对负载均衡一种说明而已。
注意:如果时中途修改一个服务的名字为一样时,则这时需要重启客户端module,否者没有轮询的效果!!
负载均衡的原理
懒加载
饥饿加载
配置在上面
先下载Nacos-server,(我这里是1.xx版本的),进入bin目录启动nacos服务
1.导入依赖
父工程依赖
org.springframework.cloud spring-cloud-dependenciesGreenwich.SR1 pom import com.alibaba.cloud spring-cloud-alibaba-dependencies2.1.0.RELEASE pom import
子工程依赖
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.boot
spring-boot-starter-web
2.编写主启动类
@SpringBootApplication
public class NacosServerApplication {
public static void main(String[] args) {
SpringApplication.run(NacosServerApplication.class,args);
}
}
3.配置文件编写
注意:一定要建立bootstramp.yml文件(名字必须是bootstramp,因为其优先级比application开头的配置文件都要高!!)
spring:
application:
name: nacos-server # 服务名称
cloud:
nacos:
discovery:
server-addr: localhost:8848 #nacos的服务地址
4.测试
依赖和上面nacos服务的配置一样,就是配置文件有些变化
bootstrap.yml 的配置内容如下:
server:
port: ${server.port:8080} # ${}:表示动态接收命令行参数
spring:
application:
name: ${server.name:nacos-server} #服务名
cloud:
nacos:
discovery:
server-addr: localhost:8848 #nacos服务的地址
cluster-name: ${cluster.name:ShangHai} #服务集群名称
动态传入参数方法
-Dserver.port=8081 -Dserver.name=nacos-server1 -Dcluster.name=ShangHai
复制工程配置
即可得到一个新的工程(和nacos服务的一模一样,就是服务的启动端口不一样而已)
测试
进入nacos控制台,点进服务列表(进入某一个服务,点击详情查看即可看到集群提示)
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)