一 开始配置config服务
-
config-server
-
1 <dependency> 2 <groupId>org.springframework.cloud</groupId> 3 <artifactId>spring-cloud-config-server</artifactId> 4 </dependency> 5 6 <dependency> 7 <groupId>org.springframework.cloud</groupId> 8 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 9 </dependency>
-
application.yml
1 #服务名称 2 spring: 3 application: 4 name: config-server 5 cloud: 6 config: 7 server: 8 git: 9 uri: http://192.168.180.112/root/test.git 10 username: root 11 password: 19920220ljyp 12 default-label: master 13 14 15 #服务的端口号 16 server: 17 port: 9100 18 19 20 #指定注册中心地址 21 eureka: 22 client: 23 serviceUrl: 24 defaultZone: http://localhost:8761/eureka/
-
启动类
1 @SpringBootApplication 2 @EnableConfigServer 3 public class ConfigServerApplication { 4 5 public static void main(String[] args) { 6 SpringApplication.run(ConfigServerApplication.class, args); 7 } 8 9 }
-
-
在gitlab上配置服务的yml文件
product-service.yml
1 # eureka: 2 # client: 3 # serviceUrl: 4 # defaultZone: http://localhost:8761/eureka/ 5 # instance: 6 # instance-id: product-service8080 7 # prefer-ip-address: true 8 9 server: 10 port: 8771 11 spring: 12 application: 13 name: product-service 14 zipkin: 15 base-url: http://192.168.180.113:9411/ 16 sleuth: 17 sampler: 18 probability: 1 19 20 info: 21 app.name: product-servic 22 company.name: www.topcheer.com
order-service.yml
1 server: 2 port: 8781 3 4 5 #指定注册中心地址 6 eureka: 7 client: 8 serviceUrl: 9 defaultZone: http://localhost:8761/eureka/ 10 11 #服务的名称 12 spring: 13 application: 14 name: order-service 15 redis: 16 port: 6379 17 host: 192.168.180.113 18 timeout: 2000 19 zipkin: 20 base-url: http://192.168.180.113:9411/ 21 sleuth: 22 sampler: 23 probability: 1 24 25 ###配置请求超时时间 26 hystrix: 27 command: 28 default: 29 execution: 30 isolation: 31 thread: 32 timeoutInMilliseconds: 5000 33 ribbon: 34 ##指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间。 35 ReadTimeout: 2000 36 ##指的是建立连接后从服务器读取到可用资源所用的时间。 37 ConnectTimeout: 3000 38 feign: 39 hystrix: 40 enabled: true 41 management: 42 endpoints: 43 web: 44 exposure: 45 include: "*" 46 47 48 #自定义负载均衡策略 49 #product-service: 50 # ribbon: 51 # NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
api-gateway.yml
1 server: 2 port: 9001 3 4 # spring: 5 # application: 6 # name: api-gateway 7 8 9 10 11 #指定注册中心地址 12 # eureka: 13 # client: 14 # serviceUrl: 15 # defaultZone: http://localhost:8761/eureka/ 16 17 zuul: 18 routes: 19 order-service: /apigateway/** 20 product-service: /apigateway1/** 21 sensitive-headers: 22 #统一入口为上面的配置,其他入口忽略 23 #ignored-patterns: /*-service/** 24 25 hystrix: 26 command: 27 default: 28 execution: 29 isolation: 30 thread: 31 timeoutInMilliseconds: 5000 32 ribbon: 33 ##指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间。 34 ReadTimeout: 2000 35 ##指的是建立连接后从服务器读取到可用资源所用的时间。 36 ConnectTimeout: 5000 37 feign: 38 hystrix: 39 enabled: true 40
测试:可以直接打开yml(改名:bootstrap.yml)
-
其他服务
1 spring: 2 application: 3 name: product-service 4 cloud: 5 config: 6 discovery: 7 service-id: CONFIG-SERVER 8 enabled: true 9 label: master 10 添加pom.xml 11 12 <dependency> 13 <groupId>org.springframework.cloud</groupId> 14 <artifactId>spring-cloud-config-client</artifactId> 15 </dependency>
可以发现通过config-server可以读取配置,正常可以调用。