springcloud :
**eureka :**
1> pom
spring-cloud-starter-config
spring-cloud-starter-eureka
spring-boot-starter-test
**eureka-server [服务注册中心] :**
1> application.properties
# 名称
spring.application.name=@project.name@
# 端口
server.port=8761
#hostname
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
#serviceUrl
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
2> application.java
@SpringBootApplication
@EnableEurekaServer
3> pom:
spring-cloud-starter-eureka-server
**eureka-client [服务提供者,可部署多个,改变端口即可] :**
1> application.properties
# 名称 : 依据此名称会进行消费
spring.application.name=service-hi
# 端口
server.port=8762
# eureka-server 的地址
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
2> application.java
@SpringBootApplication
@EnableEurekaClient
3> pom:
spring-cloud-starter-eureka
spring-boot-starter-web
4> controller :
@RequestMapping("/hello")
public String from(){return "hello world!";};
**eureka-feign [服务消费者,消费时自己同时也可以作为一个服务提供者] :**
1> application.properties
# 名称 : 依据此名称会进行消费
spring.application.name=service-feign
# 端口
server.port=8765
# eureka-server 的地址
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
2> application.java
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
3> 消费 :
// 服务提供者的名字 : spring.application.name
@FeignClient(value = "service-hi")
public interface HelloService {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hiService();
}
4> pom :
spring-boot-starter-web
spring-cloud-starter-feign
**eureka-ribbon [服务消费者,消费时自己同时也可以作为一个服务提供者] :**
1> application.properties
# 名称 : 依据此名称会进行消费
spring.application.name=service-ribbon
# 端口
server.port=8764
# eureka-server 的地址
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
2> application.java
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
3> pom
spring-cloud-starter-ribbo
4> 消费
@Autowired
RestTemplate restTemplate;
public String hiService() {
// eureka 界面看到的application
System.err.println("----" + restTemplate.getForObject("http://SERVICE-HI/hello", String.class));
return "ribbon-" + restTemplate.getForObject("http://SERVICE-HI/hello", String.class);
}
**hystrix [熔断] :**
1> application.properties
spring.application.name=service-feign
server.port=8767
# 新增
feign.hystrix.enabled=true
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
2> application.java
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrix
// 使用ribbon时需要
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
3> pom :
spring-cloud-starter-ribbon
spring-boot-starter-web
spring-cloud-starter-feign
spring-cloud-starter-hystrix
4> ribbon 使用 :
@Service
public class RibbonService {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "hiError")
public String hiService() {
System.err.println("----" + restTemplate.getForObject("http://SERVICE-HI/hello", String.class));
return "ribbon-" + restTemplate.getForObject("http://SERVICE-HI/hello", String.class);
}
public String hiError() {
return "hi,sorry,error!";
}
}
5> feign使用 :
@FeignClient(value = "service-hi",fallback = SchedualServiceHiHystric.class)
public interface FeignService {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hiService();
}
@Component
public class SchedualServiceHiHystric implements FeignService {
@Override
public String hiService() {
return "sorry";
}
}
**zuul [路由网关、过滤、安全认证] :**
1> application.properties
spring.application.name=service-zuul
server.port=8769
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
zuul.routes.api-a.path=/api-a/**
zuul.routes.api-a.serviceId=service-ribbon
zuul.routes.api-b.path=/api-b/**
zuul.routes.api-b.serviceId=service-feign
2> application.java
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
@EnableDiscoveryClient
3> 新增service
/**
* ZUUL服务过滤与安全认证
*/
/**
* filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下:
pre:路由之前
routing:路由之时
post: 路由之后
error:发送错误调用
* filterOrder:过滤的顺序
* shouldFilter:这里可以写逻辑判断,是否要过滤,本文true,永远过滤。
* run:过滤器的具体逻辑。可用很复杂,包括查sql,nosql去判断该请求到底有没有权限访问。
*/
@Component
public class MyFilter extends ZuulFilter {
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 0;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
System.err.println(request.getMethod() + " >>> " + request.getRequestURL().toString());
Object accessToken = request.getParameter("token");
if (accessToken == null) {
System.err.println("token is empty");
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(401);
try {
ctx.getResponse().getWriter().write("token is empty");
} catch (Exception e) {
}
return null;
}
System.err.println("ok");
return null;
}
}
4> pom
spring-boot-starter-web
spring-cloud-starter-zuul
**config [配置中心] :**
1> pom
spring-cloud-starter-config
spring-cloud-starter-eureka
spring-boot-starter-test
config-server [配置中心服务] :
1> application.properties
# 服务名称
spring.application.name=config-server
# 服务端口
server.port=8888
# 配置中心GIT
spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/
# 配置中心读取路径
spring.cloud.config.server.git.searchPaths=respo
# 分支
spring.cloud.config.label=master
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
2> application.java
/**
* spring.cloud.config.server.git.uri:配置git仓库地址
* spring.cloud.config.server.git.searchPaths:配置仓库路径
* spring.cloud.config.label:配置仓库的分支
* spring.cloud.config.server.git.username:访问git仓库的用户名
* spring.cloud.config.server.git.password:访问git仓库的用户密码
*/
/**
* 远程仓库https://github.com/forezp/SpringcloudConfig/ 中有个文件config-client-dev.properties文件中有一个属性:
* foo = foo version 3
* 启动程序:访问http://localhost:8888/foo/dev : {"name":"foo","profiles":["dev"],"label":"master",version":"792ffc77c03f4b138d28e89b576900ac5e01a44b","state":null,"propertySources":[]}
*/
/**
* http请求地址和资源文件映射如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
*/
@SpringBootApplication
@EnableConfigServer
3> pom
spring-boot-starter-web
spring-cloud-config-server
**config-client [配置中心客户端] :**
1> bootstrap.properties
spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri= http://localhost:8888/
server.port=8881
management.endpoints.web.exposure.include=bus-refresh
2> application.java
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@RestController
// 更改刷新
@RefreshScope
@Value("${foo}")
String foo;
3> pom
spring-boot-starter-web
spring-cloud-starter-config
**configcloud [高可用分布式配置中心] :**
1> pom
spring-cloud-starter-config
spring-cloud-starter-eureka
spring-boot-starter-test
**configcloud-eureka-server [配置中心集群服务注册中心] :**
1> application.properties
spring.application.name=configcloud-eureka-server
server.port=8889
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
2> application.java
@EnableEurekaServer
@SpringBootApplication
3> pom
spring-cloud-starter-eureka-server
spring-boot-starter-web
spring-cloud-config-server
**configcloud-server [配置中心-服务端] :**
1> application.properties
spring.application.name=config-server
server.port=8888
spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/
spring.cloud.config.server.git.searchPaths=respo
spring.cloud.config.label=master
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
eureka.client.serviceUrl.defaultZone=http://localhost:8889/eureka/
2> application.java
@SpringBootApplication
@EnableConfigServer
@EnableEurekaServer
3> pom
spring-cloud-starter-eureka-server
spring-boot-starter-web
spring-cloud-config-server
**configcloud-client [配置中心-客户端] :**
1> bootstrap.properties
spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri= http://localhost:8888/
eureka.client.serviceUrl.defaultZone=http://localhost:8889/eureka/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
server.port=8881
management.endpoints.web.exposure.include=bus-refresh
2> application.java
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@RestController
@RefreshScope // 更改刷新
// 从配置中心获取的数据
@Value("${foo}")
String foo;
/**
* 配置文件新增: management.endpoints.web.exposure.include=bus-refresh
* http://localhost:8881/actuator/bus-refresh 进行 配置文件刷新
*/
3> pom
spring-boot-starter-web
spring-cloud-starter-config
Java-springcloud-学习笔记
最新推荐文章于 2022-12-16 20:34:13 发布