微服务搭建
一、微服务常用几大组件
服务治理: Spring Cloud Eureka
客户端负载均衡: Spring Cloud Ribbon
服务容错保护: Spring Cloud Hystrix
声明式服务调用: Spring Cloud Feign
API 网关服务:Spring Cloud Zuul
二、微服务搭建步骤
2.1、搭建SpringBoot项目
1)使用Spring Initializr搭建一个SpringBoot项目
2)创建HelloController类
3)执行WorldApplication启动类,浏览器中访问 http://localhost:8080/hello
2.2、搭建注册中心Eureka
1)在上面项目上右键-》New-》Module-》创建一个项目
2)在pom文件中导入依赖
这里要加上版本号
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
</dependencies>
3)编写启动类EurekaApplication
package com.helloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //标志这是一个注册中心
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
4)编写配置文件application.yml
server:
port: 8001 # 服务端口
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false # 是否向 Eureka 注册服务。该应用为服务注册中心,不需要自注册,设置为 false
fetchRegistry: false # 是否检索服务。该应用为服务注册中心,职责为注册和发现服务,无需检索服务,设置为 false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 注册中心url
spring:
application:
name: eurka-server #服务名称
5)启动项目访问http://localhost:8001/
2.3、搭建提供者
1)在上面项目上右键-》New-》Module-》创建一个项目
2)编写配置文件application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8001/eureka/
server:
port: 8002
spring:
application:
name: ribbon-provider
3)启动类添加@EnableEurekaClient注册服务,然后注入RestTemplate对象,@LoadBalanced表示开启负载均衡
4)创建ProviderController
package com.cloud.provider.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProviderController {
@GetMapping(value = "/index")
public String index() {
return "hello provider!";
}
}
5)启动项目,访问提供者http://localhost:8002/index,可以看到返回结果。
注册中心出现数据
2.4、搭建消费者
2.4.1、基于Ribbon+RestTemplate的消费者
1)在上面项目上右键-》New-》Module-》创建一个项目
2)编写配置文件application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8001/eureka/
server:
port: 8003
spring:
application:
name: ribbon-consumer
3)启动类配置
package com.cloud.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
public class CustomerApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
4)创建CustomerController
package com.cloud.consumer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ConsumerController {
@Autowired
RestTemplate restTemplate;
@GetMapping(value = "/index")
public String index() {
return "消费者ribbon consumer-------- --- " + restTemplate.getForObject("http://ribbon-provider/index", String.class);
}
}
5)启动项目,访问消费者http://localhost:8003/index。
消费者ribbon consumer-------- — 是消费者自身的信息,
hello provider!是提供者提供的内容
6)访问注册中心http://localhost:8001/,可以看的提供者和消费者都已经注册成功。
2.4.2、基于Feign的消费者
基于Feign的消费者
2.5、搭建zuul
1)在上面项目上右键-》New-》Module-》创建一个项目
2)编写配置文件application.yml
server:
port: 8004 #端口号
spring:
application:
name: Zuul #注册名字
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8001/eureka/ #注册中心地址http://localhost:8001/eureka/
zuul:
routes:
servicel:
path: /hello/** #外界请求路径名称
service-id: ribbon-consumer #服务名称 一般指的是消费者的名称 也可以是提供者的名称 这个名称需要和注册中心 一致
3)启动类配置
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableEurekaClient //注册服务
@EnableZuulProxy //启用zuul代理
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
4)启动测试
注意:
用户发出的所有请求都是先经过zuul的
访问时其实还是访问的消费者的接口地址,只不过过滤了一下需要加一个配置文件中的请求路径的前缀而已。
2.6、搭建熔断器Hystrix
1)在上面项目上右键-》New-》Module-》创建一个项目
2)编写配置文件application.properties
# 这里只给一个端口号是为了看到效果
server.port=8005
3)启动类配置
package com.cloud.hystrix;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableHystrixDashboard //开启Hystrix
public class HystrixApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixApplication.class, args);
}
}
4)启动测试,看到野猪表示成功!localhost:8005/hystrix
在提供者中启用熔断器
1)在提供者的pom中导入hystrix的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2)在提供者的注解上加一个注解
@EnableCircuitBreaker启用熔断器监控
或者是使用:@EnableHystrix
3)配置Servlet,Hystrix的数据采集 通过这个Servlet获取服务的状态 (HystrixMetricsStreamServlet)
package com.cloud.hystrix.config;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration //标志是一个配置类
public class HystrixConfig {
@Bean
public ServletRegistrationBean registra(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.setServlet(new HystrixMetricsStreamServlet());
servletRegistrationBean.addUrlMappings("/actuator/hystrix.stream"); //查看监控的url
servletRegistrationBean.setName("hystrixMetricsStreamServlet"); //起一个名字
return servletRegistrationBean;
}
}
4)启动并测试
文章仅供学习交流,侵权联系删除。