spring cloud 微服务

服务注册   (  Eureka )

                                 服务端:

                            需要jar   

                               <dependency>
                                   <groupId>org.springframework.cloud</groupId>
                                  <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
                             </dependency>

spring cloud 启动文件上配置
                                     @EnableEurekaServer
application.yml  配置

                       server:
                                port: 8786

                         eureka:
                                  instance:
                                             hostname: localhost
                                   client:
                                           registerWithEureka: false
                                            fetchRegistry: false
                                   serviceUrl:
                                             defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

                          spring:
                                application:
                                               name: eurka-server

                              客户端:

                                     需要jar:

                                  <dependency>
                                                <groupId>org.springframework.cloud</groupId>
                                                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
                                  </dependency>

spring  boot启动文件: 

                        @EnableEurekaClient

application.yml配置:

server:
  port: 8762

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8786/eureka/     //对应服务端的defaultZone

spring:
  application:
    name: eurka-client

此时,8082端口的应用就被注册到了8081服务注册中心。就可以通过http://localhost:8081查看服务注册情况                               

负载均衡(ribbon)

                需要jar

                              <dependency>
                                   <groupId>org.springframework.cloud</groupId>
                                   <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
                               </dependency>

application.yml配置:

eureka:
  client:
    serviceUrl:
        defaultZone: http://localhost:8786/eureka/     //对应服务端的defaultZone
server:
  port: 8764
spring:
  application:
    name: service-ribbon

spring boot启动文件 :

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class ServiceRibbonApplication {

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

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

}
 

测试类:

@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    public String hiService(String name) {
        return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
    }


}


spring cloud微服务

断路器(Hystrix)

需要jar:

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  </dependency>
在原来的 8764 服务的启动类上加:

@EnableHystrix

修改其测试类:

@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hiError")
    public String hiService(String name) {
        return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
    }

    public String hiError(String name) {
        return "hi,"+name+",sorry,error!";
    }

}

 

路由(Zuul)

需要jar:

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>

application.yml配置:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8769
spring:
  application:
    name: service-zuul
zuul:
  routes:
    api-a:
      path: /api-a/**
      serviceId: service-ribbon
    api-b:
      path: /api-b/**
      serviceId: service-feign

启动类:

@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
@EnableDiscoveryClient
public class ServiceZuulApplication {

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

依次运行打开浏览器访问:http://localhost:8769/api-a/hi?name=forezp ;

zuul还可以进行路由过滤:

@Component
public class MyFilter extends ZuulFilter {

    private static Logger log = LoggerFactory.getLogger(MyFilter.class);
    @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();
        log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));
        Object accessToken = request.getParameter("token");
        if(accessToken == null) {
            log.warn("token is empty");
            ctx.setSendZuulResponse(false);
            ctx.setResponseStatusCode(401);
            try {
                ctx.getResponse().getWriter().write("token is empty");
            }catch (Exception e){}

            return null;
        }
        log.info("ok");
        return null;
    }
}

filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下:
pre:路由之前
routing:路由之时
post: 路由之后
error:发送错误调用
filterOrder:过滤的顺序
shouldFilter:这里可以写逻辑判断,是否要过滤,本文true,永远过滤。
run:过滤器的具体逻辑。可用很复杂,包括查sql,nosql去判断该请求到底有没有权限访问。
 

配置中心()

                服务端:  

           <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
          </dependency>

      application.yml配置:
spring:
  cloud:
    config:
      server:
        git:
          password:       //远程git的密码
          searchPaths: respo  //配置仓库路径
          uri: https://github.com/forezp/SpringcloudConfig/    //远程git的地址
          username:      //远程git的用户名
      label: master     //配置仓库的分支
  application:
    name: config-server
server:
  port: 8888

springboot的启动类配置:@EnableConfigServer

http请求地址和资源文件映射如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

根据我们自己的配置,我们可以这样访问:http://127.0.0.1:8888/eureka-server/default/master

application -> eureka-server (应用名)
profile -> default (启用的配置,通常是后缀,下面解释)
label -> master (分支)

profile比较重要,可以理解成读取哪些配置文件,假如我不止一个配置文件,可能会有:

eureka-server.properties(这个是通用配置文件,默认都会加载),
eureka-server-mysql.properties,
eureka-server-oracle.properties,
eureka-server-jpa.properties,
eureka-server-mysql.properties......

我们可能会选择性的加载其中的部分properties配置文件,那我们可以这样写:http://127.0.0.1:8888/eureka-server/default,mysql,jpa/master

 

客户端:<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

application.yml配置:

spring:
  cloud:
    config:
      profile: dev
      label: master
      uri: http://localhost:8888/
  application:
    name: config-client
server:
  port: 8881

springboot启动类:

@SpringBootApplication
@RestController
public class Application {

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

    @Value("${foo}")
    String foo; //对应配置文件中的key
    @RequestMapping(value = "/hi")
    public String hi(){
        return foo;
    }
}

同时客户端的项目名必须和git的配置文件保持一致否则找不到,如果客户端项目名是config-client 对应git配置文件是config-client-*.*的形式

可以把服务端和客户端都注册到注册中心,配置方法如上配置中心,配置的客户端就可以通过

spring.cloud.config.discovery.enabled=true

spring.cloud.config.discovery.serviceId=config-server

来代替spring.cloud.config.uri= http://localhost:8888/ 执行配置服务器

消息总线

对应代码https://github.com/taihexuelang/resp/tree/master/springCloud

相关文章: