搭建配置中心Config与实战

2019-10-08  本文已影响0人  长孙俊明

创建GIT

使用码云创建一个项目并创建一个配置文件


image.png

创建ConfigServer

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

application.properties

spring.cloud.config.server.git.uri=https://gitee.com/loiy/peizhizhongxinceshi.git
spring.cloud.config.server.git.search-paths=testconfig
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=

spring.application.name=Spring Cloud Config
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8100/eureka/

package com.springcloudtest.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
    }

}
image.png

创建ConfigClient

创建Client项目之前,得注意一个坑。必须创建配置文件bootstrap.properties/yml,如果创建application.properties/yml,那么server.port必须是8888,否则会抛出
org.springframework.beans.factory.BeanCreationException: Error creating bean with nameInjection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'xxx' in value "${xxx}。

配置在application.properties/yml都不行,必须是配置在bootstrap.properties/yml里面,否则还是会取本机的8888端口!!!
官网原话:Bootstrap properties are added with high precedence, so they cannot be overridden by local configuration, by default.
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>

bootstrap.yml

###服务启动端口号
server:
 port: 8001
###服务名称(服务注册到eureka名称)
spring:
   application:
       name: test-configClient #如果配置了Config,该名称必须跟Git上的配置文件前缀一致。如:test-configClient-prd.properties
   cloud:
     config:
       # 读取版本环境,也就是配置文件的后缀名。如:test-configClient-prd.properties
       profile: prd
       discovery:
         # 就是读取configserver服务,写成configserver的spring.application.name
         service-id: config-server
         enabled: true
###服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka
ribbon:
  ReadTimeout: 6000
  ConnectTimeout: 6000

# 禁止服务超时时间
hystrix:
  command:
    default:
      execution:
        timeout:
          enabled: false
package com.springcloudtest.order.api;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.springcloudtest.order.api.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private MemberService memberService;

    // 读取ConfigServer服务上的值
    @Value("${dburl}")
    private String dburl;

    @RequestMapping("/getOrder")
    @HystrixCommand(fallbackMethod = "getErrorTip")
    public String getOrder() {
        System.out.println("orderToMemberUserInfoHystrix:" + "线程池名称:" + Thread.currentThread().getName());
       // return restTemplate.getForObject("http://APP-MEMBER/geteMember", String.class);
        return memberService.getMember();
    }

    @RequestMapping("/getDburl")
    public String getDburl() {
        return dburl;
    }

    public String getErrorTip() {
        return "访问超时,请稍后重试。";
    }
}

package com.springcloudtest.order;

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.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }


}

结果

image.png

不重启服务,手动刷新配置

借助actuator实现手动刷新。

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.springcloudtest.order.api.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
// 必须添加这个
@RefreshScope
public class OrderController {
}

application.properties

# 开启所有端点management:
management:
  endpoints:
    web:
      exposure:
        include: "*"

最重要的环节需要通过post方式手动刷新这个地址。可以借助postman软件
http://localhost:8001/actuator/refresh

image.png

当每次在git上面变更了配置,都需要手动刷新下地址。

上一篇下一篇

猜你喜欢

热点阅读