- SpringCloud
- SpringCloud入门
- Eureka服务注册与发现
- 集群
- CAP
- ribbon
- Feign负载均衡
- Hystrix服务熔断
- Hystrix服务降级
- Dashboard流监控
- 路由网关Zuul
- SpringCloud 连接github
解决方案: Spring Cloud 生态: SpringBoot
1.Spring Cloud NetFlix 一站式解决方案
api网关 ,zuul组件
Feign —HttpClinet—Http通信方式,同步,阻塞
服务器足额从发现 Eureka
熔断机制:Hystrix
2.Apache Dubbo Zookeeper 半自动 ,需要整合别人
API :没有,找第三方,或者自己实现
Dubbo:
Zookeeper
借助 Hystrix
Dubbo 不完善
3.Spring Cloud Alibaba 一站式解决方案 简单
解决关键
1.api
2.HTTP,RPC
3.注册和发现
4.熔断机制
1.新建maven项目
2.导包
8 8 4.12 1.16.10 org.springframework.cloud spring-cloud-dependenciesGreenwich.SR1 pom import org.springframework.boot spring-boot-dependencies2.1.4.RELEASE pom import mysql mysql-connector-java5.1.47 com.alibaba druid1.1.10 org.mybatis.spring.boot mybatis-spring-boot-starter1.3.2 junit junit${junit.version} org.projectlombok lombok${lombok.version} log4j log4j1.2.17
3.新建一个api的 model 模块
导入相应的依赖,有的是 父类里面有 就可以不用导入
com.it org.example 1.0-SNAPSHOT 4.0.0 springclound-apiorg.projectlombok lombok8 8
api 的 model --写实体类的(pojo)
package com.it.pojo;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;
@Data
@NoArgsConstructor
@Accessors(chain = true)
public class Dept implements Serializable { //Dept 实体类
private Long id; //主键
private String name;
//一个服务对应一个数据库,同一个信息可能存在不同数据库
private String db_source;
public Dept(String name) {
this.name = name;
}
}
4.新建一个实现功能的model
1.导入相应的依赖
com.it org.example 1.0-SNAPSHOT 4.0.0 springcloud-dept-80018 8 org.springframework.boot spring-boot-starter-actuatororg.springframework.cloud spring-cloud-starter-eureka-server1.4.6.RELEASE org.example springclound-api1.0-SNAPSHOT junit junitmysql mysql-connector-javacom.alibaba druidch.qos.logback logback-coreorg.mybatis.spring.boot mybatis-spring-boot-starterorg.springframework.boot spring-boot-testorg.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-jettyorg.springframework.boot spring-boot-devtoolsorg.example springclound-api1.0-SNAPSHOT compile
2.配置相应的yml
server:
port: 8001
#mybatis配置
mybatis:
type-aliases-package: com.it.pojo
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
#spring配置
spring:
application:
name: springcloud-provider-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8
username: root
password: root
#Eureka配置
#Eureka配置
eureka:
client:
service-url: #监控页面
defaultZone: http://localhost:7001/eureka/ #发布位置, 可以设置集群 ,所有直接把所有集群 都加上去
instance:
instance-id: springcloud-dept-8001 #修改eureka 上的默认描述信息
#info配置
info:
app.name: song's first springcloud program
company.name: song666
3.然后写对应的service 和dao ,controller层 以及相应的mybatis配置等
DeptController类
package com.it.controller;
import com.it.pojo.Dept;
import com.it.service.DeptService;
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.RestController;
import java.util.List;
//提供restful服务
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@Autowired
private DiscoveryClient client;
@PostMapping("/dept/add")
public boolean addDept(Dept dept)
{
return deptService.addDept(dept);
}
@GetMapping("/dept/get/{id}")
public Dept get(@PathVariable("id") Long id)
{
return deptService.queryById(id);
}
@GetMapping("/dept/list")
public List queryAll()
{
return deptService.queryAll();
}
//注册进来的微服务,获取一些信息
@GetMapping("/dept/discovery")
public Object discovery()
{
//获取微服务列表清单
List services=client.getServices();
System.out.println("discovery=>services: "+services);
//得到微服务的具体信息
List instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
for (ServiceInstance instance : instances) {
System.out.println(
instance.getHost()+"t"+
instance.getPort()+"t"+
instance.getUri()+"t"+
instance.getServiceId()
);
}
return this.client;
}
}
Deptdao接口
package com.it.dao;
import com.it.pojo.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface DeptDao {
public boolean addDept(Dept dept);
public Dept queryById(Long id);
public List queryAll();
}
DeptService接口
package com.it.service;
import com.it.pojo.Dept;
import java.util.List;
public interface DeptService {
public boolean addDept(Dept dept);
public Dept queryById(Long id);
public List queryAll();
}
DeptServiceImpl实现类
package com.it.service;
import com.it.dao.DeptDao;
import com.it.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService{
@Autowired
private DeptDao deptDao;
@Override
public boolean addDept(Dept dept)
{
return deptDao.addDept(dept);
}
public Dept queryById(Long id)
{
return deptDao.queryById(id);
}
public List queryAll()
{
return deptDao.queryAll();
}
}
mybatis-config.xml
DeptMapper.xml
insert into dept(name,source) values (#{name},DATAbase()) select * from dept where id= #{id};
4.写启动类
package com.it;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
//启动类
@SpringBootApplication
@EnableEurekaServer
@EnableDiscoveryClient //服务发现
public class DeptProvide {
public static void main(String[] args) {
SpringApplication.run(DeptProvide.class,args);
}
}
5.写一个用户 module
1.导依赖
com.it org.example 1.0-SNAPSHOT 4.0.0 springcloud-dept-user-80org.example springclound-api1.0-SNAPSHOT org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-devtools8 8
2.配置端口(yml)
server:
port: 8002
3.写ConfigBean
package com.it.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ConfigBean {
@Bean
public RestTemplate getRestTemplate()
{
return new RestTemplate();
}
}
4.写相应的控制层,利用RestTemplate 调取其他moudel的方法
package com.it;
import com.it.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
public class DeptUserController {
//消费者 不应该有service层
//(url,实体:map,返回类型:Class responseType)
@Autowired
private RestTemplate restTemplate;
private static final String REST_URL_PREFIX="http://localhost:8001";
@RequestMapping("/user/dept/add")
public boolean add(Dept dept)
{
return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add",dept,Boolean.class);
}
@RequestMapping("/user/dept/get/{id}")
public Dept get(@PathVariable("id") Long id)
{
return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id,Dept.class);
}
@RequestMapping("/user/dept/list")
public List list()
{
return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list",List.class);
}
}
5.写用户启动类
package com.it;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Deptuser80 {
public static void main(String[] args) {
SpringApplication.run(Deptuser80.class,args);
}
}
Eureka服务注册与发现
1.开一个新的moudle 然后导包
com.it org.example 1.0-SNAPSHOT 4.0.0 springcloud-Eureka-7001org.springframework.cloud spring-cloud-starter-eureka-server1.4.6.RELEASE org.springframework.boot spring-boot-devtools8 8
2.配置yml
server:
port: 7001
#Eureka配置
eureka:
instance:
hostname: localhost #服务端实例名称
client:
register-with-eureka: false #是否向eureka注册中心注册自己
fetch-registry: false #false 自己为注册中心
service-url: #监控页面
#设置关联 如果本身是 7001的 集群 下面就 连接http://eureka7001.com:7001/eureka/,http://eureka7002.com:7003/eureka/
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3.写 启动类 测试
package com.it;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //接收别人注册
public class Eureka7001 {
public static void main(String[] args) {
SpringApplication.run(Eureka7001.class,args);
}
}
集群
集群就相当于多个Eureka 然后让这些 moudle 模块相互关联
主要是让·1每个集群相互关联,导致有一个集群崩了,另外一个可以继续运行
直接配置多个Eureka
1.配置pom.xml
org.springframework.cloud spring-cloud-starter-eureka-server1.4.6.RELEASE org.springframework.boot spring-boot-devtools
2.配置yml
server:
port: 7002
#Eureka配置
eureka:
instance:
hostname: localhost #服务端实例名称
client:
register-with-eureka: false #是否向eureka注册中心注册自己
fetch-registry: false #false 自己为注册中心
service-url: #监控页面
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3.写启动类
package com.it;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //接收别人注册
public class Eureka7002 {
public static void main(String[] args) {
SpringApplication.run(Eureka7002.class,args);
}
}
CAP
c 一致性 a可用性 p分区容错性 满足3选2
Zookeeper保证的是cp 服务注册功能对可用性的要求高于一致性 当一个节点 死了的时候,会重新选择一个节点,如果几个节点数据量比较大,选举的时候花的时间就会长一点,30-120s且选举的时候集群是不可用的,选举的时候会导致服务器瘫痪
Eureka保证的是ap
Eureka 先保证可用
Eureka可以很好的应对网络故障导致的部分节点失去联系的情况,几个节点挂掉后 不影响别的节点的工作,挂掉一台后,会自动跳到另一台节点,只是有带你慢,如果85%的节点不能用的话就会认为 崩溃.
Eureka可以很好的应对网络故障导致的部分节点失去联系的情况,不会像zookeeper使整个注册服务 瘫痪
客户端实现负载均衡
负载均衡就是将用户的请求平摊分配搭配多个服务上,从而达到系统的HA(高可用)
轮询,随机 算法
1.导包
org.springframework.cloud spring-cloud-starter-ribbon1.4.6.RELEASE org.springframework.cloud spring-cloud-starter-eureka-server1.4.6.RELEASE
2.配置yml
server:
port: 8002
#Eureka
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3.在启动类上加注解
@EnableEurekaServer
4.在ConfigBean里面配置负载均衡
直接在RestTemplate上加上注解就行
@LoadBalanced //Ribbin
5.修改控制器里面的地址,修改成变量
//Ribbon是一个变量,通过服务名来访问的,就是 获取提供者的地址
//private static final String REST_URL_PREFIX="http://localhost:8001";
private static final String REST_URL_PREFIX="SPRINGCLOUD-PROVIDER-DEPT";
Feign负载均衡
feign一种声明式的web service客户端,集成了Ribbon 和Eureka,主要是社区,接口编程,调用微服务的2种方法
1.微服务的名字 ribbon
2.接口和注解 feign
只需要用接口 并且使用注解去配置在dao接口上标注mapper,并且一个微服务标注一个Feign注解即可
目录结构
.
1.Feign是通过接口的方式来实现的,因此需要改变 api
在接口服务器中添加提供服务器中的接口
package com.it.service;
import com.it.pojo.Dept;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.List;
@Component
@FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT")
public interface DeptClientService {
@GetMapping("/dept/get/{id}")
public Dept queryById(@PathVariable("id") Long id);
@GetMapping("/dept/list")
public List queryAll();
@GetMapping("/dept/add")
public Boolean addDept(Dept dept);
}
2.控制器中直接用注解去接收
package com.it;
import com.it.pojo.Dept;
import com.it.service.DeptClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
public class DeptUserController {
@Autowired
private DeptClientService service=null;
@RequestMapping("/user/dept/add")
public boolean add(Dept dept)
{
return this.service.addDept(dept);
}
@RequestMapping("/user/dept/get/{id}")
public Dept get(@PathVariable("id") Long id)
{
return this.service.queryById(id);
}
@RequestMapping("/user/dept/list")
public List list()
{
return this.service.queryAll();
}
}
3.添加启动类 就可以测试
package com.it;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"com.it"})
@ComponentScan("com.it")
public class FeDeptuser80 {
public static void main(String[] args) {
SpringApplication.run(FeDeptuser80.class,args);
}
}
4.只需要通过控制器的方法,就可以访问相应接口的方法,然后接口的方法去调用提供者的接口方法来实现功能
Hystrix服务熔断
雪崩: 用户去掉 客户端的服务,然后 一个服务会去调另外一个服务,多的几个服务,如果中间一个服务相应时间过长 或者不可用,然后就会在a服务有很多调用,占用很多资源,导致系统崩溃。然后就可以直接给他一个备用的服务,如果崩了 这个服务就只做一些熔断的措施,返回1用户这个服务崩了,然后继续下一层服务,保证服务尽可能正常运行。
Hystrix:处理分布式系统的延迟和容错的开源库,在分布式系统里面很多依赖不可避免调用失败超时等,Hystrix保证在一个依赖出问题的情况下,不会导致整体服务失败,避免连级故障,提高分布式系统弹性。
1.导入依赖
org.springframework.cloud spring-cloud-starter-hystrix1.4.6.RELEASE
2.编写配置文件
server:
port: 8009
#mybatis配置
mybatis:
type-aliases-package: com.it.pojo
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
#spring配置
spring:
application:
name: springcloud-provider-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8
username: root
password: root
#Eureka配置
#Eureka配置
eureka:
client:
service-url: #监控页面
defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/ #发布位置, 可以设置集群 ,所有直接把所有集群 都加上去
instance:
instance-id: springcloud-dept-hystrix-8009 #修改eureka 上的默认描述信息
#info配置
info:
app.name: song's first springcloud program
company.name: song666
3.开启功能
model层等的书写,如上面的提供者。
然后写相应的控制器来开启熔断
package com.it.controller;
import com.it.pojo.Dept;
import com.it.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
//提供restful服务
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@GetMapping("/dept/get/{id}")
@HystrixCommand(fallbackMethod = "hystrixGet")
public Dept get(@PathVariable("id") Long id)
{
Dept dept=deptService.queryById(id);
if (dept==null)
{
throw new RuntimeException("id=>"+id+"不存在该用户,或者信息没找到");
}
return dept;
}
//熔断
public Dept hystrixGet(@PathVariable("id") Long id)
{
return new Dept()
.setId(id)
.setName("id=>"+id+"没有对应的信息,null--@Hystrix")
.setDb_source("no this database in Mysql");
}
}
Hystrix服务降级
客户端 多提供一个服务,然后 当客户端服务器崩了时,提供一个服务 来进行降级处理,输出处理的结果。客户正常运行,
服务熔断:是服务降级的一种特殊情况,防止服务器雪崩采取的措施
服务降级:释放服务资源来保证核心业务正常高效运行。当某个服务熔断或关闭之前,服务不再被调用,此时在客户端准备一个FallbackFactory,返回一个默认值,整体的服务水平下降。
实现步骤:
1.多设置一个服务端,在服务端设置降级 (端口和用户相同,此时的feign端 就相当于是 用户,不需要再开启一个用户端)
server:
port: 8002
#Eureka
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://localhost:7002/eureka/,http://localhost:7001/eureka/
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#feign 开启降级feign,hystrix
feign:
hystrix:
enabled: true
2.服务端启动类上加上 降级的注解
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"com.it"})
@ComponentScan("com.it")
public class FeDeptuser80 {
public static void main(String[] args) {
SpringApplication.run(FeDeptuser80.class,args);
}
}
3.在接口端 设置对应的降级控制器
package com.it.service;
import com.it.pojo.Dept;
import feign.hystrix.FallbackFactory;
import org.springframework.core.annotation.AliasFor;
import org.springframework.stereotype.Component;
import java.util.List;
//降级
@Component
public class DeptClientServiceFallbackFactory implements FallbackFactory {
public DeptClientService create(Throwable throwable)
{
return new DeptClientService() {
@Override
public Dept queryById(Long id) {
return new Dept()
.setId(id)
.setName("id=>"+id+"没有对应的信息,客户端提供降级的信息,这个服务器现在已经关闭")
.setDb_source("空,没有数据");
}
@Override
public List queryAll() {
return null;
}
@Override
public Boolean addDept(Dept dept) {
return null;
}
};
}
}
4.接口端的接口开启降级注解
@Component
@FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT",fallbackFactory = DeptClientServiceFallbackFactory.class)
public interface DeptClientService {
@GetMapping("/dept/get/{id}")
public Dept queryById(@PathVariable("id") Long id);
@GetMapping("/dept/list")
public List queryAll();
@GetMapping("/dept/add")
public Boolean addDept(Dept dept);
}
5.测试
当客户 访问服务的时候,提供端 8001如果崩了的话,就会直接进行服务降级,进行提示。
1.新建一个模块然后导包 还有 配置端口
org.springframework.boot spring-boot-starter-actuatororg.springframework.cloud spring-cloud-starter-hystrix-dashboard1.4.6.RELEASE org.springframework.cloud spring-cloud-starter-hystrix1.4.6.RELEASE org.springframework.cloud spring-cloud-starter-ribbon1.4.6.RELEASE org.springframework.cloud spring-cloud-starter-eureka-server1.4.6.RELEASE org.example springclound-api1.0-SNAPSHOT org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-devtools
2.写启动类 开启 监控页面
package com.it;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableHystrixDashboard //开启监控页面
public class DeptUserDashboard_9001 {
public static void main(String[] args) {
SpringApplication.run(DeptUserDashboard_9001.class, args);
}
}
3.测试
4.在提供服务的服务端 中 添加 相应的监控依赖
org.springframework.boot
spring-boot-starter-actuator
5.然后在服务端启动类上添加监控
//启动类
@SpringBootApplication
@EnableEurekaServer
@EnableDiscoveryClient //服务发现
public class DeptProvide {
public static void main(String[] args) {
SpringApplication.run(DeptProvide.class,args);
}
//增加一个servlet
@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet()
{
ServletRegistrationBean registrationBean=new ServletRegistrationBean(new HystrixMetricsStreamServlet());
registrationBean.addUrlMappings("/actuator/hystrix.stream");
return registrationBean;
}
}
6.在 监控页面 添加相应的地址 也就是 上面启动类上的流 然后就可以进行监控
7.在提供服务端 的控制器内 需要监控的方法上加上 @HystrixCommand注解
路由,代理,过滤
路由的请求: 外部的请求转发到具体的微服务实例上,用来跳转的
路由的过滤: 对七八个球的处理过程进行干预,校验服务聚合
Zuul和Eureka进行整合,把Zuul自身注册为Eureka下的应用,同时从Eureka获得其他微服务的消息,访问微服务也是用Zuul跳转
1.新建模块,导包
org.springframework.cloud
spring-cloud-starter-zuul
1.4.6.RELEASE
2.写控制器 加相应的 注解
@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication_9527 {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication_9527.class,args);
}
}
3.配置yml
server:
port: 9528
spring:
application:
name: springcloud-zuul
#Eureka
eureka:
client:
service-url:
defaultZone: http://localhost:7002/eureka/,http://localhost:7001/eureka/
instance:
instance-id: zuul9527.com
prefer-ip-address: true
info:
app.name: song
company.name: aaa
zuul:
routes:
mydept.serviceId: springcloud-provider-dept #这里其实就是提供者的服务名字 http://localhost:9528/springcloud-provider-dept/dept/get/2
mydept.path: /mydept/** #代替上面的路径 http://localhost:9528/mydept/dept/get/3
ignored-services: "*" #不在使用服务的那个路径 只能使用指定的
prefix: /song #设置公共前缀 http://localhost:9528/song/mydept/dept/get/3
4.直接进行访问
1.本地连接上github
可以去看我的这个博客来链接git和github操作
2.传送到github上的appliaction配置
spring:
profiles:
active:
---
spring:
profiles: dev
application:
name: springcloud-config-dev
---
spring:
profiles: test
application:
name: springcloud-config-test
3.开始配连接github的模块来连接远程
1.导报
org.springframework.cloud spring-cloud-config-server2.1.1.RELEASE org.springframework.boot spring-boot-starter-weborg.springframework.cloud spring-cloud-starter-eureka1.4.6.RELEASE
1.在yml里面配置自己的远程地址
server:
port: 3344
spring:
application:
name: springcloud-config-server
#连接远程仓库
cloud:
config:
server:
git:
uri: https://github.com/project-oys/springcloud.git #http的
2.启动类去连接
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class config_serve3344 {
public static void main(String[] args) {
SpringApplication.run(config_serve3344.class,args);
}
}
4.直接在后面加上要读的文件就可以读取
http://localhost:3344/main/application-dev.yml
客户端连接github配置
1.在github上传入客户端的相关配置
spring:
profiles:
active: dev
---
server:
port:8201
#spring配置
spring:
profiles: dev
application:
name: springcloud-provider-dept
#Eureka配置
eureka:
client:
service-url: #监控页面
defaultZone: http://localhost:7001/eureka/ #发布位置, 可以设置集群 ,所有直接把所有集群 都加上去
---
server:
port:8201
#spring配置
spring:
profiles: test
application:
name: springcloud-provider-dept
#Eureka配置
eureka:
client:
service-url: #监控页面
defaultZone: http://localhost:7001/eureka/ #发布位置, 可以设置集群 ,所有直接把所有集群 都加上去
2.开启客户端模块并且导依赖
org.springframework.cloud spring-cloud-starter-config2.1.1.RELEASE org.springframework.boot spring-boot-starter-weborg.springframework.cloud spring-cloud-starter-eureka1.4.6.RELEASE
3.配置bootstrap.yml(系统级别的配置)
spring:
cloud:
config:
name: config-client #需要获取的文件
profile: dev
label: main
uri: http://localhost:3344
4.配置application.yml
spring: application: name: springcloud-config-client-3355
5.调一个控制层来调取github上获取的东西
package com.it.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigClientController {
@Value("${spring.application.name}")
private String applicationName;
@Value("${eureka.client.service-url.defaultZone}")
private String eurekaServer;
@Value("${spring.application.name}")
private String port;
@RequestMapping("/config")
public String getConfig()
{
return "application.name"+applicationName+"eureka"+eurekaServer+"port"+port;
}
}
6.启动类测试
package com.it;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConfigClient_3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClient_3355.class,args);
}
}



