Spring cloud 基础框架集成

1. 注册中心 -eurekar

1. pom依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.forezp</groupId>
  6. <artifactId>sc-f-chapter1</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>pom</packaging>
  9. <name>sc-f-chapter1</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.3.RELEASE</version>
  15. <relativePath/>
  16. </parent>
  17. <modules>
  18. <module>eureka-server</module>
  19. <module>service-hi</module>
  20. </modules>
  21. <properties>
  22. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  23. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  24. <java.version>1.8</java.version>
  25. <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
  26. </properties>
  27. <dependencies>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-test</artifactId>
  31. <scope>test</scope>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.cloud</groupId>
  35. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  36. </dependency>
  37. </dependencies>
  38. <dependencyManagement>
  39. <dependencies>
  40. <dependency>
  41. <groupId>org.springframework.cloud</groupId>
  42. <artifactId>spring-cloud-dependencies</artifactId>
  43. <version>${spring-cloud.version}</version>
  44. <type>pom</type>
  45. <scope>import</scope>
  46. </dependency>
  47. </dependencies>
  48. </dependencyManagement>
  49. <build>
  50. <plugins>
  51. <plugin>
  52. <groupId>org.springframework.boot</groupId>
  53. <artifactId>spring-boot-maven-plugin</artifactId>
  54. </plugin>
  55. </plugins>
  56. </build>
  57. </project>

2. Application.java

  1. @SpringBootApplication
  2. @EnableEurekaServer
  3. public class EurekaServerApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run( EurekaServerApplication.class, args );
  6. }
  7. }

3. yml配置

  1. server:
  2. port: 8761
  3. eureka:
  4. instance:
  5. hostname: localhost
  6. client:
  7. registerWithEureka: false
  8. fetchRegistry: false
  9. serviceUrl:
  10. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  11. spring:
  12. application:
  13. name: eurka-server

2. 服务提供者 -server

1. pom依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.forezp</groupId>
  6. <artifactId>sc-f-chapter1</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>pom</packaging>
  9. <name>sc-f-chapter1</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.3.RELEASE</version>
  15. <relativePath/>
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-web</artifactId>
  31. </dependency>
  32. </dependencies>
  33. <build>
  34. <plugins>
  35. <plugin>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-maven-plugin</artifactId>
  38. </plugin>
  39. </plugins>
  40. </build>
  41. </project>

2. Application.java

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. @RestController
  4. public class ServiceHiApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run( ServiceHiApplication.class, args );
  7. }
  8. @Value("${server.port}")
  9. String port;
  10. @RequestMapping("/hi")
  11. public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
  12. return "hi " + name + " ,i am from port:" + port;
  13. }
  14. }

3. yml配置

  1. server:
  2. port: 8762
  3. spring:
  4. application:
  5. name: service-hi
  6. eureka:
  7. client:
  8. serviceUrl:
  9. defaultZone: http://localhost:8761/eureka/

3. Ribbon消费者服务中心

1. pom依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <properties>
  5. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  6. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  7. <java.version>1.8</java.version>
  8. <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
  9. </properties>
  10. <dependencies>
  11. <!-- 1: 发现中心 -->
  12. <dependency>
  13. <groupId>org.springframework.cloud</groupId>
  14. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-web</artifactId>
  19. </dependency>
  20. <!-- 2: ribbon代理 -->
  21. <dependency>
  22. <groupId>org.springframework.cloud</groupId>
  23. <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
  24. </dependency>
  25. <!-- 3: hystrix熔断 -->
  26. <dependency>
  27. <groupId>org.springframework.cloud</groupId>
  28. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  29. </dependency>
  30. <!-- 4:配置中心客户端 -->
  31. <dependency>
  32. <groupId>org.springframework.cloud</groupId>
  33. <artifactId>spring-cloud-starter-config</artifactId>
  34. </dependency>
  35. <!-- 5:链路追踪客户端 -->
  36. <dependency>
  37. <groupId>org.springframework.cloud</groupId>
  38. <artifactId>spring-cloud-starter-zipkin</artifactId>
  39. </dependency>
  40. <!-- 6:-hystrix-dashboard客户端 -->
  41. <dependency>
  42. <groupId>org.springframework.cloud</groupId>
  43. <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
  44. </dependency>
  45. <!-- 7:监控中心客户端 -->
  46. <dependency>
  47. <groupId>de.codecentric</groupId>
  48. <artifactId>spring-boot-admin-starter-client</artifactId>
  49. <version>2.1.0</version>
  50. </dependency>
  51. </dependencies>
  52. <build>
  53. <plugins>
  54. <plugin>
  55. <groupId>org.springframework.boot</groupId>
  56. <artifactId>spring-boot-maven-plugin</artifactId>
  57. </plugin>
  58. </plugins>
  59. </build>
  60. </project>

2. Application.java

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. @EnableDiscoveryClient
  4. @RestController
  5. @EnableHystrix
  6. @EnableHystrixDashboard
  7. public class ServiceRibbonApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run( ServiceRibbonApplication.class, args );
  10. }
  11. @Bean
  12. @LoadBalanced
  13. RestTemplate restTemplate() {
  14. return new RestTemplate();
  15. }
  16. @Autowired
  17. HelloService helloService;
  18. @GetMapping(value = "/hi")
  19. public String hi(@RequestParam String name) {
  20. return helloService.hiService( name );
  21. }
  22. }
  23. @Service
  24. public class HelloService {
  25. @Autowired
  26. RestTemplate restTemplate;
  27. public String hiService(String name) {
  28. return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
  29. }
  30. @HystrixCommand(fallbackMethod = "hiError")
  31. public String hiService(String name) {
  32. return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
  33. }
  34. //http://localhost:8762/actuator/hystrix.stream
  35. }
  • @EnableHystrix注解开启Hystrix

  • 在方法上加上@HystrixCommand注解 绑定熔断处理

  • 加上@EnableHystrix注解开启断路器,这个是必须的,并且需要在程序中声明断路点HystrixCommand;加上@EnableHystrixDashboard注解,开启HystrixDashboard

3. yml配置

  1. server:
  2. port: 8761
  3. eureka:
  4. instance:
  5. hostname: localhost
  6. client:
  7. registerWithEureka: false
  8. fetchRegistry: false
  9. serviceUrl:
  10. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  11. spring:
  12. application:
  13. name: eurka-server

4. bootstrap.properties

  1. ##配置中心客户端
  2. spring.application.name=config-client
  3. spring.cloud.config.label=master
  4. spring.cloud.config.profile=dev
  5. spring.cloud.config.uri= http://localhost:8888/
  6. server.port=8881
  7. ##链路追踪客户端
  8. server.port=8988
  9. spring.zipkin.base-url=http://localhost:9411
  10. spring.application.name=service-hi

4. Feign消费者服务中心

1. pom依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <properties>
  5. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  6. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  7. <java.version>1.8</java.version>
  8. <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
  9. </properties>
  10. <dependencies>
  11. <!-- 1: 发现中心依赖 -->
  12. <dependency>
  13. <groupId>org.springframework.cloud</groupId>
  14. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-web</artifactId>
  19. </dependency>
  20. <!-- 2: feign代理 -->
  21. <dependency>
  22. <groupId>org.springframework.cloud</groupId>
  23. <artifactId>spring-cloud-starter-openfeign</artifactId>
  24. </dependency>
  25. <!-- 3: hystrix熔断 -->
  26. <dependency>
  27. <groupId>org.springframework.cloud</groupId>
  28. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  29. </dependency>
  30. <!-- 4:配置中心客户端 -->
  31. <dependency>
  32. <groupId>org.springframework.cloud</groupId>
  33. <artifactId>spring-cloud-starter-config</artifactId>
  34. </dependency>
  35. <!-- 5:链路追踪客户端 -->
  36. <dependency>
  37. <groupId>org.springframework.cloud</groupId>
  38. <artifactId>spring-cloud-starter-zipkin</artifactId>
  39. </dependency>
  40. <!-- 6:-hystrix-dashboard客户端 -->
  41. <dependency>
  42. <groupId>org.springframework.cloud</groupId>
  43. <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
  44. </dependency>
  45. <!-- 7:监控中心客户端 -->
  46. <dependency>
  47. <groupId>de.codecentric</groupId>
  48. <artifactId>spring-boot-admin-starter-client</artifactId>
  49. <version>2.1.0</version>
  50. </dependency>
  51. </dependencies>
  52. <build>
  53. <plugins>
  54. <plugin>
  55. <groupId>org.springframework.boot</groupId>
  56. <artifactId>spring-boot-maven-plugin</artifactId>
  57. </plugin>
  58. </plugins>
  59. </build>
  60. </project>

2. Application.java

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. @EnableDiscoveryClient
  4. @EnableFeignClients
  5. @RestController
  6. @EnableHystrix
  7. @EnableEurekaServer
  8. public class ServiceFeignApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run( ServiceFeignApplication.class, args );
  11. }
  12. //编译器报错,无视。 因为这个Bean是在程序启动的时候注入的,编译器感知不到,所以报错。
  13. @Autowired
  14. SchedualServiceHi schedualServiceHi;
  15. @GetMapping(value = "/hi")
  16. public String sayHi(@RequestParam String name) {
  17. return schedualServiceHi.sayHiFromClientOne( name );
  18. }
  19. }
  20. @FeignClient(value = "service-hi")
  21. public interface SchedualServiceHi {
  22. @RequestMapping(value = "/hi",method = RequestMethod.GET)
  23. String sayHiFromClientOne(@RequestParam(value = "name") String name);
  24. }
  25. @Component
  26. public class SchedualServiceHiHystric implements SchedualServiceHi {
  27. @Override
  28. public String sayHiFromClientOne(String name) {
  29. return "sorry "+name;
  30. }
  31. }
  • @ FeignClient(“服务名”),来指定调用哪个服务

  • @EnableHystrix注解开启Hystrix

  • @EnableEurekaServer

3. yml配置

  1. eureka:
  2. client:
  3. serviceUrl:
  4. defaultZone: http://localhost:8761/eureka/
  5. server:
  6. port: 8765
  7. spring:
  8. application:
  9. name: service-feign
  10. boot:
  11. admin:
  12. client:
  13. url: http://localhost:8769
  14. management:
  15. endpoints:
  16. web:
  17. exposure:
  18. include: '*'
  19. endpoint:
  20. health:
  21. show-details: ALWAYS
  • feign是自带断路器的,在D版本的Spring Cloud之后,它没有默认打开。需要在配置文件中配置打开它

feign.hystrix.enabled=true

4.配置中心客户端

bootstrap.properties

  1. spring.application.name=config-client
  2. spring.cloud.config.label=master
  3. spring.cloud.config.profile=dev
  4. #spring.cloud.config.uri= http://localhost:8888/
  5. eureka.client.serviceUrl.defaultZone=http://localhost:8889/eureka/
  6. spring.cloud.config.discovery.enabled=true
  7. spring.cloud.config.discovery.serviceId=config-server
  8. server.port=8881

5. 配置中心 - configserver

1. pom依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.forezp</groupId>
  6. <artifactId>sc-f-chapter1</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>pom</packaging>
  9. <name>sc-f-chapter1</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.3.RELEASE</version>
  15. <relativePath/>
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-config-server</artifactId>
  31. </dependency>
  32. </dependencies>
  33. <build>
  34. <plugins>
  35. <plugin>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-maven-plugin</artifactId>
  38. </plugin>
  39. </plugins>
  40. </build>
  41. </project>

2. Application.java

  1. @SpringBootApplication
  2. @EnableConfigServer
  3. public class ConfigServerApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(ConfigServerApplication.class, args);
  6. }
  7. }
  • @EnableConfigServer注解开启配置服务器的功能

3. 配置

  1. spring.application.name=config-server
  2. server.port=8888
  3. spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/
  4. spring.cloud.config.server.git.searchPaths=respo
  5. spring.cloud.config.label=master
  6. spring.cloud.config.server.git.username=
  7. spring.cloud.config.server.git.password=
  8. spring.application.name=config-server
  9. server.port=8888
  10. spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/
  11. spring.cloud.config.server.git.searchPaths=respo
  12. spring.cloud.config.label=master
  13. spring.cloud.config.server.git.username= your username
  14. spring.cloud.config.server.git.password= your password
  • 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仓库的用户密码

http请求地址和资源文件映射如下:

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

6. 网关中心 - zull

1. pom依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.forezp</groupId>
  6. <artifactId>sc-f-chapter1</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>pom</packaging>
  9. <name>sc-f-chapter1</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.3.RELEASE</version>
  15. <relativePath/>
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-web</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.cloud</groupId>
  34. <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
  35. </dependency>
  36. </dependencies>
  37. <build>
  38. <plugins>
  39. <plugin>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-maven-plugin</artifactId>
  42. </plugin>
  43. </plugins>
  44. </build>
  45. </project>

2. Application.java

  1. @SpringBootApplication
  2. @EnableZuulProxy
  3. @EnableEurekaClient
  4. @EnableDiscoveryClient
  5. public class ServiceZuulApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run( ServiceZuulApplication.class, args );
  8. }
  9. }
  • @EnableZuulProxy,开启zuul的功能

网关过滤

  1. @Component
  2. public class MyFilter extends ZuulFilter {
  3. private static Logger log = LoggerFactory.getLogger(MyFilter.class);
  4. @Override
  5. public String filterType() {
  6. return "pre";
  7. }
  8. @Override
  9. public int filterOrder() {
  10. return 0;
  11. }
  12. @Override
  13. public boolean shouldFilter() {
  14. return true;
  15. }
  16. @Override
  17. public Object run() {
  18. RequestContext ctx = RequestContext.getCurrentContext();
  19. HttpServletRequest request = ctx.getRequest();
  20. log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));
  21. Object accessToken = request.getParameter("token");
  22. if(accessToken == null) {
  23. log.warn("token is empty");
  24. ctx.setSendZuulResponse(false);
  25. ctx.setResponseStatusCode(401);
  26. try {
  27. ctx.getResponse().getWriter().write("token is empty");
  28. }catch (Exception e){}
  29. return null;
  30. }
  31. log.info("ok");
  32. return null;
  33. }
  34. }
  • filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下:

    • pre:路由之前
    • routing:路由之时
    • post: 路由之后
    • error:发送错误调
    • filterOrder:过滤的顺序
    • shouldFilter:这里可以写逻辑判断,是否要过滤,本文true,永远过滤。
    • run:过滤器的具体逻辑。可用很复杂,包括查sql,nosql去判断该请求到底有没有权限访问

3. yml配置

  1. eureka:
  2. client:
  3. serviceUrl:
  4. defaultZone: http://localhost:8761/eureka/
  5. server:
  6. port: 8769
  7. spring:
  8. application:
  9. name: service-zuul
  10. zuul:
  11. routes:
  12. api-a:
  13. path: /api-a/**
  14. serviceId: service-ribbon
  15. api-b:
  16. path: /api-b/**
  17. serviceId: service-feign

7. 链路中心 -Cloud Sleuth

服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件 ,在spring Cloud为F版本的时候,已经不需要自己构建Zipkin Server了,只需要下载jar即可,下载地址:

https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/

java -jar zipkin-server-2.10.1-exec.jar //http://localhost:9494

客户端

1. pom依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.forezp</groupId>
  6. <artifactId>sc-f-chapter1</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>pom</packaging>
  9. <name>sc-f-chapter1</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.3.RELEASE</version>
  15. <relativePath/>
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-sleuth</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-sleuth-zipkin</artifactId>
  31. </dependency>
  32. </dependencies>
  33. <build>
  34. <plugins>
  35. <plugin>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-maven-plugin</artifactId>
  38. </plugin>
  39. </plugins>
  40. </build>
  41. </project>

2. Application.java

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. @EnableDiscoveryClient
  4. @RestController
  5. @RefreshScope
  6. public class ConfigClientApplication {
  7. /**
  8. * http://localhost:8881/actuator/bus-refresh
  9. */
  10. public static void main(String[] args) {
  11. SpringApplication.run(ConfigClientApplication.class, args);
  12. }
  13. @Value("${foo}")
  14. String foo;
  15. @RequestMapping(value = "/hi")
  16. public String hi(){
  17. return foo;
  18. }
  19. }
  • @EnableZuulProxy,开启zuul的功能

3. 配置

  1. server.port=8988
  2. spring.zipkin.base-url=http://localhost:9411
  3. spring.application.name=service-hi
  4. spring.zipkin.sender.type: web
  5. #设置采样率默认为 0.1 注意之前的版本是percentage 新版本中更换为 probability
  6. spring.sleuth.sampler.probability: 1

8. 断路器聚合监控(Hystrix Turbine)

利用Hystrix Dashboard去监控断路器的Hystrix command。当我们有很多个服务的时候,这就需要聚合所以服务的Hystrix Dashboard的数据了。这就需要用到Spring Cloud的另一个组件了,即Hystrix Turbine

1. pom依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-actuator</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.cloud</groupId>
  16. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.cloud</groupId>
  20. <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
  25. </dependency>
  26. </dependencies>

2. Application.java

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. @EnableDiscoveryClient
  4. @RestController
  5. @EnableHystrix
  6. @EnableHystrixDashboard
  7. @EnableCircuitBreaker
  8. @EnableTurbine
  9. public class ServiceTurbineApplication {
  10. /**
  11. * http://localhost:8764/turbine.stream
  12. */
  13. public static void main(String[] args) {
  14. SpringApplication.run( ServiceTurbineApplication.class, args );
  15. }
  16. }
  • @EnableTurbine注解包含了@EnableDiscoveryClient注解,即开启了注册服务

3. 配置

  1. server:
  2. port: 8764
  3. spring:
  4. application:
  5. name: service-turbine
  6. eureka:
  7. client:
  8. serviceUrl:
  9. defaultZone: http://localhost:8761/eureka/
  10. management:
  11. endpoints:
  12. web:
  13. exposure:
  14. include: "*"
  15. cors:
  16. allowed-origins: "*"
  17. allowed-methods: "*"
  18. turbine:
  19. app-config: service-hi,service-lucy
  20. aggregator:
  21. clusterConfig: default
  22. clusterNameExpression: new String("default")
  23. combine-host: true
  24. instanceUrlSuffix:
  25. default: actuator/hystrix.stream

9. Spring Cloud Gateway网关

Spring Cloud Gateway是Spring Cloud官方推出的第二代网关框架,取代Zuul网关。网关作为流量的,在微服务系统中有着非常作用,网关常见的功能有路由转发、权限校验、限流控制等作用

1. pom依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.cloud</groupId>
  8. <artifactId>spring-cloud-starter-gateway</artifactId>
  9. </dependency>
  10. </dependencies>

2. Application.java

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. @EnableDiscoveryClient
  4. @RestController
  5. @RefreshScope
  6. public class ConfigClientApplication {
  7. /**
  8. * http://localhost:8881/actuator/bus-refresh
  9. */
  10. public static void main(String[] args) {
  11. SpringApplication.run(ConfigClientApplication.class, args);
  12. }
  13. @Value("${foo}")
  14. String foo;
  15. @RequestMapping(value = "/hi")
  16. public String hi(){
  17. return foo;
  18. }
  19. }
  • @EnableZuulProxy,开启zuul的功能

3. 配置

  1. server:
  2. port: 8081
  3. spring:
  4. application:
  5. name: sc-gateway-service
  6. cloud:
  7. gateway:
  8. discovery:
  9. locator:
  10. enabled: true
  11. lowerCaseServiceId: true
  12. eureka:
  13. client:
  14. service-url:
  15. defaultZone: http://localhost:8761/eureka/

10. 线路中心 - Bus

1. pom依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.forezp</groupId>
  6. <artifactId>sc-f-chapter1</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>pom</packaging>
  9. <name>sc-f-chapter1</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.3.RELEASE</version>
  15. <relativePath/>
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-config</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-web</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.cloud</groupId>
  34. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.cloud</groupId>
  38. <artifactId>spring-cloud-starter-bus-amqp</artifactId>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-starter-actuator</artifactId>
  43. </dependency>
  44. </dependencies>
  45. <build>
  46. <plugins>
  47. <plugin>
  48. <groupId>org.springframework.boot</groupId>
  49. <artifactId>spring-boot-maven-plugin</artifactId>
  50. </plugin>
  51. </plugins>
  52. </build>
  53. </project>

2. Application.java

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. @EnableDiscoveryClient
  4. @RestController
  5. @RefreshScope
  6. public class ConfigClientApplication {
  7. /**
  8. * http://localhost:8881/actuator/bus-refresh
  9. */
  10. public static void main(String[] args) {
  11. SpringApplication.run(ConfigClientApplication.class, args);
  12. }
  13. @Value("${foo}")
  14. String foo;
  15. @RequestMapping(value = "/hi")
  16. public String hi(){
  17. return foo;
  18. }
  19. }
  • @EnableZuulProxy,开启zuul的功能

3. 配置

  1. spring.rabbitmq.host=localhost
  2. spring.rabbitmq.port=5672
  3. spring.rabbitmq.username=guest
  4. spring.rabbitmq.password=guest
  5. spring.cloud.bus.enabled=true
  6. spring.cloud.bus.trace.enabled=true
  7. management.endpoints.web.exposure.include=bus-refresh

11.监控中心 -adminserver

1. pom依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.forezp</groupId>
  6. <artifactId>sc-f-chapter1</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>pom</packaging>
  9. <name>sc-f-chapter1</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.3.RELEASE</version>
  15. <relativePath/>
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>de.codecentric</groupId>
  34. <artifactId>spring-boot-admin-starter-server</artifactId>
  35. <version>2.1.0</version>
  36. </dependency>
  37. </dependencies>
  38. <build>
  39. <plugins>
  40. <plugin>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-maven-plugin</artifactId>
  43. </plugin>
  44. </plugins>
  45. </build>
  46. </project>

2. Application.java

  1. @SpringBootApplication
  2. @EnableAdminServer
  3. public class ConfigClientApplication {
  4. /**
  5. * http://localhost:8881/actuator/bus-refresh
  6. */
  7. public static void main(String[] args) {
  8. SpringApplication.run(ConfigClientApplication.class, args);
  9. }
  10. }
  • @EnableAdminServer注解,开启AdminServer的功能

3. 配置

  1. spring:
  2. application:
  3. name: admin-server
  4. server:
  5. port: 8769

4.客户端

依赖

  1. <dependency>
  2. <groupId>de.codecentric</groupId>
  3. <artifactId>spring-boot-admin-starter-client</artifactId>
  4. <version>2.1.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>

配置

  1. spring:
  2. application:
  3. name: admin-client
  4. boot:
  5. admin:
  6. client:
  7. url: http://localhost:8769
  8. server:
  9. port: 8768
  10. management:
  11. endpoints:
  12. web:
  13. exposure:
  14. include: '*'
  15. endpoint:
  16. health:
  17. show-details: ALWAYS

Spring cloud 基础框架集成的更多相关文章

  1. Spring MVC 学习总结(十)——Spring+Spring MVC+MyBatis框架集成(IntelliJ IDEA SSM集成)

    与SSH(Struts/Spring/Hibernate/)一样,Spring+SpringMVC+MyBatis也有一个简称SSM,Spring实现业务对象管理,Spring MVC负责请求的转发和 ...

  2. Spring+SpringMvc+Mybatis框架集成搭建教程

    一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以在自己搭建SSM框架集成的时候,出现了这样或者那样的问题,很是苦恼 ...

  3. Spring Cloud基础教程

    Spring Cloud基础教程  2017-04-04 被围观 90375 次 该教程内容不定时更新,如您对这些内容感兴趣,可以关注我的博客或微信公众号! 本教程示例代码: GitHub:https ...

  4. Spring Cloud基础教程视频教程

    视频课程包含: Spring Cloud基础视频教程24G 目录 获取方式: 关注公众微信号:博涵大数据 或者扫描下面的二维码关注获取. 关注后在公众平台上回复"SpringCloud基础& ...

  5. 基础架构之spring cloud基础架构

    这篇文章是给公司设计的微服务基础架构,包括架构设计.部署流程.部署架构.开发Tip等等.这里分享出来,如果对看官们有点用,我就非常的高兴了. 首页 2. 架构设计 3. 部署流程 4. 部署架构 5. ...

  6. Spring cloud整体框架

    研究了一段时间spring boot了准备向spirng cloud进发,公司架构和项目也全面拥抱了Spring Cloud.在使用了一段时间后发现Spring Cloud从技术架构上降低了对大型系统 ...

  7. Spring Cloud 研发框架demo

    第一步:准备工作 1.下载并集成公司自定义maven maven包见QQ群文件 2.克隆Git源码到本地eclipse: xx 3.构建项目 一键初始化parent:run as maven inst ...

  8. spring cloud 项目相关集成简介

    Spring Cloud Config 配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储.Git以及Subversion. Spring Cloud Bus 事件.消 ...

  9. Spring Cloud基础

    1.网站架构演变过程 传统架构(单点应用SSM或SSH)→分布式架构(项目拆分)→SOA架构(面向服务架构)→微服务架构 2.微服务概述 2.1SOA架构 面向服务的架构(SOA)是一个组件模型,它将 ...

随机推荐

  1. 基于autofac的属性注入

    基于autofac的属性注入 什么是属性注入 在了解属性注入之前,要先了解一下DI(Dependency Injection),即依赖注入.在ASP.NET Core里自带了一个IOC容器,而且程序支 ...

  2. Spring 声明式事务与编程式事务详解

    本文转载自IBM开发者论坛:https://developer.ibm.com/zh/articles/os-cn-spring-trans 根据自己的学习理解有所调整,用于学习备查. 事务管理对于企 ...

  3. IDA 创建本地类型

    在IDA中我们常常使用 shift+F9打开结构体视图,ins 创建结构体,但操作有些繁琐. 我们可以在View-->Open Subviews-->Local Types(视图--> ...

  4. 4、Spring教程之Spring配置

    别名 alias 设置别名 , 为bean设置别名 , 可以设置多个别名 <!--设置别名:在获取Bean的时候可以使用别名获取--> <alias name="userT ...

  5. 【LiteOS】LiteOS消息队列-实战

    目录 前言 链接 参考 笔录草稿 创建测试任务 部分源码 前言 链接 LiteOS源码链接 常见问题 华为开发者社区 华为LiteOS官方教程 我的gitee-LiteOS-mcu 参考 上面链接 笔 ...

  6. oo第四单元——UML图解析

    本单元是在理解UML图的基础上实现对图的解析和检查.UML图是新接触的一种建模工具,一开始接触UML的时候觉得理解起来比较困难,并不能单纯从代码的角度按照类.方法这样来理解,这只是从类图的角度,还有从 ...

  7. oo第二单元——多线程魔鬼电梯

    在初步认识了面向对象思想后,立刻进入了多线程的学习,本单元的难点主要是锁的理解,需要保证线程安全的同时防止死锁的发生,也要尽可能缩小锁的范围,提高性能.这一单元以电梯为载体,让我们从生活出发,从电梯运 ...

  8. 简述Java多线程(一)

    JAVA多线程 程序:是指令和数据的有序集合,其本身没有任何运行的含义,是一个静态的概念. 进程:是执行程序的一次执行过程,是一个动态的概念,是系统资源分配的单位. 线程是CPU调度和执行的单位. 创 ...

  9. Kubernetes Secrets

    Secrets 背景信息 Kubernetes版本 [09:08:04 yhf@test ~]$ kubectl version Client Version: version.Info{Major: ...

  10. Gson?So easy.

    1.概述 这篇文章主要讲述了Gson的使用.包括从最基础的基本类型的序列化,到对象,数组,集合,再到Gson注解,Gson Builder,再到格式化,自定义序列化与反序列化等内容. 另外文章篇幅较长 ...